From 1dc148e1fc2ee4a23b9502a804249c08bfefcd2d Mon Sep 17 00:00:00 2001 From: Rita Zerrizuela Date: Mon, 14 Jun 2021 18:06:45 -0300 Subject: [PATCH] Track all commands if logged in (#312) --- internal/cli/cli.go | 1 - internal/cli/login.go | 3 +++ internal/cli/root.go | 20 +++++++++++++------- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/internal/cli/cli.go b/internal/cli/cli.go index 22bdc06fa..4600efe0d 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -76,7 +76,6 @@ type cli struct { api *auth0.API renderer *display.Renderer tracker *analytics.Tracker - context context.Context // set of flags which are user specified. debug bool tenant string diff --git a/internal/cli/login.go b/internal/cli/login.go index 95e62b040..4cb457e27 100644 --- a/internal/cli/login.go +++ b/internal/cli/login.go @@ -21,6 +21,9 @@ func loginCmd(cli *cli) *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() _, err := RunLogin(ctx, cli, false) + if err == nil { + cli.tracker.TrackCommandRun(cmd, cli.config.InstallID) + } return err }, } diff --git a/internal/cli/root.go b/internal/cli/root.go index b5199c0fb..80f2179bf 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -25,7 +25,6 @@ func Execute() { cli := &cli{ renderer: display.NewRenderer(), tracker: analytics.NewTracker(), - context: cliContext(), } rootCmd := &cobra.Command{ @@ -45,6 +44,14 @@ func Execute() { return nil } + // We're tracking the login command in its Run method + // so we'll only add this defer if the command is not login + defer func() { + if cli.isLoggedIn() { + cli.tracker.TrackCommandRun(cmd, cli.config.InstallID) + } + }() + // If the user is trying to logout, session information // isn't important as well. if cmd.Use == "logout" && cmd.Parent().Use == "auth0" { @@ -71,8 +78,6 @@ func Execute() { return nil } - defer cli.tracker.TrackCommandRun(cmd, cli.config.InstallID) - // Initialize everything once. Later callers can then // freely assume that config is fully primed and ready // to go. @@ -142,7 +147,8 @@ func Execute() { // for most of the architectures there's no requirements: ansi.InitConsole() - if err := rootCmd.ExecuteContext(cli.context); err != nil { + cancelCtx := contextWithCancel() + if err := rootCmd.ExecuteContext(cancelCtx); err != nil { cli.renderer.Heading("error") cli.renderer.Errorf(err.Error()) @@ -150,13 +156,13 @@ func Execute() { os.Exit(1) } - ctx, cancel := context.WithTimeout(cli.context, 3*time.Second) + timeoutCtx, cancel := context.WithTimeout(cancelCtx, 3*time.Second) // defers are executed in LIFO order defer cancel() - defer cli.tracker.Wait(ctx) // No event should be tracked after this has run, or it will panic e.g. in earlier deferred functions + defer cli.tracker.Wait(timeoutCtx) // No event should be tracked after this has run, or it will panic e.g. in earlier deferred functions } -func cliContext() context.Context { +func contextWithCancel() context.Context { ctx, cancel := context.WithCancel(context.Background()) ch := make(chan os.Signal, 1)