Skip to content

Commit

Permalink
Track all commands if logged in (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
Widcket authored Jun 14, 2021
1 parent da352f4 commit 1dc148e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
1 change: 0 additions & 1 deletion internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions internal/cli/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
20 changes: 13 additions & 7 deletions internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func Execute() {
cli := &cli{
renderer: display.NewRenderer(),
tracker: analytics.NewTracker(),
context: cliContext(),
}

rootCmd := &cobra.Command{
Expand All @@ -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" {
Expand All @@ -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.
Expand Down Expand Up @@ -142,21 +147,22 @@ 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())

instrumentation.ReportException(err)
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)
Expand Down

0 comments on commit 1dc148e

Please sign in to comment.