Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: mark name flag as required #20

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions cmd/helmrelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ with kubectl.`,
helmReleaseNamespace, _ := cmd.Flags().GetString("namespace")
confirmMigrate, _ := cmd.Flags().GetBool("confirm-migrate")

if helmReleaseName == "" || helmReleaseNamespace == "" {
log.Fatal("Both --name and --namespace flags must be provided")
if helmReleaseName == "" {
log.Fatal("Flag --name must be provided")
}

// Set up the default context
Expand Down Expand Up @@ -208,7 +208,6 @@ func GetHelmRepoNamespace(helmRelease *helmv2.HelmRelease) string {

func init() {
rootCmd.AddCommand(helmreleaseCmd)
rootCmd.MarkPersistentFlagRequired("name")

helmreleaseCmd.Flags().Bool("confirm-migrate", false, "Automatically Migrate the HelmRelease to an ApplicationSet")
}
56 changes: 33 additions & 23 deletions cmd/helmrelease_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,49 @@ package cmd
import (
helmv2 "github.com/fluxcd/helm-controller/api/v2beta1"
"github.com/magiconair/properties/assert"
"k8s.io/apimachinery/pkg/runtime"
"testing"
)

func TestHelmGetRepoNamespace(t *testing.T) {
scheme := runtime.NewScheme()
helmv2.AddToScheme(scheme)

helmReleaseWithNamespace := &helmv2.HelmRelease{
Spec: helmv2.HelmReleaseSpec{
Chart: helmv2.HelmChartTemplate{
Spec: helmv2.HelmChartTemplateSpec{
SourceRef: helmv2.CrossNamespaceObjectReference{
Namespace: "custom-namespace",
func TestGetHelmRepoNamespace(t *testing.T) {
tests := []struct {
name string
helmRelease *helmv2.HelmRelease
helmRepoNamespace string
}{
{
name: "when helmrelease namespace is defined",
helmRelease: &helmv2.HelmRelease{
Spec: helmv2.HelmReleaseSpec{
Chart: helmv2.HelmChartTemplate{
Spec: helmv2.HelmChartTemplateSpec{
SourceRef: helmv2.CrossNamespaceObjectReference{
Namespace: "custom-namespace",
},
},
},
},
},
helmRepoNamespace: "custom-namespace",
},
}

namespace := GetHelmRepoNamespace(helmReleaseWithNamespace)
assert.Equal(t, "custom-namespace", namespace, "Expected the namespace to be 'custom-namespace'")

helmReleaseWithoutNamespace := &helmv2.HelmRelease{
Spec: helmv2.HelmReleaseSpec{
Chart: helmv2.HelmChartTemplate{
Spec: helmv2.HelmChartTemplateSpec{
SourceRef: helmv2.CrossNamespaceObjectReference{},
{
name: "when helmrelease namespace is not defined",
helmRelease: &helmv2.HelmRelease{
Spec: helmv2.HelmReleaseSpec{
Chart: helmv2.HelmChartTemplate{
Spec: helmv2.HelmChartTemplateSpec{
SourceRef: helmv2.CrossNamespaceObjectReference{},
},
},
},
},
helmRepoNamespace: "",
},
}

namespace = GetHelmRepoNamespace(helmReleaseWithoutNamespace)
assert.Equal(t, helmReleaseWithoutNamespace.Namespace, namespace, "Expected the namespace to be 'default'")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
namespace := GetHelmRepoNamespace(tt.helmRelease)
assert.Equal(t, namespace, tt.helmRepoNamespace)
})
}
}
5 changes: 4 additions & 1 deletion cmd/kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ with kubectl.`,
kustomizationNamespace, _ := cmd.Flags().GetString("namespace")
confirmMigrate, _ := cmd.Flags().GetBool("confirm-migrate")

if kustomizationName == "" {
log.Fatal("Flag --name must be provided")
}

// Set up the default context
ctx := context.TODO()

Expand Down Expand Up @@ -214,7 +218,6 @@ with kubectl.`,

func init() {
rootCmd.AddCommand(kustomizationCmd)
rootCmd.MarkPersistentFlagRequired("name")

kustomizationCmd.Flags().Bool("confirm-migrate", false, "Automatically Migrate the Kustomization to an ApplicationSet")
kustomizationCmd.Flags().StringSlice("exclude-dirs", []string{}, "Additional Directories (besides flux-system) to exclude from the GitDir generator. Can be single or comma separated")
Expand Down
8 changes: 0 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,12 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)

// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.mta.yaml)")

rootCmd.PersistentFlags().String("kubeconfig", "", "Path to the kubeconfig file to use (if not the standard one).")
rootCmd.PersistentFlags().String("name", "", "Name of Kustomization or HelmRelease to export")
rootCmd.PersistentFlags().String("namespace", "flux-system", "Namespace of where the Kustomization or HelmRelease is")
rootCmd.PersistentFlags().String("argocd-namespace", "argocd", "Namespace where Argo CD is installed")

// Cobra also supports local flags, which will only run
// when this action is called directly.
//rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

// initConfig reads in config file and ENV variables if set.
Expand Down
9 changes: 3 additions & 6 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -16,7 +16,6 @@ limitations under the License.
package cmd

import (
"encoding/json"
"fmt"

"github.com/spf13/cobra"
Expand All @@ -26,11 +25,9 @@ import (
var versionCmd = &cobra.Command{
Use: "version",
Short: "Displays version.",
Long: `This command will display the version of the CLI in json format`,
Long: `This command will display the version of the CLI`,
Run: func(cmd *cobra.Command, args []string) {
versionMap := map[string]string{rootCmd.Use: rootCmd.Version}
versionJson, _ := json.Marshal(versionMap)
fmt.Println(string(versionJson))
fmt.Println(rootCmd.Version)
},
}

Expand Down