refactor: move cmd handlers to cmd/cli/commands/ following tag_file architecture
This commit is contained in:
+120
@@ -0,0 +1,120 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.yoresee.cc/nightstar/ry/internal/config"
|
||||
"git.yoresee.cc/nightstar/ry/internal/output"
|
||||
"git.yoresee.cc/nightstar/ry/internal/provider"
|
||||
|
||||
"git.yoresee.cc/nightstar/ry/cmd/cli/commands/billing"
|
||||
"git.yoresee.cc/nightstar/ry/cmd/cli/commands/configcmd"
|
||||
"git.yoresee.cc/nightstar/ry/cmd/cli/commands/domain"
|
||||
"git.yoresee.cc/nightstar/ry/cmd/cli/commands/providercmd"
|
||||
"git.yoresee.cc/nightstar/ry/cmd/cli/commands/server"
|
||||
"git.yoresee.cc/nightstar/ry/cmd/cli/commands/storage"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
cfg *config.Config
|
||||
cfgPath string
|
||||
prov provider.Provider
|
||||
out *output.Printer
|
||||
flagProvider string
|
||||
flagOutput string
|
||||
flagRaw bool
|
||||
flagDebug bool
|
||||
)
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return fmt.Errorf("load config: %w", err)
|
||||
}
|
||||
|
||||
if cmd.Annotations != nil && cmd.Annotations["skipProvider"] == "true" {
|
||||
return nil
|
||||
}
|
||||
|
||||
providerName := cfg.CurrentProvider
|
||||
if flagProvider != "" {
|
||||
providerName = flagProvider
|
||||
}
|
||||
if env := os.Getenv("RY_PROVIDER"); env != "" {
|
||||
providerName = env
|
||||
}
|
||||
|
||||
if providerName == "" {
|
||||
return fmt.Errorf("no provider configured — run 'ry provider add <name>' first")
|
||||
}
|
||||
|
||||
pc := cfg.Providers[providerName]
|
||||
prov, err = provider.New(providerName, provider.ProviderConfig{
|
||||
APIKey: pc.APIKey,
|
||||
AccessKeyID: pc.AccessKeyID,
|
||||
AccessKeySecret: pc.AccessKeySecret,
|
||||
Endpoint: pc.Endpoint,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
outputFormat := cfg.Output
|
||||
if flagOutput != "" {
|
||||
outputFormat = flagOutput
|
||||
}
|
||||
if flagRaw {
|
||||
outputFormat = "raw"
|
||||
}
|
||||
out = output.New(outputFormat, os.Stdout)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
cfgPath = config.DefaultPath()
|
||||
rootCmd.PersistentFlags().StringVarP(&flagProvider, "provider", "p", "", "Cloud provider to use")
|
||||
rootCmd.PersistentFlags().StringVarP(&flagOutput, "output", "o", "", "Output format: table, json, yaml, raw")
|
||||
rootCmd.PersistentFlags().BoolVar(&flagRaw, "raw", false, "Output raw API response")
|
||||
rootCmd.PersistentFlags().BoolVar(&flagDebug, "debug", false, "Enable debug output")
|
||||
|
||||
completionCmd := &cobra.Command{
|
||||
Use: "completion [bash|zsh|fish]",
|
||||
Short: "Generate shell completion script",
|
||||
Annotations: map[string]string{"skipProvider": "true"},
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
switch args[0] {
|
||||
case "bash":
|
||||
return rootCmd.GenBashCompletion(os.Stdout)
|
||||
case "zsh":
|
||||
return rootCmd.GenZshCompletion(os.Stdout)
|
||||
case "fish":
|
||||
return rootCmd.GenFishCompletion(os.Stdout, true)
|
||||
default:
|
||||
return fmt.Errorf("unsupported shell: %s", args[0])
|
||||
}
|
||||
},
|
||||
}
|
||||
rootCmd.AddCommand(completionCmd)
|
||||
|
||||
rootCmd.AddCommand(configcmd.Cmd(cfg, cfgPath))
|
||||
rootCmd.AddCommand(providercmd.Cmd(cfg, cfgPath))
|
||||
rootCmd.AddCommand(server.Cmd(&prov, &out, &flagRaw))
|
||||
rootCmd.AddCommand(storage.Cmd(&prov, &out))
|
||||
rootCmd.AddCommand(domain.Cmd(&prov, &out))
|
||||
rootCmd.AddCommand(billing.Cmd(&prov, &out))
|
||||
}
|
||||
Reference in New Issue
Block a user