@@ -8,17 +8,21 @@ 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
1416func newCMD (controller * controller.HookController ) * cmd {
1517 return & cmd {
1618 controller : controller ,
19+ logger : log .Default (),
1720 }
1821}
1922
2023type 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.
@@ -27,9 +31,12 @@ func (c *cmd) Execute() {
2731 rootCmd := c .rootCmd ()
2832 rootCmd .AddCommand (c .hooksCmd ())
2933 rootCmd .CompletionOptions .DisableDefaultCmd = true
34+ rootCmd .SilenceUsage = true
35+ rootCmd .SilenceErrors = true
3036
3137 err := rootCmd .Execute ()
3238 if err != nil {
39+ c .logger .Error ("failed to execute root command" , "error" , err )
3340 os .Exit (1 )
3441 }
3542}
@@ -67,77 +74,82 @@ func (c *cmd) hooksCmd() *cobra.Command {
6774 },
6875 })
6976
70- hooksCmd . AddCommand ( & cobra.Command {
77+ configCmd := & cobra.Command {
7178 Use : "config" ,
7279 Short : "Print hooks configs" ,
7380 Long : `Print list of hooks configs in json format` ,
74- RunE : func (_ * cobra.Command , _ []string ) error {
81+ Run : func (_ * cobra.Command , _ []string ) {
7582 err := c .controller .PrintHookConfigs ()
7683 if err != nil {
77- return fmt .Errorf ("can not print configs: %w" , err )
84+ c .logger .Error ("can not print configs" , "error" , err )
85+ os .Exit (1 )
7886 }
79-
80- return nil
8187 },
82- })
88+ }
89+ hooksCmd .AddCommand (configCmd )
8390
84- hooksCmd . AddCommand ( & cobra.Command {
91+ dumpCmd := & cobra.Command {
8592 Use : "dump" ,
8693 Short : "Dump hooks configs" ,
8794 Long : `Dump list of hooks configs in config.json file` ,
8895 Hidden : true ,
89- RunE : func (_ * cobra.Command , _ []string ) error {
96+ Run : func (_ * cobra.Command , _ []string ) {
9097 err := c .controller .WriteHookConfigsInFile ()
9198 if err != nil {
92- return fmt .Errorf ("can not write configs to file: %w" , err )
99+ c .logger .Error ("can not write configs to file" , "error" , err )
100+ os .Exit (1 )
93101 }
94102
95103 fmt .Println ("dump successfully" )
96-
97- return nil
98104 },
99- })
105+ }
106+ hooksCmd .AddCommand (dumpCmd )
100107
101- hooksCmd . AddCommand ( & cobra.Command {
108+ runCmd := & cobra.Command {
102109 Use : "run" ,
103110 Short : "Running hook" ,
104111 Long : `Run hook from binary registry` ,
105112 Hidden : true ,
106- Args : cobra .ExactArgs (1 ),
107- RunE : func (cmd * cobra.Command , args []string ) error {
113+ Args : func (cmd * cobra.Command , args []string ) error {
114+ if len (args ) != 1 {
115+ c .logger .Error ("invalid number of arguments" , "expected" , 1 , "received" , len (args ))
116+ os .Exit (1 )
117+ }
118+ return nil
119+ },
120+ Run : func (cmd * cobra.Command , args []string ) {
108121 ctx := cmd .Context ()
109122
110123 idxRaw := args [0 ]
111124 idx , err := strconv .Atoi (idxRaw )
112125 if err != nil {
113- return fmt .Errorf ("argument '%s' is not integer" , idxRaw )
126+ c .logger .Error ("invalid argument" , "argument" , idxRaw , "error" , err )
127+ os .Exit (1 )
114128 }
115129
116130 err = c .controller .RunHook (ctx , idx )
117131 if err != nil {
118- return fmt . Errorf ( "run hook error: %w " , err )
132+ c . logger . Warn ( " hook shutdown" , "error " , err )
119133 }
120-
121- return nil
122134 },
123- })
135+ }
136+ hooksCmd .AddCommand (runCmd )
124137
125- hooksCmd . AddCommand ( & cobra.Command {
138+ readyCmd := & cobra.Command {
126139 Use : "ready" ,
127140 Short : "Check readiness" ,
128141 Long : `Run readiness hook for module` ,
129142 Hidden : true ,
130- RunE : func (cmd * cobra.Command , _ []string ) error {
143+ Run : func (cmd * cobra.Command , _ []string ) {
131144 ctx := cmd .Context ()
132145
133146 err := c .controller .RunReadiness (ctx )
134147 if err != nil {
135- return fmt . Errorf ( "run readiness hook error: %w " , err )
148+ c . logger . Warn ( " readiness hook shutdown" , "error " , err )
136149 }
137-
138- return nil
139150 },
140- })
151+ }
152+ hooksCmd .AddCommand (readyCmd )
141153
142154 return hooksCmd
143155}
0 commit comments