Files
blackroad-cli/internal/commands/config.go

105 lines
2.5 KiB
Go

package commands
import (
"fmt"
"github.com/BlackRoad-OS/blackroad-cli/internal/config"
"github.com/spf13/cobra"
)
var configCmd = &cobra.Command{
Use: "config",
Short: "Manage CLI configuration",
Long: "Get and set CLI configuration values.",
}
var configGetCmd = &cobra.Command{
Use: "get <key>",
Short: "Get a config value",
Long: `Get a configuration value.
Available keys:
api_key - BlackRoad API key
api_url - API base URL
output - Output format (table, json)
timeout - Request timeout in seconds`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
cfg := config.Get()
key := args[0]
switch key {
case "api_key":
if cfg.APIKey != "" {
masked := cfg.APIKey[:4] + "..." + cfg.APIKey[len(cfg.APIKey)-4:]
fmt.Println(masked)
} else {
fmt.Println("(not set)")
}
case "api_url":
fmt.Println(cfg.APIURL)
case "output":
fmt.Println(cfg.Output)
case "timeout":
fmt.Println(cfg.Timeout)
default:
printError(fmt.Sprintf("Unknown config key: %s", key))
}
},
}
var configSetCmd = &cobra.Command{
Use: "set <key> <value>",
Short: "Set a config value",
Long: `Set a configuration value.
Examples:
blackroad config set api_key br_xxxxxxxxxxxxxxxx
blackroad config set api_url https://api.blackroad.io/v1
blackroad config set output json
blackroad config set timeout 60`,
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
key := args[0]
value := args[1]
switch key {
case "api_key", "api_url", "output", "timeout":
config.Set(key, value)
if err := config.Save(); err != nil {
printError(fmt.Sprintf("Failed to save config: %v", err))
return
}
printSuccess(fmt.Sprintf("Set %s", key))
default:
printError(fmt.Sprintf("Unknown config key: %s", key))
}
},
}
var configShowCmd = &cobra.Command{
Use: "show",
Short: "Show all config values",
Run: func(cmd *cobra.Command, args []string) {
cfg := config.Get()
fmt.Printf("%s\n", bold("Configuration"))
apiKey := "(not set)"
if cfg.APIKey != "" {
apiKey = cfg.APIKey[:4] + "..." + cfg.APIKey[len(cfg.APIKey)-4:]
}
fmt.Printf(" %s %s\n", cyan("api_key:"), apiKey)
fmt.Printf(" %s %s\n", cyan("api_url:"), cfg.APIURL)
fmt.Printf(" %s %s\n", cyan("output:"), cfg.Output)
fmt.Printf(" %s %d\n", cyan("timeout:"), cfg.Timeout)
},
}
func init() {
configCmd.AddCommand(configGetCmd)
configCmd.AddCommand(configSetCmd)
configCmd.AddCommand(configShowCmd)
}