@@ -61,6 +61,7 @@ import (
6161)
6262
6363var (
64+ metricsClient metrics.Client
6465 contextAgnosticCommands = map [string ]struct {}{
6566 "context" : {},
6667 "login" : {},
@@ -86,6 +87,13 @@ func init() {
8687 if err := os .Setenv ("PATH" , appendPaths (os .Getenv ("PATH" ), path )); err != nil {
8788 panic (err )
8889 }
90+
91+ // init metrics client
92+ metricsClient = metrics .NewClient ()
93+ metricsClient .WithCliVersionFunc (func () string {
94+ return mobycli .CliVersion ()
95+ })
96+
8997 // Seed random
9098 rand .Seed (time .Now ().UnixNano ())
9199}
@@ -249,7 +257,7 @@ func main() {
249257 if err = root .ExecuteContext (ctx ); err != nil {
250258 handleError (ctx , err , ctype , currentContext , cc , root )
251259 }
252- metrics .Track (ctype , os .Args [1 :], compose .SuccessStatus )
260+ metricsClient .Track (ctype , os .Args [1 :], compose .SuccessStatus )
253261}
254262
255263func customizeCliForACI (command * cobra.Command , proxy * api.ServiceProxy ) {
@@ -271,7 +279,7 @@ func customizeCliForACI(command *cobra.Command, proxy *api.ServiceProxy) {
271279func handleError (ctx context.Context , err error , ctype string , currentContext string , cc * store.DockerContext , root * cobra.Command ) {
272280 // if user canceled request, simply exit without any error message
273281 if api .IsErrCanceled (err ) || errors .Is (ctx .Err (), context .Canceled ) {
274- metrics .Track (ctype , os .Args [1 :], compose .CanceledStatus )
282+ metricsClient .Track (ctype , os .Args [1 :], compose .CanceledStatus )
275283 os .Exit (130 )
276284 }
277285 if ctype == store .AwsContextType {
@@ -293,7 +301,7 @@ $ docker context create %s <name>`, cc.Type(), store.EcsContextType), ctype)
293301
294302func exit (ctx string , err error , ctype string ) {
295303 if exit , ok := err .(cli.StatusError ); ok {
296- metrics .Track (ctype , os .Args [1 :], compose .SuccessStatus )
304+ metricsClient .Track (ctype , os .Args [1 :], compose .SuccessStatus )
297305 os .Exit (exit .StatusCode )
298306 }
299307
@@ -308,7 +316,7 @@ func exit(ctx string, err error, ctype string) {
308316 metricsStatus = compose .CommandSyntaxFailure .MetricsStatus
309317 exitCode = compose .CommandSyntaxFailure .ExitCode
310318 }
311- metrics .Track (ctype , os .Args [1 :], metricsStatus )
319+ metricsClient .Track (ctype , os .Args [1 :], metricsStatus )
312320
313321 if errors .Is (err , api .ErrLoginRequired ) {
314322 fmt .Fprintln (os .Stderr , err )
@@ -343,7 +351,7 @@ func checkIfUnknownCommandExistInDefaultContext(err error, currentContext string
343351
344352 if mobycli .IsDefaultContextCommand (dockerCommand ) {
345353 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 )
346- metrics .Track (contextType , os .Args [1 :], compose .FailureStatus )
354+ metricsClient .Track (contextType , os .Args [1 :], compose .FailureStatus )
347355 os .Exit (1 )
348356 }
349357 }
0 commit comments