Skip to content

Commit 6ae7846

Browse files
committed
Simulate cascading PersistPreRunE calls
Because cobra will only call the last PersistPreRunE we need to simulate a cascading run. spf13/cobra#252 We also switches every function to `...E` alternative
1 parent 4c3c2e2 commit 6ae7846

File tree

4 files changed

+70
-24
lines changed

4 files changed

+70
-24
lines changed

cmd/aws.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cmd
33

44
import (
55
"fmt"
6-
"os"
76

87
"github.com/mateimicu/kdiscover/internal"
98
"github.com/spf13/cobra"
@@ -20,7 +19,20 @@ func newAWSCommand() *cobra.Command {
2019
AWSCommand := &cobra.Command{
2120
Use: "aws",
2221
Short: "Work with AWS EKS clusters",
23-
PersistentPreRun: func(cmd *cobra.Command, args []string) {
22+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
23+
// Because cobra will only run the last PersistentPreRunE
24+
// we need to search for the root and run the function.
25+
// This is not a robust solution as there may be multiple
26+
// PersistentPreRunE function between the leaf command and root
27+
// also this assumes that this is the root one
28+
// An issue about this https://github.com/spf13/cobra/issues/252
29+
root := cmd
30+
for ; root.HasParent(); root = root.Parent() {
31+
}
32+
err := root.PersistentPreRunE(cmd, args)
33+
if err != nil {
34+
return err
35+
}
2436
log.WithFields(log.Fields{
2537
"partitions": awsPartitions,
2638
}).Debug("Search regions for partitions")
@@ -31,15 +43,17 @@ func newAWSCommand() *cobra.Command {
3143
log.WithFields(log.Fields{
3244
"partitions": awsPartitions,
3345
}).Error("Can't find regions for partitions")
34-
os.Exit(errorExitCode)
46+
return fmt.Errorf("Can't find regions for partitions %v", awsPartitions)
3547
}
3648

3749
log.WithFields(log.Fields{
3850
"regions": awsRegions,
3951
}).Info("Founds regions")
52+
return nil
4053
},
41-
Run: func(cmd *cobra.Command, args []string) {
54+
RunE: func(cmd *cobra.Command, args []string) error {
4255
cmd.HelpFunc()(cmd, args)
56+
return nil
4357
},
4458
}
4559

@@ -55,8 +69,6 @@ func newAWSCommand() *cobra.Command {
5569
internal.GetDefaultKubeconfigPath(),
5670
"Path to the kubeconfig to work with")
5771

58-
AWSCommand.AddCommand(newListCommand())
59-
AWSCommand.AddCommand(newUpdateCommand())
60-
72+
AWSCommand.AddCommand(newListCommand(), newUpdateCommand())
6173
return AWSCommand
6274
}

cmd/aws_list.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func newListCommand() *cobra.Command {
1515
listCommand := &cobra.Command{
1616
Use: "list",
1717
Short: "List all EKS Clusters",
18-
Run: func(cmd *cobra.Command, args []string) {
18+
RunE: func(cmd *cobra.Command, args []string) error {
1919
remoteEKSClusters := internal.GetEKSClusters(awsRegions)
2020
log.Info(remoteEKSClusters)
2121

@@ -45,6 +45,8 @@ func newListCommand() *cobra.Command {
4545
})
4646
// render it
4747
fmt.Println(tw.Render())
48+
49+
return nil
4850
},
4951
}
5052

cmd/aws_update.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func newUpdateCommand() *cobra.Command {
2424
updateCommand := &cobra.Command{
2525
Use: "update",
2626
Short: "Update all EKS Clusters",
27-
Run: func(cmd *cobra.Command, args []string) {
27+
RunE: func(cmd *cobra.Command, args []string) error {
2828
fmt.Println(cmd.Short)
2929
remoteEKSClusters := internal.GetEKSClusters(awsRegions)
3030
log.Info(remoteEKSClusters)
@@ -39,15 +39,14 @@ func newUpdateCommand() *cobra.Command {
3939
fmt.Printf("Backup kubeconfig to %v\n", bName)
4040
err = copy(kubeconfigPath, bName)
4141
if err != nil {
42-
fmt.Println(err.Error())
43-
return
42+
return err
4443
}
4544
}
4645
err := internal.UpdateKubeconfig(remoteEKSClusters, kubeconfigPath, contextName{templateValue: alias})
4746
if err != nil {
48-
fmt.Println(err.Error())
49-
return
47+
return err
5048
}
49+
return nil
5150
},
5251
}
5352
updateCommand.Flags().BoolVar(&backupKubeconfig, "backup-kubeconfig", true, "Backup cubeconfig before update")

cmd/root.go

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,50 @@ import (
1515
const errorExitCode int = 1
1616

1717
var (
18+
loggingLevels map[string]log.Level = map[string]log.Level{
19+
"none": 0,
20+
"panic": log.PanicLevel,
21+
"fatal": log.FatalLevel,
22+
"error": log.ErrorLevel,
23+
"warn": log.WarnLevel,
24+
"info": log.InfoLevel,
25+
"debug": log.DebugLevel,
26+
"trace": log.TraceLevel,
27+
}
1828
kubeconfigPath string
19-
debug bool
20-
rootCmd = &cobra.Command{
29+
logLevel string
30+
)
31+
32+
func NewRootCommand() *cobra.Command {
33+
rootCmd := &cobra.Command{
2134
Use: "kdiscover",
2235
Short: "Discover all EKS clusters on an account.",
2336
Long: `kdiscover is a simple utility that can search
2437
all regions on an AWS account and try to find all EKS clsuters.
2538
It will try to upgrade the kube-config for each cluster.`,
2639

27-
PersistentPreRun: func(cmd *cobra.Command, args []string) {
28-
if debug {
29-
log.SetLevel(log.DebugLevel)
30-
} else {
40+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
41+
fmt.Println("root")
42+
if logLevel == "none" {
3143
log.SetOutput(ioutil.Discard)
44+
return nil
45+
}
46+
47+
if v, ok := loggingLevels[logLevel]; ok {
48+
log.SetLevel(v)
49+
return nil
3250
}
51+
52+
return fmt.Errorf("Can't find logging level %v", logLevel)
3353
},
3454

35-
Run: func(cmd *cobra.Command, args []string) {
55+
RunE: func(cmd *cobra.Command, args []string) error {
3656
cmd.HelpFunc()(cmd, args)
57+
return nil
3758
},
3859
}
39-
)
4060

41-
// Execute will create the tree of commands and will start parsing and execution
42-
func Execute() {
43-
rootCmd.PersistentFlags().BoolVar(&debug, "debug", false, "set log level to debug")
61+
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "none", fmt.Sprintf("Set logging lvl. Supported %v", getAllLogglingLevels()))
4462

4563
rootCmd.PersistentFlags().StringVar(
4664
&kubeconfigPath,
@@ -49,9 +67,24 @@ func Execute() {
4967
"Path to the kubeconfig to work with")
5068

5169
rootCmd.AddCommand(newAWSCommand())
70+
return rootCmd
71+
}
72+
73+
// Execute will create the tree of commands and will start parsing and execution
74+
func Execute() {
75+
rootCmd := NewRootCommand()
5276

5377
if err := rootCmd.Execute(); err != nil {
5478
fmt.Println(err)
5579
os.Exit(errorExitCode)
5680
}
5781
}
82+
83+
func getAllLogglingLevels() []string {
84+
keys := make([]string, 0, len(loggingLevels))
85+
for k := range loggingLevels {
86+
keys = append(keys, k)
87+
}
88+
89+
return keys
90+
}

0 commit comments

Comments
 (0)