@@ -102,17 +102,21 @@ func isContextAgnosticCommand(cmd *cobra.Command) bool {
102102func main () {
103103 var opts cliopts.GlobalOpts
104104 root := & cobra.Command {
105- Use : "docker" ,
106- SilenceErrors : true ,
107- SilenceUsage : true ,
105+ Use : "docker" ,
106+ SilenceErrors : true ,
107+ SilenceUsage : true ,
108+ TraverseChildren : true ,
108109 PersistentPreRunE : func (cmd * cobra.Command , args []string ) error {
109110 if ! isContextAgnosticCommand (cmd ) {
110111 mobycli .ExecIfDefaultCtxType (cmd .Context (), cmd .Root ())
111112 }
112113 return nil
113114 },
114115 RunE : func (cmd * cobra.Command , args []string ) error {
115- return cmd .Help ()
116+ if len (args ) == 0 {
117+ return cmd .Help ()
118+ }
119+ return fmt .Errorf ("unknown command %q" , args [0 ])
116120 },
117121 }
118122
@@ -146,20 +150,20 @@ func main() {
146150 helpFunc (cmd , args )
147151 })
148152
149- root .PersistentFlags ().StringVarP (& opts .LogLevel , "log-level" , "l" , "info" , "Set the logging level (\" debug\" |\" info\" |\" warn\" |\" error\" |\" fatal\" )" )
150- root .PersistentFlags ().BoolVarP (& opts .Debug , "debug" , "D" , false , "Enable debug output in the logs" )
151- root .PersistentFlags ().StringVarP (& opts .Host , "host" , "H" , "" , "Daemon socket(s) to connect to" )
152- opts .AddContextFlags (root .PersistentFlags ())
153- opts .AddConfigFlags (root .PersistentFlags ())
154- root .Flags ().BoolVarP (& opts .Version , "version" , "v" , false , "Print version information and quit" )
153+ flags := root .Flags ()
154+ flags .StringVarP (& opts .LogLevel , "log-level" , "l" , "info" , "Set the logging level (\" debug\" |\" info\" |\" warn\" |\" error\" |\" fatal\" )" )
155+ flags .BoolVarP (& opts .Debug , "debug" , "D" , false , "Enable debug output in the logs" )
156+ flags .StringVarP (& opts .Host , "host" , "H" , "" , "Daemon socket(s) to connect to" )
157+ opts .AddContextFlags (flags )
158+ opts .AddConfigFlags (flags )
159+ flags .BoolVarP (& opts .Version , "version" , "v" , false , "Print version information and quit" )
155160
156161 walk (root , func (c * cobra.Command ) {
157162 c .Flags ().BoolP ("help" , "h" , false , "Help for " + c .Name ())
158163 })
159164
160165 // populate the opts with the global flags
161- _ = root .PersistentFlags ().Parse (os .Args [1 :])
162- _ = root .Flags ().Parse (os .Args [1 :])
166+ flags .Parse (os .Args [1 :]) //nolint: errcheck
163167
164168 level , err := logrus .ParseLevel (opts .LogLevel )
165169 if err != nil {
@@ -208,28 +212,32 @@ func main() {
208212 ctx = store .WithContextStore (ctx , s )
209213
210214 if err = root .ExecuteContext (ctx ); err != nil {
211- // if user canceled request, simply exit without any error message
212- if errdefs .IsErrCanceled (err ) || errors .Is (ctx .Err (), context .Canceled ) {
213- metrics .Track (ctype , os .Args [1 :], metrics .CanceledStatus )
214- os .Exit (130 )
215- }
216- if ctype == store .AwsContextType {
217- exit (currentContext , errors .Errorf (`%q context type has been renamed. Recreate the context by running:
218- $ docker context create %s <name>` , cc .Type (), store .EcsContextType ), ctype )
219- }
220-
221- // Context should always be handled by new CLI
222- requiredCmd , _ , _ := root .Find (os .Args [1 :])
223- if requiredCmd != nil && isContextAgnosticCommand (requiredCmd ) {
224- exit (currentContext , err , ctype )
225- }
226- mobycli .ExecIfDefaultCtxType (ctx , root )
215+ handleError (ctx , err , ctype , currentContext , cc , root )
216+ }
217+ metrics .Track (ctype , os .Args [1 :], metrics .SuccessStatus )
218+ }
227219
228- checkIfUnknownCommandExistInDefaultContext (err , currentContext , ctype )
220+ func handleError (ctx context.Context , err error , ctype string , currentContext string , cc * store.DockerContext , root * cobra.Command ) {
221+ // if user canceled request, simply exit without any error message
222+ if errdefs .IsErrCanceled (err ) || errors .Is (ctx .Err (), context .Canceled ) {
223+ metrics .Track (ctype , os .Args [1 :], metrics .CanceledStatus )
224+ os .Exit (130 )
225+ }
226+ if ctype == store .AwsContextType {
227+ exit (currentContext , errors .Errorf (`%q context type has been renamed. Recreate the context by running:
228+ $ docker context create %s <name>` , cc .Type (), store .EcsContextType ), ctype )
229+ }
229230
231+ // Context should always be handled by new CLI
232+ requiredCmd , _ , _ := root .Find (os .Args [1 :])
233+ if requiredCmd != nil && isContextAgnosticCommand (requiredCmd ) {
230234 exit (currentContext , err , ctype )
231235 }
232- metrics .Track (ctype , os .Args [1 :], metrics .SuccessStatus )
236+ mobycli .ExecIfDefaultCtxType (ctx , root )
237+
238+ checkIfUnknownCommandExistInDefaultContext (err , currentContext , ctype )
239+
240+ exit (currentContext , err , ctype )
233241}
234242
235243func exit (ctx string , err error , ctype string ) {
0 commit comments