@@ -17,6 +17,7 @@ import (
1717 "github.com/spf13/cobra"
1818 "github.com/spf13/viper"
1919 "github.com/supabase/cli/internal/debug"
20+ "github.com/supabase/cli/internal/telemetry"
2021 "github.com/supabase/cli/internal/utils"
2122 "github.com/supabase/cli/internal/utils/flags"
2223 "golang.org/x/mod/semver"
@@ -122,6 +123,24 @@ var (
122123 fmt .Fprintln (os .Stderr , cmd .Root ().Short )
123124 fmt .Fprintf (os .Stderr , "Using profile: %s (%s)\n " , utils .CurrentProfile .Name , utils .CurrentProfile .ProjectHost )
124125 }
126+ isTTY := telemetryIsTTY ()
127+ isCI := telemetryIsCI ()
128+ isAgent := telemetryIsAgent ()
129+ envSignals := telemetryEnvSignals ()
130+ service , err := telemetry .NewService (fsys , telemetry.Options {
131+ Now : time .Now ,
132+ IsTTY : isTTY ,
133+ IsCI : isCI ,
134+ IsAgent : isAgent ,
135+ EnvSignals : envSignals ,
136+ CLIName : utils .Version ,
137+ })
138+ if err != nil {
139+ fmt .Fprintln (utils .GetDebugLogger (), err )
140+ } else {
141+ ctx = telemetry .WithService (ctx , service )
142+ }
143+ ctx = telemetry .WithCommandContext (ctx , commandAnalyticsContext (cmd ))
125144 cmd .SetContext (ctx )
126145 // Setup sentry last to ignore errors from parsing cli flags
127146 apiHost , err := url .Parse (utils .GetSupabaseAPIHost ())
@@ -137,11 +156,26 @@ var (
137156
138157func Execute () {
139158 defer recoverAndExit ()
140- if err := rootCmd .Execute (); err != nil {
159+ startedAt := time .Now ()
160+ executedCmd , err := rootCmd .ExecuteC ()
161+ if executedCmd != nil {
162+ if service := telemetry .FromContext (executedCmd .Context ()); service != nil {
163+ _ = service .Capture (executedCmd .Context (), telemetry .EventCommandExecuted , map [string ]any {
164+ telemetry .PropExitCode : exitCode (err ),
165+ telemetry .PropDurationMs : time .Since (startedAt ).Milliseconds (),
166+ }, nil )
167+ _ = service .Close ()
168+ }
169+ }
170+ if err != nil {
141171 panic (err )
142172 }
143173 // Check upgrade last because --version flag is initialised after execute
144- version , err := checkUpgrade (rootCmd .Context (), afero .NewOsFs ())
174+ ctx := rootCmd .Context ()
175+ if executedCmd != nil {
176+ ctx = executedCmd .Context ()
177+ }
178+ version , err := checkUpgrade (ctx , afero .NewOsFs ())
145179 if err != nil {
146180 fmt .Fprintln (utils .GetDebugLogger (), err )
147181 }
@@ -153,6 +187,13 @@ func Execute() {
153187 }
154188}
155189
190+ func exitCode (err error ) int {
191+ if err != nil {
192+ return 1
193+ }
194+ return 0
195+ }
196+
156197func checkUpgrade (ctx context.Context , fsys afero.Fs ) (string , error ) {
157198 if shouldFetchRelease (fsys ) {
158199 version , err := utils .GetLatestRelease (ctx )
0 commit comments