diff --git a/cmd/root.go b/cmd/root.go index bb09f67..af6b5a5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,18 +1,19 @@ -/* -Copyright © 2024 NAME HERE -*/ package cmd import ( "fmt" "os" + "os/exec" "path/filepath" + "strings" + "github.com/pterm/pterm" "github.com/spf13/cobra" "github.com/spf13/viper" ) var cfgFile string +var endpoint string // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ @@ -21,9 +22,9 @@ var rootCmd = &cobra.Command{ Long: `cfctl controls the SpaceONE services. Find more information at: https://docs.spaceone.megazone.io/cfctl`, - // Uncomment the following line if your bare application - // has an action associated with it: - // Run: func(cmd *cobra.Command, args []string) { }, + Run: func(cmd *cobra.Command, args []string) { + runEvans() + }, } // Execute adds all child commands to the root command and sets flags appropriately. @@ -31,6 +32,7 @@ var rootCmd = &cobra.Command{ func Execute() { err := rootCmd.Execute() if err != nil { + pterm.Error.Println("Error executing root command:", err) os.Exit(1) } } @@ -42,7 +44,9 @@ func init() { // Cobra supports persistent flags, which, if defined here, // will be global for your application. - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.spaceone/cfctl.yaml)") + rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.spaceone/cfctl.yaml)") + rootCmd.PersistentFlags().StringVarP(&endpoint, "endpoint", "e", "", "endpoint to use for the command (e.g., identity, inventory)") + rootCmd.MarkPersistentFlagRequired("endpoint") // Cobra also supports local flags, which will only run // when this action is called directly. @@ -69,6 +73,43 @@ func initConfig() { // If a config file is found, read it in. if err := viper.ReadInConfig(); err == nil { - fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) + pterm.Info.Println("Using config file:", viper.ConfigFileUsed()) + } else { + pterm.Warning.Println("No valid config file found.") + } +} + +// runEvans executes the Evans command with the required parameters. +func runEvans() { + token := viper.GetString("token") + if token == "" { + pterm.Error.Println("Error: token not found in config file") + os.Exit(1) + } + + // Find the specified endpoint + var host string + endpoints := viper.GetStringMapString("endpoints") + if endpointURL, exists := endpoints[endpoint]; exists { + parts := strings.Split(endpointURL, "//") + if len(parts) > 1 { + host = strings.Split(parts[1], ":")[0] + } + } + + if host == "" { + pterm.Error.Printf("Error: endpoint '%s' not found or invalid in config file\n", endpoint) + os.Exit(1) + } + + cmd := exec.Command("evans", "--reflection", "repl", "-p", "443", "--tls", "--header", fmt.Sprintf("token=%s", token), "--host", host) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.Stdin = os.Stdin + + err := cmd.Run() + if err != nil { + pterm.Error.Printf("Error running Evans: %v\n", err) + os.Exit(1) } }