Compare commits

..
2 Commits
3 changed files with 106 additions and 0 deletions
+1
View File
@@ -5,6 +5,7 @@ import (
"os"
"git.yoresee.cc/nightstar/ry/internal/cmd"
_ "git.yoresee.cc/nightstar/ry/internal/provider/rainyun"
)
func main() {
+94
View File
@@ -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)
}
+11
View File
@@ -25,6 +25,12 @@ var (
var rootCmd = &cobra.Command{
Use: "ry",
Short: "Multi-cloud CLI — manage cloud resources from your terminal",
Long: `ry is a multi-cloud CLI that lets you manage cloud resources from your terminal.
Use "ry [command] --help" for more information about a command.`,
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
var err error
cfg, err = config.Load(cfgPath)
@@ -32,6 +38,11 @@ var rootCmd = &cobra.Command{
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
if flagProvider != "" {
providerName = flagProvider