Skip to content

Commit 518d8f8

Browse files
committed
fix: cobra could throw error that will end up in json parser
Signed-off-by: Timur Tuktamyshev <[email protected]>
1 parent fd2a647 commit 518d8f8

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

pkg/app/root.go

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,36 @@ import (
88

99
"github.com/spf13/cobra"
1010

11+
"github.com/deckhouse/deckhouse/pkg/log"
12+
1113
"github.com/deckhouse/module-sdk/internal/controller"
1214
)
1315

1416
func newCMD(controller *controller.HookController) *cmd {
1517
return &cmd{
1618
controller: controller,
19+
logger: log.Default(),
1720
}
1821
}
1922

2023
type cmd struct {
2124
controller *controller.HookController
25+
logger *log.Logger
2226
}
2327

2428
// Execute adds all child commands to the root command and sets flags appropriately.
2529
// This is called by main.main(). It only needs to happen once to the rootCmd.
2630
func (c *cmd) Execute() {
31+
2732
rootCmd := c.rootCmd()
2833
rootCmd.AddCommand(c.hooksCmd())
2934
rootCmd.CompletionOptions.DisableDefaultCmd = true
35+
rootCmd.SilenceUsage = true
36+
rootCmd.SilenceErrors = true
3037

3138
err := rootCmd.Execute()
3239
if err != nil {
40+
c.logger.Error("failed to execute root command", "error", err)
3341
os.Exit(1)
3442
}
3543
}
@@ -67,77 +75,82 @@ func (c *cmd) hooksCmd() *cobra.Command {
6775
},
6876
})
6977

70-
hooksCmd.AddCommand(&cobra.Command{
78+
configCmd := &cobra.Command{
7179
Use: "config",
7280
Short: "Print hooks configs",
7381
Long: `Print list of hooks configs in json format`,
74-
RunE: func(_ *cobra.Command, _ []string) error {
82+
Run: func(_ *cobra.Command, _ []string) {
7583
err := c.controller.PrintHookConfigs()
7684
if err != nil {
77-
return fmt.Errorf("can not print configs: %w", err)
85+
c.logger.Error("can not print configs", "error", err)
86+
os.Exit(1)
7887
}
79-
80-
return nil
8188
},
82-
})
89+
}
90+
hooksCmd.AddCommand(configCmd)
8391

84-
hooksCmd.AddCommand(&cobra.Command{
92+
dumpCmd := &cobra.Command{
8593
Use: "dump",
8694
Short: "Dump hooks configs",
8795
Long: `Dump list of hooks configs in config.json file`,
8896
Hidden: true,
89-
RunE: func(_ *cobra.Command, _ []string) error {
97+
Run: func(_ *cobra.Command, _ []string) {
9098
err := c.controller.WriteHookConfigsInFile()
9199
if err != nil {
92-
return fmt.Errorf("can not write configs to file: %w", err)
100+
c.logger.Error("can not write configs to file", "error", err)
101+
os.Exit(1)
93102
}
94103

95104
fmt.Println("dump successfully")
96-
97-
return nil
98105
},
99-
})
106+
}
107+
hooksCmd.AddCommand(dumpCmd)
100108

101-
hooksCmd.AddCommand(&cobra.Command{
109+
runCmd := &cobra.Command{
102110
Use: "run",
103111
Short: "Running hook",
104112
Long: `Run hook from binary registry`,
105113
Hidden: true,
106-
Args: cobra.ExactArgs(1),
107-
RunE: func(cmd *cobra.Command, args []string) error {
114+
Args: func(cmd *cobra.Command, args []string) error {
115+
if len(args) != 1 {
116+
c.logger.Error("invalid number of arguments", "expected", 1, "received", len(args))
117+
os.Exit(1)
118+
}
119+
return nil
120+
},
121+
Run: func(cmd *cobra.Command, args []string) {
108122
ctx := cmd.Context()
109123

110124
idxRaw := args[0]
111125
idx, err := strconv.Atoi(idxRaw)
112126
if err != nil {
113-
return fmt.Errorf("argument '%s' is not integer", idxRaw)
127+
c.logger.Error("invalid argument", "argument", idxRaw, "error", err)
128+
os.Exit(1)
114129
}
115130

116131
err = c.controller.RunHook(ctx, idx)
117132
if err != nil {
118-
return fmt.Errorf("run hook error: %w", err)
133+
c.logger.Warn("hook shutdown", "error", err)
119134
}
120-
121-
return nil
122135
},
123-
})
136+
}
137+
hooksCmd.AddCommand(runCmd)
124138

125-
hooksCmd.AddCommand(&cobra.Command{
139+
readyCmd := &cobra.Command{
126140
Use: "ready",
127141
Short: "Check readiness",
128142
Long: `Run readiness hook for module`,
129143
Hidden: true,
130-
RunE: func(cmd *cobra.Command, _ []string) error {
144+
Run: func(cmd *cobra.Command, _ []string) {
131145
ctx := cmd.Context()
132146

133147
err := c.controller.RunReadiness(ctx)
134148
if err != nil {
135-
return fmt.Errorf("run readiness hook error: %w", err)
149+
c.logger.Warn("readiness hook shutdown", "error", err)
136150
}
137-
138-
return nil
139151
},
140-
})
152+
}
153+
hooksCmd.AddCommand(readyCmd)
141154

142155
return hooksCmd
143156
}

0 commit comments

Comments
 (0)