package cli import ( "fmt" "os" "path/filepath" "github.com/spf13/cobra" "git.unkin.net/unkin/teabot/internal/config" ) func newConfigCmd() *cobra.Command { cmd := &cobra.Command{ Use: "config", Short: "Inspect and scaffold teabot configuration", } cmd.AddCommand(newConfigInitCmd()) cmd.AddCommand(newConfigShowCmd()) return cmd } func newConfigInitCmd() *cobra.Command { var force bool cmd := &cobra.Command{ Use: "init", Short: "Write an example config file", RunE: func(cmd *cobra.Command, _ []string) error { path := configPath if _, err := os.Stat(path); err == nil && !force { return fmt.Errorf("config %s already exists (use --force to overwrite)", path) } if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { return err } if err := os.WriteFile(path, []byte(config.Example), 0o644); err != nil { return err } fmt.Fprintf(cmd.OutOrStdout(), "wrote example config to %s\n", path) fmt.Fprintln(cmd.OutOrStdout(), "edit it, then create the referenced tea config files with `tea logins add`.") return nil }, } cmd.Flags().BoolVar(&force, "force", false, "overwrite an existing config file") return cmd } func newConfigShowCmd() *cobra.Command { return &cobra.Command{ Use: "show", Short: "Validate and print the effective configuration", RunE: func(cmd *cobra.Command, _ []string) error { cfg, err := config.Load(configPath) if err != nil { return err } out := cmd.OutOrStdout() fmt.Fprintf(out, "config: %s\n", configPath) fmt.Fprintf(out, "gitea_url: %s\n", cfg.GiteaURL) fmt.Fprintf(out, "state_dir: %s\n", cfg.StateDirOrDefault()) fmt.Fprintf(out, "poll_interval: %s\n", cfg.PollInterval) fmt.Fprintf(out, "job_timeout: %s\n", cfg.JobTimeout) fmt.Fprintf(out, "max_concurrent: %d\n", cfg.MaxConcurrent) fmt.Fprintf(out, "job_image: %s\n", cfg.JobImage) fmt.Fprintf(out, "claude_config: %s\n", cfg.ClaudeConfigDir) if cfg.AnthropicBaseURL != "" { fmt.Fprintf(out, "anthropic_base_url: %s\n", cfg.AnthropicBaseURL) } if cfg.AnthropicAPIKey != "" { fmt.Fprintf(out, "anthropic_api_key: (set)\n") } fmt.Fprintf(out, "repos:\n") for _, r := range cfg.Repos { fmt.Fprintf(out, " - %s\n", r) } fmt.Fprintf(out, "personalities:\n") for _, p := range cfg.Personalities { fmt.Fprintf(out, " - %s (role=%s, login=%s)\n", p.Name, p.Role, p.Login) } return nil }, } }