Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track all commands if the user is logged in [CLI-178] #312

Merged
merged 1 commit into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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