@@ -102,7 +102,7 @@ func main() {
102
102
SilenceUsage : true ,
103
103
PersistentPreRunE : func (cmd * cobra.Command , args []string ) error {
104
104
if ! isContextAgnosticCommand (cmd ) {
105
- mobycli .ExecIfDefaultCtxType (cmd .Context ())
105
+ mobycli .ExecIfDefaultCtxType (cmd .Context (), cmd . Root () )
106
106
}
107
107
return nil
108
108
},
@@ -136,7 +136,7 @@ func main() {
136
136
helpFunc := root .HelpFunc ()
137
137
root .SetHelpFunc (func (cmd * cobra.Command , args []string ) {
138
138
if ! isContextAgnosticCommand (cmd ) {
139
- mobycli .ExecIfDefaultCtxType (cmd .Context ())
139
+ mobycli .ExecIfDefaultCtxType (cmd .Context (), cmd . Root () )
140
140
}
141
141
helpFunc (cmd , args )
142
142
})
@@ -158,7 +158,7 @@ func main() {
158
158
159
159
// --host and --version should immediately be forwarded to the original cli
160
160
if opts .Host != "" || opts .Version {
161
- mobycli .Exec ()
161
+ mobycli .Exec (root )
162
162
}
163
163
164
164
if opts .Config == "" {
@@ -171,7 +171,7 @@ func main() {
171
171
172
172
s , err := store .New (configDir )
173
173
if err != nil {
174
- mobycli .Exec ()
174
+ mobycli .Exec (root )
175
175
}
176
176
177
177
ctype := store .DefaultContextType
@@ -185,41 +185,43 @@ func main() {
185
185
root .AddCommand (volume .ACICommand ())
186
186
}
187
187
188
- metrics .Track (ctype , os .Args [1 :], root .PersistentFlags ())
189
-
190
188
ctx = apicontext .WithCurrentContext (ctx , currentContext )
191
189
ctx = store .WithContextStore (ctx , s )
192
190
193
191
if err = root .ExecuteContext (ctx ); err != nil {
194
192
// if user canceled request, simply exit without any error message
195
- if errors .Is (ctx .Err (), context .Canceled ) {
193
+ if errdefs .IsErrCanceled (err ) || errors .Is (ctx .Err (), context .Canceled ) {
194
+ metrics .Track (ctype , os .Args [1 :], root .PersistentFlags (), metrics .CanceledStatus )
196
195
os .Exit (130 )
197
196
}
198
197
if ctype == store .AwsContextType {
199
198
exit (root , currentContext , errors .Errorf (`%q context type has been renamed. Recreate the context by running:
200
- $ docker context create %s <name>` , cc .Type (), store .EcsContextType ))
199
+ $ docker context create %s <name>` , cc .Type (), store .EcsContextType ), ctype )
201
200
}
202
201
203
202
// Context should always be handled by new CLI
204
203
requiredCmd , _ , _ := root .Find (os .Args [1 :])
205
204
if requiredCmd != nil && isContextAgnosticCommand (requiredCmd ) {
206
- exit (root , currentContext , err )
205
+ exit (root , currentContext , err , ctype )
207
206
}
208
- mobycli .ExecIfDefaultCtxType (ctx )
207
+ mobycli .ExecIfDefaultCtxType (ctx , root )
209
208
210
- checkIfUnknownCommandExistInDefaultContext (err , currentContext )
209
+ checkIfUnknownCommandExistInDefaultContext (err , currentContext , root )
211
210
212
- exit (root , currentContext , err )
211
+ exit (root , currentContext , err , ctype )
213
212
}
213
+ metrics .Track (ctype , os .Args [1 :], root .PersistentFlags (), metrics .SuccessStatus )
214
214
}
215
215
216
- func exit (cmd * cobra.Command , ctx string , err error ) {
216
+ func exit (root * cobra.Command , ctx string , err error , ctype string ) {
217
+ metrics .Track (ctype , os .Args [1 :], root .PersistentFlags (), metrics .FailureStatus )
218
+
217
219
if errors .Is (err , errdefs .ErrLoginRequired ) {
218
220
fmt .Fprintln (os .Stderr , err )
219
221
os .Exit (errdefs .ExitCodeLoginRequired )
220
222
}
221
223
if errors .Is (err , errdefs .ErrNotImplemented ) {
222
- cmd , _ , _ := cmd .Traverse (os .Args [1 :])
224
+ cmd , _ , _ := root .Traverse (os .Args [1 :])
223
225
name := cmd .Name ()
224
226
parent := cmd .Parent ()
225
227
if parent != nil && parent .Parent () != nil {
@@ -237,13 +239,14 @@ func fatal(err error) {
237
239
os .Exit (1 )
238
240
}
239
241
240
- func checkIfUnknownCommandExistInDefaultContext (err error , currentContext string ) {
242
+ func checkIfUnknownCommandExistInDefaultContext (err error , currentContext string , root * cobra. Command ) {
241
243
submatch := unknownCommandRegexp .FindSubmatch ([]byte (err .Error ()))
242
244
if len (submatch ) == 2 {
243
245
dockerCommand := string (submatch [1 ])
244
246
245
247
if mobycli .IsDefaultContextCommand (dockerCommand ) {
246
248
fmt .Fprintf (os .Stderr , "Command %q not available in current context (%s), you can use the \" default\" context to run this command\n " , dockerCommand , currentContext )
249
+ metrics .Track (currentContext , os .Args [1 :], root .PersistentFlags (), metrics .FailureStatus )
247
250
os .Exit (1 )
248
251
}
249
252
}
0 commit comments