From 00942c33509c9f10ee71023d72cddf40efb2b0d2 Mon Sep 17 00:00:00 2001 From: domenicsim1 <87625140+domenicsim1@users.noreply.github.com> Date: Wed, 5 Oct 2022 13:53:45 +1100 Subject: [PATCH] feat: config list --- cmd/octopus/main.go | 2 +- pkg/cmd/config/config.go | 2 + pkg/cmd/config/list/list.go | 93 +++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 pkg/cmd/config/list/list.go diff --git a/cmd/octopus/main.go b/cmd/octopus/main.go index 7d3e430f..0cd8d834 100644 --- a/cmd/octopus/main.go +++ b/cmd/octopus/main.go @@ -51,7 +51,7 @@ func main() { clientFactory, err := apiclient.NewClientFactoryFromConfig(askProvider) if err != nil { // a small subset of commands can function even if the app doesn't have valid configuration information - if cmdToRun == "config" || cmdToRun == "version" { + if cmdToRun == "config" || cmdToRun == "version" || cmdToRun == "help" { clientFactory = apiclient.NewStubClientFactory() } else { // can't possibly work diff --git a/pkg/cmd/config/config.go b/pkg/cmd/config/config.go index f2ccd2f5..66f04b7c 100644 --- a/pkg/cmd/config/config.go +++ b/pkg/cmd/config/config.go @@ -2,6 +2,7 @@ package config import ( getCmd "github.com/OctopusDeploy/cli/pkg/cmd/config/get" + listCmd "github.com/OctopusDeploy/cli/pkg/cmd/config/list" setCmd "github.com/OctopusDeploy/cli/pkg/cmd/config/set" "github.com/OctopusDeploy/cli/pkg/constants/annotations" "github.com/OctopusDeploy/cli/pkg/factory" @@ -20,5 +21,6 @@ func NewCmdConfig(f factory.Factory) *cobra.Command { cmd.AddCommand(getCmd.NewCmdGet(f)) cmd.AddCommand(setCmd.NewCmdSet(f)) + cmd.AddCommand(listCmd.NewCmdList(f)) return cmd } diff --git a/pkg/cmd/config/list/list.go b/pkg/cmd/config/list/list.go new file mode 100644 index 00000000..d3cbd5da --- /dev/null +++ b/pkg/cmd/config/list/list.go @@ -0,0 +1,93 @@ +package list + +import ( + "encoding/json" + "fmt" + "strings" + + "github.com/MakeNowJust/heredoc/v2" + "github.com/OctopusDeploy/cli/pkg/constants" + "github.com/OctopusDeploy/cli/pkg/factory" + "github.com/OctopusDeploy/cli/pkg/output" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +func NewCmdList(_ factory.Factory) *cobra.Command { + cmd := &cobra.Command{ + Use: "list", + Short: "List values from config file", + Long: "List values from config file.", + Example: fmt.Sprintf(heredoc.Doc(` + $ %s config list" + `), constants.ExecutableName), + Aliases: []string{"ls"}, + RunE: func(cmd *cobra.Command, args []string) error { + return listRun(cmd) + }, + } + + return cmd +} + +func listRun(cmd *cobra.Command) error { + configFile := viper.New() + configFile.SetConfigFile(viper.ConfigFileUsed()) + configFile.ReadInConfig() + + if configFile.IsSet(constants.ConfigApiKey) { + configFile.Set(constants.ConfigApiKey, "***") + } + + type ConfigData struct { + ApiKey string `json:"apikey"` + Editor string `json:"editor"` + Host string `json:"host"` + NoPrompt string `json:"noprompt"` + OutputFormat string `json:"outputformat"` + Space string `json:"space"` + } + + outputFormat, _ := cmd.Flags().GetString(constants.FlagOutputFormat) + if outputFormat == "" { + outputFormat = viper.GetString(constants.ConfigOutputFormat) + } + + switch strings.ToLower(outputFormat) { + case constants.OutputFormatJson: + configData := &ConfigData{} + for _, key := range configFile.AllKeys() { + switch strings.ToLower(key) { + case strings.ToLower(constants.ConfigApiKey): + configData.ApiKey = configFile.GetString(key) + case strings.ToLower(constants.ConfigEditor): + configData.Editor = configFile.GetString(key) + case strings.ToLower(constants.ConfigHost): + configData.Host = configFile.GetString(key) + case strings.ToLower(constants.ConfigNoPrompt): + configData.NoPrompt = configFile.GetString(key) + case strings.ToLower(constants.ConfigSpace): + configData.Space = configFile.GetString(key) + case strings.ToLower(constants.ConfigOutputFormat): + configData.OutputFormat = configFile.GetString(key) + default: + return fmt.Errorf("the key '%s' is not a supported config option", key) + } + } + data, _ := json.MarshalIndent(configData, "", " ") + cmd.Println(string(data)) + case constants.OutputFormatBasic: + for _, key := range configFile.AllKeys() { + cmd.Println(configFile.GetString(key)) + } + case constants.OutputFormatTable: + t := output.NewTable(cmd.OutOrStdout()) + t.AddRow(output.Bold("KEY"), output.Bold("VALUE")) + for _, key := range configFile.AllKeys() { + t.AddRow(key, configFile.GetString(key)) + } + t.Print() + } + + return nil +}