-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d82273c
commit 00942c3
Showing
3 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |