Skip to content

Commit

Permalink
Merge pull request #8 from yjinjo/v0
Browse files Browse the repository at this point in the history
feat: Update cfctl using Evans CLI
  • Loading branch information
yjinjo authored Nov 6, 2024
2 parents 964f4b9 + 68ac3c7 commit 7758ac9
Showing 1 changed file with 49 additions and 8 deletions.
57 changes: 49 additions & 8 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
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{
Expand All @@ -21,16 +22,17 @@ 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.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
pterm.Error.Println("Error executing root command:", err)
os.Exit(1)
}
}
Expand All @@ -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.
Expand All @@ -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)
}
}

0 comments on commit 7758ac9

Please sign in to comment.