@@ -4,24 +4,31 @@ import (
44 "context"
55 "fmt"
66 "os"
7+ "strings"
78
89 "github.com/charmbracelet/fang"
10+ "github.com/duneanalytics/cli/authconfig"
11+ "github.com/duneanalytics/cli/cmd/auth"
12+ "github.com/duneanalytics/cli/cmd/execution"
913 "github.com/duneanalytics/cli/cmd/query"
14+ "github.com/duneanalytics/cli/cmdutil"
1015 "github.com/duneanalytics/duneapi-client-go/config"
1116 "github.com/duneanalytics/duneapi-client-go/dune"
1217 "github.com/spf13/cobra"
1318)
1419
15- type clientKey struct {}
16-
1720var apiKeyFlag string
1821
1922var rootCmd = & cobra.Command {
2023 Use : "dune" ,
2124 Short : "Dune CLI — interact with the Dune Analytics API" ,
2225 Long : "A command-line interface for interacting with the Dune Analytics API.\n " +
2326 "Manage queries, execute them, and retrieve results." ,
24- PersistentPreRunE : func (cmd * cobra.Command , args []string ) error {
27+ PersistentPreRunE : func (cmd * cobra.Command , _ []string ) error {
28+ if cmd .Annotations ["skipAuth" ] == "true" {
29+ return nil
30+ }
31+
2532 var env * config.Env
2633
2734 switch {
@@ -31,28 +38,39 @@ var rootCmd = &cobra.Command{
3138 var err error
3239 env , err = config .FromEnvVars ()
3340 if err != nil {
34- return fmt .Errorf ("missing API key: set DUNE_API_KEY or pass --api-key" )
41+ cfg , cfgErr := authconfig .Load ()
42+ if cfgErr != nil {
43+ return fmt .Errorf ("reading auth config: %w" , cfgErr )
44+ }
45+ if cfg != nil {
46+ key := strings .TrimSpace (cfg .APIKey )
47+ if key == "" {
48+ return fmt .Errorf ("empty API key in config: run dune auth --api-key <key>" )
49+ }
50+ env = config .FromAPIKey (key )
51+ } else {
52+ return fmt .Errorf ("missing API key: set DUNE_API_KEY, pass --api-key, or run dune auth" )
53+ }
3554 }
3655 }
3756
3857 client := dune .NewDuneClient (env )
39- cmd . SetContext ( context . WithValue ( cmd . Context (), clientKey {}, dune . DuneClient ( client )) )
58+ cmdutil . SetClient ( cmd , client )
4059 return nil
4160 },
4261}
4362
4463func init () {
4564 rootCmd .PersistentFlags ().StringVar (& apiKeyFlag , "api-key" , "" , "Dune API key (overrides DUNE_API_KEY env var)" )
65+ rootCmd .AddCommand (auth .NewAuthCmd ())
4666 rootCmd .AddCommand (query .NewQueryCmd ())
47- }
48-
49- // ClientFromCmd extracts the DuneClient stored in the command's context.
50- func ClientFromCmd (cmd * cobra.Command ) dune.DuneClient {
51- return cmd .Context ().Value (clientKey {}).(dune.DuneClient )
67+ rootCmd .AddCommand (execution .NewExecutionCmd ())
5268}
5369
5470// Execute runs the root command via Fang.
55- func Execute () {
71+ func Execute (version , commit , date string ) {
72+ rootCmd .Version = fmt .Sprintf ("%s (commit: %s, built: %s)" , version , commit , date )
73+
5674 if err := fang .Execute (context .Background (), rootCmd ); err != nil {
5775 fmt .Fprintln (os .Stderr , err )
5876 os .Exit (1 )
0 commit comments