diff --git a/cmd/helmrelease.go b/cmd/helmrelease.go index a98e71c..6b8b7f1 100644 --- a/cmd/helmrelease.go +++ b/cmd/helmrelease.go @@ -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 @@ -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") } diff --git a/cmd/helmrelease_test.go b/cmd/helmrelease_test.go index 2942938..a72022a 100644 --- a/cmd/helmrelease_test.go +++ b/cmd/helmrelease_test.go @@ -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) + }) + } } diff --git a/cmd/kustomization.go b/cmd/kustomization.go index d49d6cc..7dbe1e7 100644 --- a/cmd/kustomization.go +++ b/cmd/kustomization.go @@ -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() @@ -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") diff --git a/cmd/root.go b/cmd/root.go index a2fe770..0b66ac7 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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. diff --git a/cmd/version.go b/cmd/version.go index 7089060..55f11f4 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -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, @@ -16,7 +16,6 @@ limitations under the License. package cmd import ( - "encoding/json" "fmt" "github.com/spf13/cobra" @@ -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) }, }