Skip to content

Commit

Permalink
Bump spf13/cobra to 1.8.0 and adapt PersistentPreRun usage
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <[email protected]>
  • Loading branch information
kke committed Nov 7, 2023
1 parent dc94b85 commit c935483
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 51 deletions.
3 changes: 1 addition & 2 deletions cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ func NewAPICmd() *cobra.Command {
cmd := &cobra.Command{
Use: "api",
Short: "Run the controller API",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
PersistentPreRun: func(cmd *cobra.Command, args []string) {
logrus.SetOutput(cmd.OutOrStdout())
k0slog.SetInfoLevel()
return config.CallParentPersistentPreRun(cmd, args)
},
RunE: func(cmd *cobra.Command, args []string) error {
opts, err := config.GetCmdOpts(cmd)
Expand Down
3 changes: 1 addition & 2 deletions cmd/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ func NewControllerCmd() *cobra.Command {
or CLI flag:
$ k0s controller --token-file [path_to_file]
Note: Token can be passed either as a CLI argument or as a flag`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
PersistentPreRun: func(cmd *cobra.Command, args []string) {
logrus.SetOutput(cmd.OutOrStdout())
k0slog.SetInfoLevel()
return config.CallParentPersistentPreRun(cmd, args)
},
RunE: func(cmd *cobra.Command, args []string) error {
opts, err := config.GetCmdOpts(cmd)
Expand Down
4 changes: 0 additions & 4 deletions cmd/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ func NewEtcdCmd() *cobra.Command {
Use: "etcd",
Short: "Manage etcd cluster",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := config.CallParentPersistentPreRun(cmd, args); err != nil {
return err
}

opts, err := config.GetCmdOpts(cmd)
if err != nil {
return err
Expand Down
4 changes: 0 additions & 4 deletions cmd/kubectl/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,6 @@ func hookKubectlPluginHandler(kubectlCmd *cobra.Command) {
logs.InitLogs()
cobra.OnFinalize(logs.FlushLogs)

if err := config.CallParentPersistentPreRun(kubectlCmd, args); err != nil {
return err
}

if err := fallbackToK0sKubeconfig(cmd); err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ $ k0s completion fish > ~/.config/fish/completions/k0s.fish
}

func Execute() {
cobra.EnableTraverseRunHooks = true
if err := NewRootCmd().Execute(); err != nil {
os.Exit(1)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ func NewWorkerCmd() *cobra.Command {
or CLI flag:
$ k0s worker --token-file [path_to_file]
Note: Token can be passed either as a CLI argument or as a flag`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
PersistentPreRun: func(cmd *cobra.Command, args []string) {
logrus.SetOutput(cmd.OutOrStdout())
k0slog.SetInfoLevel()
return config.CallParentPersistentPreRun(cmd, args)
},
RunE: func(cmd *cobra.Command, args []string) error {
opts, err := config.GetCmdOpts(cmd)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ require (
github.com/rqlite/rqlite v4.6.0+incompatible
github.com/segmentio/analytics-go v3.1.0+incompatible
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
github.com/urfave/cli v1.22.14
Expand Down Expand Up @@ -121,7 +121,7 @@ require (
github.com/containernetworking/plugins v1.2.0 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/daviddengcn/go-colortext v1.0.0 // indirect
Expand Down
74 changes: 71 additions & 3 deletions go.sum

Large diffs are not rendered by default.

32 changes: 0 additions & 32 deletions pkg/config/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/k0sproject/k0s/pkg/k0scloudprovider"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"golang.org/x/exp/slices"
)
Expand Down Expand Up @@ -263,34 +262,3 @@ func GetCmdOpts(cobraCmd command) (*CLIOptions, error) {
DebugListenOn: DebugListenOn,
}, nil
}

// CallParentPersistentPreRun runs the parent command's persistent pre-run.
// Cobra does not do this automatically.
//
// See: https://github.com/spf13/cobra/issues/216
// See: https://github.com/spf13/cobra/blob/v1.4.0/command.go#L833-L843
func CallParentPersistentPreRun(cmd *cobra.Command, args []string) error {
for p := cmd.Parent(); p != nil; p = p.Parent() {
preRunE := p.PersistentPreRunE
preRun := p.PersistentPreRun

p.PersistentPreRunE = nil
p.PersistentPreRun = nil

defer func() {
p.PersistentPreRunE = preRunE
p.PersistentPreRun = preRun
}()

if preRunE != nil {
return preRunE(cmd, args)
}

if preRun != nil {
preRun(cmd, args)
return nil
}
}

return nil
}

0 comments on commit c935483

Please sign in to comment.