| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "github.com/spf13/cobra" |
| 7 | "sourcecraft.dev/bigbes/claudio/cache" |
| 8 | "sourcecraft.dev/bigbes/claudio/config" |
| 9 | "sourcecraft.dev/bigbes/claudio/provider/copilot" |
| 10 | ) |
| 11 | |
| 12 | var configCmd = &cobra.Command{ |
| 13 | Use: "config", |
| 14 | Aliases: []string{"configure"}, |
| 15 | Short: "Open the configuration screen", |
| 16 | RunE: runConfig, |
| 17 | } |
| 18 | |
| 19 | func init() { |
| 20 | rootCmd.AddCommand(configCmd) |
| 21 | } |
| 22 | |
| 23 | func runConfig(cmd *cobra.Command, args []string) error { |
| 24 | cfg, err := config.Load() |
| 25 | if err != nil { |
| 26 | return fmt.Errorf("loading config: %w", err) |
| 27 | } |
| 28 | |
| 29 | cdb, _ := cache.Open() |
| 30 | if cdb != nil { |
| 31 | defer cdb.Close() |
| 32 | } |
| 33 | |
| 34 | quit, launch := showTUI(cfg, cdb) |
| 35 | if quit { |
| 36 | return nil |
| 37 | } |
| 38 | if err := config.Save(cfg); err != nil { |
| 39 | return fmt.Errorf("saving config: %w", err) |
| 40 | } |
| 41 | // Copilot needs OAuth before we can use it. |
| 42 | if cfg.ActiveProvider == "copilot" { |
| 43 | pc := cfg.Providers["copilot"] |
| 44 | if pc.APIKey == "" { |
| 45 | fmt.Println("Authenticating with GitHub Copilot...") |
| 46 | token, err := copilot.DeviceAuth() |
| 47 | if err != nil { |
| 48 | return fmt.Errorf("copilot auth: %w", err) |
| 49 | } |
| 50 | pc.APIKey = token |
| 51 | cfg.Providers["copilot"] = pc |
| 52 | if err := config.Save(cfg); err != nil { |
| 53 | return fmt.Errorf("saving config: %w", err) |
| 54 | } |
| 55 | // After fresh auth, show model picker. |
| 56 | if err := showCopilotModelPicker(cfg, cdb); err != nil { |
| 57 | return err |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | if !launch { |
| 62 | return nil |
| 63 | } |
| 64 | // Continue to launch Claude Code via root command logic. |
| 65 | return runRoot(cmd.Root(), args) |
| 66 | } |
| 67 | |