Skip to content

Commit

Permalink
Add aws namespace to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
mateimicu committed May 1, 2020
1 parent 562efa7 commit 6072a57
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 28 deletions.
62 changes: 62 additions & 0 deletions cmd/aws.go
Original file line number Diff line number Diff line change
@@ -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
}
File renamed without changes.
File renamed without changes.
32 changes: 4 additions & 28 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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) {
Expand All @@ -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)
}
}

0 comments on commit 6072a57

Please sign in to comment.