Skip to content
34 changes: 32 additions & 2 deletions commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"regexp"
"slices"
"strings"
"time"

"github.com/fatih/color"
"github.com/platformsh/platformify/commands"
Expand All @@ -22,6 +23,7 @@ import (
"github.com/upsun/cli/internal/config"
"github.com/upsun/cli/internal/config/alt"
"github.com/upsun/cli/internal/legacy"
"github.com/upsun/cli/internal/telemetry"
)

// Execute is the main entrypoint to run the CLI.
Expand Down Expand Up @@ -88,11 +90,23 @@ func newRootCommand(cnf *config.Config, assets *vendorization.VendorAssets) *cob
},
Run: func(cmd *cobra.Command, _ []string) {
c := makeLegacyCLIWrapper(cnf, cmd.OutOrStdout(), cmd.ErrOrStderr(), cmd.InOrStdin())
if err := c.Exec(cmd.Context(), os.Args[1:]...); err != nil {
err := c.Exec(cmd.Context(), os.Args[1:]...)

if err != nil {
exitWithError(err)
}

waitForTelemetry(cmd.Context(), cnf, os.Args[1:])
},
PersistentPostRun: func(cmd *cobra.Command, _ []string) {
// Send telemetry for Go-native subcommands only
// Legacy commands send telemetry in Run before exitWithError
// cmd.Parent() != nil means this is a subcommand, not the root
if cmd.Parent() != nil {
telemetry.SendTelemetryEvent(cmd.Context(), cnf, cmd.Use)
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Cobra commands, cmd.Use can include placeholders like "init [flags]" (e.g. init is defined as Use: "init [flags]"), which won't match the exact strings in IsTracked(). As a result, telemetry won't be sent for tracked Go-native commands. Use cmd.Name() (or cmd.CommandPath() if you want full path) instead of cmd.Use when reporting the command identifier.

Suggested change
telemetry.SendTelemetryEvent(cmd.Context(), cnf, cmd.Use)
telemetry.SendTelemetryEvent(cmd.Context(), cnf, cmd.CommandPath())

Copilot uses AI. Check for mistakes.
// Fire and forget for native commands
}

checkShellConfigLeftovers(cmd.ErrOrStderr(), cnf)
select {
case rel := <-updateMessageChan:
Expand All @@ -118,9 +132,13 @@ func newRootCommand(cnf *config.Config, assets *vendorization.VendorAssets) *cob
}

c := makeLegacyCLIWrapper(cnf, cmd.OutOrStdout(), cmd.ErrOrStderr(), cmd.InOrStdin())
if err := c.Exec(cmd.Context(), args...); err != nil {
err := c.Exec(cmd.Context(), args...)

if err != nil {
exitWithError(err)
}

// Don't send telemetry for help commands
})

cmd.PersistentFlags().BoolP("version", "V", false, fmt.Sprintf("Displays the %s version", cnf.Application.Name))
Expand Down Expand Up @@ -276,3 +294,15 @@ func makeLegacyCLIWrapper(cnf *config.Config, stdout, stderr io.Writer, stdin io
Stdin: stdin,
}
}

func waitForTelemetry(ctx context.Context, cnf *config.Config, args []string) {
command := telemetry.ExtractCommand(args)
done := telemetry.SendTelemetryEvent(ctx, cnf, command)

select {
case <-done:
// Telemetry completed
case <-time.After(2 * time.Second):
// Timeout - proceed anyway to avoid blocking user
}
Comment on lines +298 to +307
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

waitForTelemetry() derives the command from raw os.Args[1:], but ExtractCommand() currently returns args[0] even if it's a flag (e.g. --no-interaction project:list). That will cause telemetry to be skipped for tracked legacy commands invoked with global flags. Consider passing Cobra-parsed args, or updating ExtractCommand() to skip leading flags and return the first non-flag token.

Copilot uses AI. Check for mistakes.
}
Loading
Loading