@@ -5,16 +5,19 @@ import (
55 "fmt"
66 "os"
77 "strings"
8+ "time"
89
910 "github.com/charmbracelet/fang"
1011 "github.com/duneanalytics/cli/authconfig"
1112 "github.com/duneanalytics/cli/cmd/auth"
13+ duneconfig "github.com/duneanalytics/cli/cmd/config"
1214 "github.com/duneanalytics/cli/cmd/dataset"
1315 "github.com/duneanalytics/cli/cmd/docs"
1416 "github.com/duneanalytics/cli/cmd/execution"
1517 "github.com/duneanalytics/cli/cmd/query"
1618 "github.com/duneanalytics/cli/cmd/usage"
1719 "github.com/duneanalytics/cli/cmdutil"
20+ "github.com/duneanalytics/cli/tracking"
1821 "github.com/duneanalytics/duneapi-client-go/config"
1922 "github.com/duneanalytics/duneapi-client-go/dune"
2023 "github.com/spf13/cobra"
@@ -37,6 +40,8 @@ var rootCmd = &cobra.Command{
3740 "Authenticate with an API key via --api-key, the DUNE_API_KEY environment variable,\n " +
3841 "or by running `dune auth`." ,
3942 PersistentPreRunE : func (cmd * cobra.Command , _ []string ) error {
43+ cmdutil .SetStartTime (cmd , time .Now ())
44+
4045 if cmd .Annotations ["skipAuth" ] == "true" {
4146 return nil
4247 }
@@ -68,13 +73,32 @@ var rootCmd = &cobra.Command{
6873
6974 client := dune .NewDuneClient (env )
7075 cmdutil .SetClient (cmd , client )
76+
77+ return nil
78+ },
79+ PersistentPostRunE : func (cmd * cobra.Command , _ []string ) error {
80+ tr := cmdutil .TrackerFromCmd (cmd )
81+ if tr == nil {
82+ return nil
83+ }
84+ start := cmdutil .StartTimeFromCmd (cmd )
85+ durationMs := time .Since (start ).Milliseconds ()
86+
87+ commandPath := cmd .CommandPath ()
88+ // Strip the root command name for cleaner paths.
89+ if parts := strings .SplitN (commandPath , " " , 2 ); len (parts ) == 2 {
90+ commandPath = parts [1 ]
91+ }
92+
93+ tr .Track (commandPath , tracking .StatusSuccess , "" , durationMs )
7194 return nil
7295 },
7396}
7497
7598func init () {
7699 rootCmd .PersistentFlags ().StringVar (& apiKeyFlag , "api-key" , "" , "Dune API key (overrides DUNE_API_KEY env var)" )
77100 rootCmd .AddCommand (auth .NewAuthCmd ())
101+ rootCmd .AddCommand (duneconfig .NewConfigCmd ())
78102 rootCmd .AddCommand (dataset .NewDatasetCmd ())
79103 rootCmd .AddCommand (docs .NewDocsCmd ())
80104 rootCmd .AddCommand (query .NewQueryCmd ())
@@ -83,12 +107,27 @@ func init() {
83107}
84108
85109// Execute runs the root command via Fang.
86- func Execute (version , commit , date string ) {
110+ func Execute (version , commit , date , amplitudeKey string ) {
87111 versionStr := fmt .Sprintf ("%s (commit: %s, built: %s)" , version , commit , date )
88112
89- if err := fang .Execute (context .Background (), rootCmd ,
113+ telemetryEnabled := duneconfig .IsTelemetryEnabled ()
114+ configDir , _ := authconfig .Dir ()
115+ tracker := tracking .New (tracking.Config {
116+ AmplitudeKey : amplitudeKey ,
117+ CLIVersion : version ,
118+ ConfigDir : configDir ,
119+ Enabled : telemetryEnabled ,
120+ })
121+ defer tracker .Shutdown ()
122+
123+ rootCmd .SetContext (context .Background ())
124+ cmdutil .SetTracker (rootCmd , tracker )
125+
126+ if err := fang .Execute (rootCmd .Context (), rootCmd ,
90127 fang .WithVersion (versionStr ),
91128 ); err != nil {
129+ tracker .Track ("unknown" , tracking .StatusError , err .Error (), 0 )
130+
92131 fmt .Fprintln (os .Stderr , err )
93132 os .Exit (1 )
94133 }
0 commit comments