From 6072a573f1025477fd7a662169fc9776b4c8e372 Mon Sep 17 00:00:00 2001 From: Matei-Marius Micu Date: Fri, 1 May 2020 20:36:48 +0300 Subject: [PATCH] Add `aws` namespace to CLI --- cmd/aws.go | 62 ++++++++++++++++++++++++++++++++ cmd/{list.go => aws_list.go} | 0 cmd/{update.go => aws_update.go} | 0 cmd/root.go | 32 +++-------------- 4 files changed, 66 insertions(+), 28 deletions(-) create mode 100644 cmd/aws.go rename cmd/{list.go => aws_list.go} (100%) rename cmd/{update.go => aws_update.go} (100%) diff --git a/cmd/aws.go b/cmd/aws.go new file mode 100644 index 0000000..574dda2 --- /dev/null +++ b/cmd/aws.go @@ -0,0 +1,62 @@ +// Package cmd offers CLI functionality +package cmd + +import ( + "fmt" + "os" + + "github.com/mateimicu/kdiscover/internal" + "github.com/spf13/cobra" + + log "github.com/sirupsen/logrus" +) + +var ( + awsPartitions []string + awsRegions []string +) + +func newAWSCommand() *cobra.Command { + AWSCommand := &cobra.Command{ + Use: "aws", + Short: "Work with AWS EKS clusters", + PersistentPreRun: func(cmd *cobra.Command, args []string) { + log.WithFields(log.Fields{ + "partitions": awsPartitions, + }).Debug("Search regions for partitions") + + awsRegions = internal.GetRegions(awsPartitions) + + if len(awsRegions) == 0 { + log.WithFields(log.Fields{ + "partitions": awsPartitions, + }).Error("Can't find regions for partitions") + os.Exit(errorExitCode) + } + + log.WithFields(log.Fields{ + "regions": awsRegions, + }).Info("Founds regions") + }, + Run: func(cmd *cobra.Command, args []string) { + cmd.HelpFunc()(cmd, args) + }, + } + + AWSCommand.PersistentFlags().StringSliceVar( + &awsPartitions, + "aws-partitions", + []string{"aws"}, + fmt.Sprintf("In what partitions to search for clusters. Supported %v", internal.AllowedParitions())) + + AWSCommand.PersistentFlags().StringVar( + &kubeconfigPath, + "kubeconfig-path", + internal.GetDefaultKubeconfigPath(), + "Path to the kubeconfig to work with") + + AWSCommand.AddCommand(newListCommand()) + AWSCommand.AddCommand(newUpdateCommand()) + + return AWSCommand +} diff --git a/cmd/list.go b/cmd/aws_list.go similarity index 100% rename from cmd/list.go rename to cmd/aws_list.go diff --git a/cmd/update.go b/cmd/aws_update.go similarity index 100% rename from cmd/update.go rename to cmd/aws_update.go diff --git a/cmd/root.go b/cmd/root.go index 2c92d90..e2bc78e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -12,9 +12,9 @@ import ( log "github.com/sirupsen/logrus" ) +const errorExitCode int = 1 + var ( - awsPartitions []string - awsRegions []string kubeconfigPath string debug bool rootCmd = &cobra.Command{ @@ -30,23 +30,6 @@ It will try to upgrade the kube-config for each cluster.`, } else { log.SetOutput(ioutil.Discard) } - - log.WithFields(log.Fields{ - "partitions": awsPartitions, - }).Debug("Search regions for partitions") - - awsRegions = internal.GetRegions(awsPartitions) - - if len(awsRegions) == 0 { - log.WithFields(log.Fields{ - "partitions": awsPartitions, - }).Error("Can't find regions for partitions") - os.Exit(1) - } - - log.WithFields(log.Fields{ - "regions": awsRegions, - }).Info("Founds regions") }, Run: func(cmd *cobra.Command, args []string) { @@ -59,23 +42,16 @@ It will try to upgrade the kube-config for each cluster.`, func Execute() { rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "set log level to debug") - rootCmd.PersistentFlags().StringSliceVar( - &awsPartitions, - "aws-partitions", - []string{"aws"}, - fmt.Sprintf("In what partitions to search for clusters. Supported %v", internal.AllowedParitions())) - rootCmd.PersistentFlags().StringVar( &kubeconfigPath, "kubeconfig-path", internal.GetDefaultKubeconfigPath(), "Path to the kubeconfig to work with") - rootCmd.AddCommand(newListCommand()) - rootCmd.AddCommand(newUpdateCommand()) + rootCmd.AddCommand(newAWSCommand()) if err := rootCmd.Execute(); err != nil { fmt.Println(err) - os.Exit(1) + os.Exit(errorExitCode) } }