Skip to content

Commit

Permalink
last changes
Browse files Browse the repository at this point in the history
  • Loading branch information
JulesFaucherre committed Aug 1, 2023
1 parent eaf3f6b commit f6d26cc
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 20 deletions.
21 changes: 7 additions & 14 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,20 @@ var rootTokenFromFlag string
// Execute adds all child commands to rootCmd, sets the flags appropriately
// and put the telemetry client in the command context. This function is
// called by main.main(). It only needs to happen once to the rootCmd
func Execute() {
func Execute() error {
header.SetCommandStr(CommandStr())
command := MakeCommands()

// If we have no context, we can't add the telemetry to the context
if command.Context() == nil {
command.SetContext(context.Background())
}

telemetryClient := CreateTelemetry(rootOptions)
// We defer to close the telemetry in case of panic
defer telemetryClient.Close()
command.SetContext(telemetry.NewContext(command.Context(), telemetryClient))

err := command.Execute()
// We close here because defer is not called when `os.Exit` is called
telemetryClient.Close()

if err != nil {
os.Exit(-1)
cmdContext := command.Context()
if cmdContext == nil {
cmdContext = context.Background()
}
command.SetContext(telemetry.NewContext(cmdContext, telemetryClient))

return command.Execute()
}

// Returns a string (e.g. "circleci context list") indicating what
Expand Down
4 changes: 3 additions & 1 deletion cmd/runner/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ func newRunnerInstanceCommand(o *runnerOpts, preRunE validator.Validator) *cobra
telemetryClient, ok := telemetry.FromContext(cmd.Context())
if ok {
// We defer the call to be sure the `err` has been filled
defer telemetryClient.Track(telemetry.CreateRunnerInstanceEvent(telemetry.GetCommandInformation(cmd, true), err))
defer (func() {
_ = telemetryClient.Track(telemetry.CreateRunnerInstanceEvent(telemetry.GetCommandInformation(cmd, true), err))
})()
}

runners, err := o.r.GetRunnerInstances(args[0])
Expand Down
2 changes: 1 addition & 1 deletion cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func newSetupCommand(config *settings.Config) *cobra.Command {
telemetryClient, ok := telemetry.FromContext(cmd.Context())
if ok {
// We defer the call to make sure the `opts.cfg.Host` has been filled
defer telemetryClient.Track(telemetry.CreateSetupEvent(opts.cfg.Host != defaultHost))
defer (func() { _ = telemetryClient.Track(telemetry.CreateSetupEvent(opts.cfg.Host != defaultHost)) })()
}

if opts.integrationTesting {
Expand Down
2 changes: 1 addition & 1 deletion cmd/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var _ = Describe("Setup telemetry", func() {

Eventually(session).Should(gexec.Exit(0))
clitest.CompareTelemetryEvent(tempSettings, []telemetry.Event{
telemetry.CreateSetupEvent(false),
telemetry.CreateSetupEvent(true),
})
})
})
Expand Down
2 changes: 0 additions & 2 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ func newVersionCommand(config *settings.Config) *cobra.Command {
version := fmt.Sprintf("%s+%s (%s)", version.Version, version.Commit, version.PackageManager())

telemetryClient, ok := telemetry.FromContext(cmd.Context())
fmt.Printf("telemetryClient = %+v\n", telemetryClient)
fmt.Printf("ok = %+v\n", ok)
if ok {
_ = telemetryClient.Track(telemetry.CreateVersionEvent(version))
}
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package main

import (
"os"

"github.com/CircleCI-Public/circleci-cli/cmd"
)

func main() {
// See cmd/root.go for Execute()
cmd.Execute()
if err := cmd.Execute(); err != nil {
os.Exit(-1)
}
}

0 comments on commit f6d26cc

Please sign in to comment.