config.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package cmd
  2. import (
  3. "fmt"
  4. "github.com/fatih/color"
  5. "github.com/letta/letta-switchboard-cli/internal/config"
  6. "github.com/spf13/cobra"
  7. )
  8. var configCmd = &cobra.Command{
  9. Use: "config",
  10. Short: "Manage CLI configuration",
  11. Long: "Configure API credentials and settings for the Letta Schedules CLI",
  12. }
  13. var setAPIKeyCmd = &cobra.Command{
  14. Use: "set-api-key [api-key]",
  15. Short: "Set the Letta API key",
  16. Args: cobra.ExactArgs(1),
  17. RunE: func(cmd *cobra.Command, args []string) error {
  18. apiKey := args[0]
  19. if err := config.SetAPIKey(apiKey); err != nil {
  20. return fmt.Errorf("failed to set API key: %w", err)
  21. }
  22. color.Green("✓ API key set successfully")
  23. return nil
  24. },
  25. }
  26. var setURLCmd = &cobra.Command{
  27. Use: "set-url [url]",
  28. Short: "Set the API base URL",
  29. Args: cobra.ExactArgs(1),
  30. RunE: func(cmd *cobra.Command, args []string) error {
  31. url := args[0]
  32. if err := config.SetBaseURL(url); err != nil {
  33. return fmt.Errorf("failed to set base URL: %w", err)
  34. }
  35. color.Green("✓ Base URL set successfully")
  36. return nil
  37. },
  38. }
  39. var showConfigCmd = &cobra.Command{
  40. Use: "show",
  41. Short: "Show current configuration",
  42. RunE: func(cmd *cobra.Command, args []string) error {
  43. cfg, err := config.Load()
  44. if err != nil {
  45. return fmt.Errorf("failed to load config: %w", err)
  46. }
  47. fmt.Println("Current configuration:")
  48. fmt.Printf(" Base URL: %s\n", cfg.BaseURL)
  49. if cfg.APIKey != "" {
  50. fmt.Printf(" API Key: %s...%s\n", cfg.APIKey[:8], cfg.APIKey[len(cfg.APIKey)-4:])
  51. } else {
  52. fmt.Println(" API Key: (not set)")
  53. }
  54. configDir, _ := config.GetConfigDir()
  55. fmt.Printf("\nConfig file: %s/config.yaml\n", configDir)
  56. return nil
  57. },
  58. }
  59. func init() {
  60. rootCmd.AddCommand(configCmd)
  61. configCmd.AddCommand(setAPIKeyCmd)
  62. configCmd.AddCommand(setURLCmd)
  63. configCmd.AddCommand(showConfigCmd)
  64. }