Skip to content

Commit

Permalink
feat: config list
Browse files Browse the repository at this point in the history
  • Loading branch information
domenicsim1 authored Oct 5, 2022
1 parent d82273c commit 00942c3
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/octopus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
93 changes: 93 additions & 0 deletions pkg/cmd/config/list/list.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 00942c3

Please sign in to comment.