-
Notifications
You must be signed in to change notification settings - Fork 2
add config list-keys command #66
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b48c53a
add config list-keys command
wesleymccollam bd67fc1
update test regex
wesleymccollam d8acb29
update tests
wesleymccollam 00fd652
match other test formatting
wesleymccollam 2e5c911
remove activeProfile on output, update error handling, add output tes…
wesleymccollam abae9a2
check key length before running function, remove unneeded comment lin…
wesleymccollam 352c74d
adjust spacing in test
wesleymccollam 1eba4c2
remove yaml test
wesleymccollam 9130db8
add command example, update logic and readability
wesleymccollam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,44 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "github.com/pingidentity/pingcli/cmd/common" | ||
| config_internal "github.com/pingidentity/pingcli/internal/commands/config" | ||
| "github.com/pingidentity/pingcli/internal/configuration/options" | ||
| "github.com/pingidentity/pingcli/internal/logger" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| const ( | ||
| listKeysCommandExamples = ` List all configuration keys stored in the CLI configuration file. | ||
| pingcli config list-keys | ||
| pingcli config list-keys --yaml` | ||
| ) | ||
|
|
||
| func NewConfigListKeysCommand() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Args: common.ExactArgs(0), | ||
| DisableFlagsInUseLine: true, // We write our own flags in @Use attribute | ||
| Example: listKeysCommandExamples, | ||
| Long: `View the complete list of available configuration options. These attributes can be saved via the set and unset config subcommands or stored in a profile within the config file. | ||
| For details on the configuration options visit: https://github.com/pingidentity/pingcli/blob/main/docs/tool-configuration/configuration-key.md`, | ||
| RunE: configListKeysRunE, | ||
| Short: "List all configuration keys.", | ||
| Use: "list-keys [flags]", | ||
| } | ||
|
|
||
| cmd.Flags().AddFlag(options.ConfigListKeysYamlOption.Flag) | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func configListKeysRunE(cmd *cobra.Command, args []string) error { | ||
| l := logger.Get() | ||
| l.Debug().Msgf("Config list-keys Subcommand Called.") | ||
|
|
||
| err := config_internal.RunInternalConfigListKeys() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or 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,80 @@ | ||
| package config_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/pingidentity/pingcli/internal/configuration/options" | ||
| "github.com/pingidentity/pingcli/internal/testing/testutils" | ||
| "github.com/pingidentity/pingcli/internal/testing/testutils_cobra" | ||
| ) | ||
|
|
||
| // Test Config List Keys Command Executes without issue | ||
| func TestConfigListKeysCmd_Execute(t *testing.T) { | ||
| err := testutils_cobra.ExecutePingcli(t, "config", "list-keys") | ||
| testutils.CheckExpectedError(t, err, nil) | ||
| } | ||
|
|
||
| // Test Config List Keys YAML Command --yaml, -y flag | ||
| func TestConfigListKeysCmd_YAMLFlag(t *testing.T) { | ||
| err := testutils_cobra.ExecutePingcli(t, "config", "list-keys", "--yaml") | ||
| testutils.CheckExpectedError(t, err, nil) | ||
|
|
||
| err = testutils_cobra.ExecutePingcli(t, "config", "list-keys", "-y") | ||
| testutils.CheckExpectedError(t, err, nil) | ||
| } | ||
|
|
||
| // Test Config List Keys Command --help, -h flag | ||
| func TestConfigListKeysCmd_HelpFlag(t *testing.T) { | ||
| err := testutils_cobra.ExecutePingcli(t, "config", "list-keys", "--help") | ||
| testutils.CheckExpectedError(t, err, nil) | ||
|
|
||
| err = testutils_cobra.ExecutePingcli(t, "config", "list-keys", "-h") | ||
| testutils.CheckExpectedError(t, err, nil) | ||
| } | ||
|
|
||
| // Test Config List Keys Command fails when provided too many arguments | ||
| func TestConfigListKeysCmd_TooManyArgs(t *testing.T) { | ||
| expectedErrorPattern := `^failed to execute 'pingcli config list-keys': command accepts 0 arg\(s\), received 1$` | ||
| err := testutils_cobra.ExecutePingcli(t, "config", "list-keys", options.RootColorOption.ViperKey) | ||
| testutils.CheckExpectedError(t, err, &expectedErrorPattern) | ||
| } | ||
|
|
||
| // https://pkg.go.dev/testing#hdr-Examples | ||
| func Example_listKeysValue() { | ||
| t := testing.T{} | ||
| _ = testutils_cobra.ExecutePingcli(&t, "config", "list-keys") | ||
|
|
||
| // Output: | ||
| // Valid Keys: | ||
| // - activeProfile | ||
| // - description | ||
| // - export.format | ||
| // - export.outputDirectory | ||
| // - export.overwrite | ||
| // - export.pingone.environmentID | ||
| // - export.services | ||
| // - noColor | ||
| // - outputFormat | ||
| // - request.accessToken | ||
| // - request.accessTokenExpiry | ||
| // - request.fail | ||
| // - request.service | ||
| // - service.pingfederate.adminAPIPath | ||
| // - service.pingfederate.authentication.accessTokenAuth.accessToken | ||
| // - service.pingfederate.authentication.basicAuth.password | ||
| // - service.pingfederate.authentication.basicAuth.username | ||
| // - service.pingfederate.authentication.clientCredentialsAuth.clientID | ||
| // - service.pingfederate.authentication.clientCredentialsAuth.clientSecret | ||
| // - service.pingfederate.authentication.clientCredentialsAuth.scopes | ||
| // - service.pingfederate.authentication.clientCredentialsAuth.tokenURL | ||
| // - service.pingfederate.authentication.type | ||
| // - service.pingfederate.caCertificatePemFiles | ||
| // - service.pingfederate.httpsHost | ||
| // - service.pingfederate.insecureTrustAllTLS | ||
| // - service.pingfederate.xBypassExternalValidationHeader | ||
| // - service.pingone.authentication.type | ||
| // - service.pingone.authentication.worker.clientID | ||
| // - service.pingone.authentication.worker.clientSecret | ||
| // - service.pingone.authentication.worker.environmentID | ||
| // - service.pingone.regionCode | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 config_internal | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/pingidentity/pingcli/internal/configuration" | ||
| "github.com/pingidentity/pingcli/internal/configuration/options" | ||
| "github.com/pingidentity/pingcli/internal/output" | ||
| "github.com/pingidentity/pingcli/internal/profiles" | ||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| func returnKeysYamlString() (string, error) { | ||
| var err error | ||
| viperKeys := configuration.ViperKeys() | ||
|
|
||
| if len(viperKeys) == 0 { | ||
| return "", fmt.Errorf("unable to retrieve valid keys") | ||
| } | ||
|
|
||
| // Split the input string into individual keys | ||
| keyMap := make(map[string]interface{}) | ||
|
|
||
| // Iterate over each viper key | ||
| for _, viperKey := range viperKeys { | ||
| // Skip the "activeProfile" key | ||
| if viperKey == "activeProfile" { | ||
| continue | ||
| } | ||
|
|
||
| // Create a nested map for each yaml key | ||
| currentMap := keyMap | ||
| yamlKeys := strings.Split(viperKey, ".") | ||
| for i, k := range yamlKeys { | ||
| // If it's the last yaml key, set an empty map | ||
| if i == len(yamlKeys)-1 { | ||
| currentMap[k] = "" | ||
| } else { | ||
| // Otherwise, create or navigate to the next level | ||
| if _, exists := currentMap[k]; !exists { | ||
| currentMap[k] = make(map[string]interface{}) | ||
| } | ||
| currentMap = currentMap[k].(map[string]interface{}) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Marshal the result into YAML | ||
| yamlData, err := yaml.Marshal(keyMap) | ||
| if err != nil { | ||
| return "", fmt.Errorf("error marshaling keys to YAML format") | ||
| } | ||
|
|
||
| return string(yamlData), nil | ||
| } | ||
|
|
||
| func returnKeysString() (string, error) { | ||
| // var err error | ||
| validKeys := configuration.ViperKeys() | ||
|
|
||
| if len(validKeys) == 0 { | ||
| return "", fmt.Errorf("unable to retrieve valid keys") | ||
| } else { | ||
| validKeysJoined := strings.Join(validKeys, "\n- ") | ||
| return "Valid Keys:\n- " + validKeysJoined, nil | ||
| } | ||
| } | ||
|
|
||
| func RunInternalConfigListKeys() (err error) { | ||
| var outputMessageString string | ||
| yamlFlagStr, err := profiles.GetOptionValue(options.ConfigListKeysYamlOption) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if yamlFlagStr == "true" { | ||
| // Output the YAML data as a string | ||
| outputMessageString, err = returnKeysYamlString() | ||
| if err != nil { | ||
erikostien-pingidentity marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return err | ||
| } | ||
| } else { | ||
| // Output data list string | ||
| outputMessageString, err = returnKeysString() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| output.Message(outputMessageString, nil) | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or 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,16 @@ | ||
| package config_internal | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/pingidentity/pingcli/internal/testing/testutils" | ||
| "github.com/pingidentity/pingcli/internal/testing/testutils_viper" | ||
| ) | ||
|
|
||
| // Test RunInternalConfigListKeys function | ||
| func Test_RunInternalConfigListKeys(t *testing.T) { | ||
| testutils_viper.InitVipers(t) | ||
|
|
||
| err := RunInternalConfigListKeys() | ||
| testutils.CheckExpectedError(t, err, nil) | ||
| } | ||
erikostien-pingidentity marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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 hidden or 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 hidden or 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,35 @@ | ||
| package configuration_config | ||
|
|
||
| import ( | ||
| "github.com/pingidentity/pingcli/internal/configuration/options" | ||
| "github.com/pingidentity/pingcli/internal/customtypes" | ||
| "github.com/spf13/pflag" | ||
| ) | ||
|
|
||
| func InitConfigListKeyOptions() { | ||
| initConfigListKeysYAMLOption() | ||
| } | ||
|
|
||
| func initConfigListKeysYAMLOption() { | ||
| cobraParamName := "yaml" | ||
| cobraValue := new(customtypes.Bool) | ||
| defaultValue := customtypes.Bool(false) | ||
|
|
||
| options.ConfigListKeysYamlOption = options.Option{ | ||
| CobraParamName: cobraParamName, | ||
| CobraParamValue: cobraValue, | ||
| DefaultValue: &defaultValue, | ||
| EnvVar: "", // No environment variable | ||
| Flag: &pflag.Flag{ | ||
| Name: cobraParamName, | ||
| Shorthand: "y", | ||
| Usage: "Output configuration keys in YAML format. " + | ||
| "(default false)", | ||
| Value: cobraValue, | ||
| NoOptDefVal: "true", // Make this flag a boolean flag | ||
| }, | ||
| Sensitive: false, | ||
| Type: options.ENUM_BOOL, | ||
| ViperKey: "", // No viper key | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.