Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cfctl using Evans CLI #8

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
Loading