@@ -102,17 +102,21 @@ func isContextAgnosticCommand(cmd *cobra.Command) bool {
102
102
func main () {
103
103
var opts cliopts.GlobalOpts
104
104
root := & cobra.Command {
105
- Use : "docker" ,
106
- SilenceErrors : true ,
107
- SilenceUsage : true ,
105
+ Use : "docker" ,
106
+ SilenceErrors : true ,
107
+ SilenceUsage : true ,
108
+ TraverseChildren : true ,
108
109
PersistentPreRunE : func (cmd * cobra.Command , args []string ) error {
109
110
if ! isContextAgnosticCommand (cmd ) {
110
111
mobycli .ExecIfDefaultCtxType (cmd .Context (), cmd .Root ())
111
112
}
112
113
return nil
113
114
},
114
115
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 ])
116
120
},
117
121
}
118
122
@@ -146,20 +150,20 @@ func main() {
146
150
helpFunc (cmd , args )
147
151
})
148
152
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" )
155
160
156
161
walk (root , func (c * cobra.Command ) {
157
162
c .Flags ().BoolP ("help" , "h" , false , "Help for " + c .Name ())
158
163
})
159
164
160
165
// 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
163
167
164
168
level , err := logrus .ParseLevel (opts .LogLevel )
165
169
if err != nil {
@@ -208,28 +212,32 @@ func main() {
208
212
ctx = store .WithContextStore (ctx , s )
209
213
210
214
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
+ }
227
219
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
+ }
229
230
231
+ // Context should always be handled by new CLI
232
+ requiredCmd , _ , _ := root .Find (os .Args [1 :])
233
+ if requiredCmd != nil && isContextAgnosticCommand (requiredCmd ) {
230
234
exit (currentContext , err , ctype )
231
235
}
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 )
233
241
}
234
242
235
243
func exit (ctx string , err error , ctype string ) {
0 commit comments