feat: cobra root — config integration, global flags, provider resolution
This commit is contained in:
+67
-1
@@ -1,14 +1,80 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.yoresee.cc/nightstar/ry/internal/config"
|
||||
"git.yoresee.cc/nightstar/ry/internal/output"
|
||||
"git.yoresee.cc/nightstar/ry/internal/provider"
|
||||
|
||||
"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, like gh for cloud providers",
|
||||
Short: "Multi-cloud CLI — manage cloud resources from your terminal",
|
||||
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)
|
||||
}
|
||||
|
||||
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 Execute() error {
|
||||
return rootCmd.Execute()
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user