133 lines
3.1 KiB
Go
133 lines
3.1 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func NewConfigCmd() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "config",
|
|
Short: "Manage CLI configuration",
|
|
Long: "View and modify CLI configuration settings",
|
|
}
|
|
|
|
cmd.AddCommand(newConfigShowCmd())
|
|
cmd.AddCommand(newConfigSetCmd())
|
|
cmd.AddCommand(newConfigResetCmd())
|
|
|
|
return cmd
|
|
}
|
|
|
|
func newConfigShowCmd() *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "show",
|
|
Short: "Show current configuration",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
config, err := LoadConfig()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to load config: %w", err)
|
|
}
|
|
|
|
configPath, _ := GetConfigPath()
|
|
|
|
fmt.Printf("Configuration:\n")
|
|
fmt.Printf(" Config file: %s\n", configPath)
|
|
fmt.Printf(" Server URL: %s\n", config.ServerURL)
|
|
fmt.Printf(" Account Number: %s\n", getConfigValue(config.AccountNumber, "Not set"))
|
|
fmt.Printf(" Access Token: %s\n", getConfigValue(config.AccessToken, "Not set"))
|
|
|
|
if config.IsAuthenticated() {
|
|
fmt.Printf(" Status: Authenticated\n")
|
|
} else {
|
|
fmt.Printf(" Status: Not authenticated\n")
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
}
|
|
|
|
func newConfigSetCmd() *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "set <key> <value>",
|
|
Short: "Set a configuration value",
|
|
Long: `Set a configuration value. Available keys:
|
|
server-url - TermCloud server URL (e.g., https://api.termcloud.com)`,
|
|
Args: cobra.ExactArgs(2),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
key := args[0]
|
|
value := args[1]
|
|
|
|
config, err := LoadConfig()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to load config: %w", err)
|
|
}
|
|
|
|
switch key {
|
|
case "server-url":
|
|
config.ServerURL = value
|
|
fmt.Printf("Server URL set to: %s\n", value)
|
|
default:
|
|
return fmt.Errorf("unknown config key: %s", key)
|
|
}
|
|
|
|
if err := config.Save(); err != nil {
|
|
return fmt.Errorf("failed to save config: %w", err)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
}
|
|
}
|
|
|
|
func newConfigResetCmd() *cobra.Command {
|
|
var force bool
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "reset",
|
|
Short: "Reset configuration to defaults",
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
if !force {
|
|
fmt.Printf("This will delete all configuration including login credentials.\n")
|
|
fmt.Printf("Are you sure? (y/N): ")
|
|
var response string
|
|
fmt.Scanln(&response)
|
|
if response != "y" && response != "yes" && response != "Y" && response != "YES" {
|
|
fmt.Printf("Cancelled.\n")
|
|
return nil
|
|
}
|
|
}
|
|
|
|
configPath, err := GetConfigPath()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get config path: %w", err)
|
|
}
|
|
|
|
if err := os.Remove(configPath); err != nil && !os.IsNotExist(err) {
|
|
return fmt.Errorf("failed to remove config file: %w", err)
|
|
}
|
|
|
|
fmt.Printf("Configuration reset successfully.\n")
|
|
fmt.Printf("You'll need to log in again with 'tcman account login' or create a new account.\n")
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
cmd.Flags().BoolVar(&force, "force", false, "Force reset without confirmation")
|
|
return cmd
|
|
}
|
|
|
|
func getConfigValue(value, defaultValue string) string {
|
|
if value == "" {
|
|
return defaultValue
|
|
}
|
|
if len(value) > 20 {
|
|
return value[:17] + "..."
|
|
}
|
|
return value
|
|
}
|