config.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package cmd
  2. import (
  3. "context"
  4. "fmt"
  5. "gogs.dmsc.dev/arp/arp_cli/config"
  6. "github.com/AlecAivazis/survey/v2"
  7. "github.com/urfave/cli/v3"
  8. )
  9. // ConfigCommand returns the config command
  10. func ConfigCommand() *cli.Command {
  11. return &cli.Command{
  12. Name: "config",
  13. Usage: "Manage CLI configuration",
  14. Description: `View and manage the arp_cli configuration.
  15. The configuration is stored in ~/.arp_cli/config.json and contains:
  16. - Server URL
  17. - Authentication token
  18. - Logged-in user email`,
  19. Commands: []*cli.Command{
  20. {
  21. Name: "view",
  22. Usage: "Show current configuration",
  23. Action: configView,
  24. },
  25. {
  26. Name: "set-url",
  27. Usage: "Set the server URL",
  28. Action: configSetURL,
  29. Flags: []cli.Flag{
  30. &cli.StringFlag{
  31. Name: "url",
  32. Aliases: []string{"u"},
  33. Usage: "Server URL",
  34. Required: true,
  35. },
  36. },
  37. },
  38. {
  39. Name: "logout",
  40. Usage: "Clear stored authentication token",
  41. Aliases: []string{"clear"},
  42. Action: configLogout,
  43. },
  44. },
  45. Action: configView,
  46. }
  47. }
  48. func configView(ctx context.Context, cmd *cli.Command) error {
  49. cfg, err := config.Load()
  50. if err != nil {
  51. return fmt.Errorf("failed to load config: %w", err)
  52. }
  53. fmt.Println("Current Configuration:")
  54. fmt.Printf(" Server URL: %s\n", cfg.ServerURL)
  55. if cfg.UserEmail != "" {
  56. fmt.Printf(" Logged in as: %s\n", cfg.UserEmail)
  57. } else {
  58. fmt.Println(" Logged in as: (not authenticated)")
  59. }
  60. if cfg.Token != "" {
  61. // Show first/last few characters of token
  62. if len(cfg.Token) > 20 {
  63. fmt.Printf(" Token: %s...%s\n", cfg.Token[:10], cfg.Token[len(cfg.Token)-6:])
  64. } else {
  65. fmt.Println(" Token: (set)")
  66. }
  67. } else {
  68. fmt.Println(" Token: (not set)")
  69. }
  70. return nil
  71. }
  72. func configSetURL(ctx context.Context, cmd *cli.Command) error {
  73. url := cmd.String("url")
  74. cfg, err := config.Load()
  75. if err != nil {
  76. return fmt.Errorf("failed to load config: %w", err)
  77. }
  78. // If user is logged in, warn them
  79. if cfg.Token != "" {
  80. confirm := false
  81. prompt := &survey.Confirm{
  82. Message: "Changing the server URL will clear your authentication token. Continue?",
  83. Default: false,
  84. }
  85. if err := survey.AskOne(prompt, &confirm); err != nil {
  86. return err
  87. }
  88. if !confirm {
  89. fmt.Println("Aborted.")
  90. return nil
  91. }
  92. cfg.Token = ""
  93. cfg.UserEmail = ""
  94. }
  95. cfg.ServerURL = url
  96. if err := config.Save(cfg); err != nil {
  97. return fmt.Errorf("failed to save config: %w", err)
  98. }
  99. fmt.Printf("Server URL set to: %s\n", url)
  100. if cfg.Token == "" {
  101. fmt.Println("Please run 'arp_cli login' to authenticate with the new server.")
  102. }
  103. return nil
  104. }
  105. func configLogout(ctx context.Context, cmd *cli.Command) error {
  106. cfg, err := config.Load()
  107. if err != nil {
  108. return fmt.Errorf("failed to load config: %w", err)
  109. }
  110. if cfg.Token == "" {
  111. fmt.Println("No authentication token stored.")
  112. return nil
  113. }
  114. confirm := false
  115. prompt := &survey.Confirm{
  116. Message: "Are you sure you want to logout?",
  117. Default: true,
  118. }
  119. if err := survey.AskOne(prompt, &confirm); err != nil {
  120. return err
  121. }
  122. if !confirm {
  123. fmt.Println("Aborted.")
  124. return nil
  125. }
  126. if err := config.Clear(); err != nil {
  127. return fmt.Errorf("failed to clear config: %w", err)
  128. }
  129. fmt.Println("Logged out successfully.")
  130. return nil
  131. }