feat: provider cmd — list, use, add with interactive key input
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"git.yoresee.cc/nightstar/ry/internal/cmd"
|
"git.yoresee.cc/nightstar/ry/internal/cmd"
|
||||||
|
_ "git.yoresee.cc/nightstar/ry/internal/provider/rainyun"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
@@ -0,0 +1,94 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.yoresee.cc/nightstar/ry/internal/config"
|
||||||
|
"git.yoresee.cc/nightstar/ry/internal/provider"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var providerCmd = &cobra.Command{
|
||||||
|
Use: "provider",
|
||||||
|
Short: "Manage cloud providers",
|
||||||
|
}
|
||||||
|
|
||||||
|
var providerListCmd = &cobra.Command{
|
||||||
|
Use: "list",
|
||||||
|
Short: "List configured providers",
|
||||||
|
Annotations: map[string]string{"skipProvider": "true"},
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
// show all registered providers, mark current with *
|
||||||
|
current := ""
|
||||||
|
if cfg != nil {
|
||||||
|
current = cfg.CurrentProvider
|
||||||
|
}
|
||||||
|
for _, name := range provider.List() {
|
||||||
|
marker := " "
|
||||||
|
if name == current {
|
||||||
|
marker = "*"
|
||||||
|
}
|
||||||
|
fmt.Printf("%s %s\n", marker, name)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var providerUseCmd = &cobra.Command{
|
||||||
|
Use: "use <name>",
|
||||||
|
Short: "Switch the current cloud provider",
|
||||||
|
Annotations: map[string]string{"skipProvider": "true"},
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
name := args[0]
|
||||||
|
if _, ok := cfg.Providers[name]; !ok {
|
||||||
|
return fmt.Errorf("provider %q not configured — run 'ry provider add %s' first", name, name)
|
||||||
|
}
|
||||||
|
cfg.CurrentProvider = name
|
||||||
|
if err := config.Save(cfgPath, cfg); err != nil {
|
||||||
|
return fmt.Errorf("save config: %w", err)
|
||||||
|
}
|
||||||
|
fmt.Printf("Switched to provider %q\n", name)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var providerAddCmd = &cobra.Command{
|
||||||
|
Use: "add <name>",
|
||||||
|
Short: "Add a new cloud provider",
|
||||||
|
Annotations: map[string]string{"skipProvider": "true"},
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
name := args[0]
|
||||||
|
fmt.Printf("Adding provider %q...\n", name)
|
||||||
|
fmt.Print("Enter API Key: ")
|
||||||
|
var apiKey string
|
||||||
|
fmt.Scanln(&apiKey)
|
||||||
|
if apiKey == "" {
|
||||||
|
return fmt.Errorf("API Key is required")
|
||||||
|
}
|
||||||
|
if cfg.Providers == nil {
|
||||||
|
cfg.Providers = make(map[string]config.ProviderConfig)
|
||||||
|
}
|
||||||
|
cfg.Providers[name] = config.ProviderConfig{APIKey: apiKey}
|
||||||
|
if cfg.CurrentProvider == "" {
|
||||||
|
cfg.CurrentProvider = name
|
||||||
|
}
|
||||||
|
if err := config.Save(cfgPath, cfg); err != nil {
|
||||||
|
return fmt.Errorf("save config: %w", err)
|
||||||
|
}
|
||||||
|
fmt.Printf("Provider %q added successfully\n", name)
|
||||||
|
if cfg.CurrentProvider == name {
|
||||||
|
fmt.Printf("Set as current provider\n")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
providerCmd.AddCommand(providerListCmd)
|
||||||
|
providerCmd.AddCommand(providerUseCmd)
|
||||||
|
providerCmd.AddCommand(providerAddCmd)
|
||||||
|
rootCmd.AddCommand(providerCmd)
|
||||||
|
}
|
||||||
@@ -38,6 +38,11 @@ Use "ry [command] --help" for more information about a command.`,
|
|||||||
return fmt.Errorf("load config: %w", err)
|
return fmt.Errorf("load config: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// provider subcommands don't need an initialized provider
|
||||||
|
if cmd.Annotations != nil && cmd.Annotations["skipProvider"] == "true" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
providerName := cfg.CurrentProvider
|
providerName := cfg.CurrentProvider
|
||||||
if flagProvider != "" {
|
if flagProvider != "" {
|
||||||
providerName = flagProvider
|
providerName = flagProvider
|
||||||
|
|||||||
Reference in New Issue
Block a user