root.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package cli
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "github.com/spf13/cobra"
  7. "github.com/kotoyuuko/cc-switch-cli/internal/config"
  8. )
  9. // appState is threaded through every subcommand via cobra's persistent
  10. // pre-run. Subcommands read from app.cfg, mutate it in place, and call
  11. // app.save() to persist.
  12. type appState struct {
  13. // Streams. Tests substitute these; production uses os.Std*.
  14. stdin io.Reader
  15. stdout io.Writer
  16. stderr io.Writer
  17. // Flags.
  18. verbose bool
  19. configFlag string // --config; beats env var precedence
  20. // Loaded state.
  21. configPath string
  22. cfg config.Config
  23. // requestedExit lets subcommands like `use` hand back a specific exit
  24. // code (claude's own) that bypasses cobra's 0/1 mapping.
  25. requestedExit *int
  26. }
  27. func (a *appState) tracef(format string, args ...any) {
  28. if !a.verbose {
  29. return
  30. }
  31. fmt.Fprintf(a.stderr, "[cc-switch] "+format+"\n", args...)
  32. }
  33. // exitCode collapses (requestedExit, cobra err) into a single process exit
  34. // code. requestedExit takes precedence — subcommands use it to propagate
  35. // claude's own exit code even when we also returned an error for logging.
  36. func (a *appState) exitCode(err error) int {
  37. if a.requestedExit != nil {
  38. return *a.requestedExit
  39. }
  40. if err != nil {
  41. return 1
  42. }
  43. return 0
  44. }
  45. func (a *appState) save() error {
  46. return config.Save(a.configPath, a.cfg)
  47. }
  48. func newRootCmd(app *appState) *cobra.Command {
  49. root := &cobra.Command{
  50. Use: "cc-switch",
  51. Short: "Switch between Claude Code provider subscriptions",
  52. Long: "cc-switch manages multiple Claude Code provider env profiles and launches the `claude` CLI with the right environment.",
  53. SilenceUsage: true,
  54. SilenceErrors: false,
  55. // Bare run -> `use` (interactive when tty).
  56. RunE: func(cmd *cobra.Command, args []string) error {
  57. return runUse(cmd, app, args)
  58. },
  59. }
  60. root.PersistentFlags().BoolVarP(&app.verbose, "verbose", "v", false,
  61. "print trace output to stderr")
  62. root.PersistentFlags().StringVar(&app.configFlag, "config", "",
  63. "path to config file (overrides $CC_SWITCH_CONFIG)")
  64. root.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
  65. // Compute effective config path.
  66. if app.configFlag != "" {
  67. expanded, err := config.ExpandUser(app.configFlag)
  68. if err != nil {
  69. return err
  70. }
  71. app.configPath = expanded
  72. } else {
  73. p, err := config.ResolvePath()
  74. if err != nil {
  75. return err
  76. }
  77. app.configPath = p
  78. }
  79. app.tracef("config path: %s", app.configPath)
  80. res, err := config.Load(app.configPath)
  81. if err != nil {
  82. return err
  83. }
  84. if res.Warning != "" {
  85. fmt.Fprintln(app.stderr, res.Warning)
  86. }
  87. app.cfg = res.Config
  88. return nil
  89. }
  90. root.AddCommand(
  91. newAddCmd(app),
  92. newListCmd(app),
  93. newEditCmd(app),
  94. newRemoveCmd(app),
  95. newUseCmd(app),
  96. newConfigCmd(app),
  97. newTemplatesCmd(app),
  98. newVersionCmd(app),
  99. )
  100. return root
  101. }
  102. // Build-time injected values (see Makefile LDFLAGS).
  103. var (
  104. version = "dev"
  105. commit = "none"
  106. date = "unknown"
  107. )
  108. func newVersionCmd(app *appState) *cobra.Command {
  109. return &cobra.Command{
  110. Use: "version",
  111. Short: "Print version information",
  112. RunE: func(cmd *cobra.Command, args []string) error {
  113. _, err := fmt.Fprintf(app.stdout, "cc-switch %s (commit %s, built %s)\n", version, commit, date)
  114. return err
  115. },
  116. }
  117. }
  118. // ensure os.Stdin satisfies io.Reader — helps static tools.
  119. var _ io.Reader = os.Stdin