-
Notifications
You must be signed in to change notification settings - Fork 1
Refactor pc config to support key-based configuration and commands
#90
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
Open
austin-denoble
wants to merge
8
commits into
main
Choose a base branch
from
adenoble/improve-configuration-path
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0af76ca
Refactor `pc config` to use key-based get/set/list/describe/unset com…
austin-denoble 00eb59a
Fix onChange contract in `pc config set` and `unset`
austin-denoble 62e6b69
Refactor pc config commands for testability and add unit tests
austin-denoble 8b0af44
correct bug in flow from config input normalization to persistence to…
austin-denoble 5bfe55c
use msg.FailJSON in get, describe, and list config commands
austin-denoble f499f0a
make sure visibleKeys() preserves the ordering of the listed keys
austin-denoble 1a6a4f9
clean up confusing aliasing around ValidValues and how things are act…
austin-denoble 8936497
clean up error message when using unset and the value is unchanged
austin-denoble 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package config | ||
|
|
||
| import "context" | ||
|
|
||
| // mockConfigService implements ConfigService for unit tests. | ||
| // Each field controls what the corresponding method returns. | ||
| // The last* fields record the arguments of the most recent call. | ||
| type mockConfigService struct { | ||
| // Get | ||
| getValue string | ||
| getSensitive bool | ||
| getErr error | ||
| lastGetKey string | ||
|
|
||
| // Set | ||
| setLines []string | ||
| setErr error | ||
| lastSetKey string | ||
| lastSetValue string | ||
|
|
||
| // Unset | ||
| unsetLines []string | ||
| unsetErr error | ||
| lastUnsetKey string | ||
|
|
||
| // List | ||
| listResult []ConfigEntry | ||
|
|
||
| // Describe | ||
| describeResult ConfigDescription | ||
| describeErr error | ||
| lastDescribeKey string | ||
| } | ||
|
|
||
| func (m *mockConfigService) Get(key string) (string, bool, error) { | ||
| m.lastGetKey = key | ||
| return m.getValue, m.getSensitive, m.getErr | ||
| } | ||
|
|
||
| func (m *mockConfigService) Set(ctx context.Context, key, value string) ([]string, error) { | ||
| m.lastSetKey = key | ||
| m.lastSetValue = value | ||
| return m.setLines, m.setErr | ||
| } | ||
|
|
||
| func (m *mockConfigService) Unset(ctx context.Context, key string) ([]string, error) { | ||
| m.lastUnsetKey = key | ||
| return m.unsetLines, m.unsetErr | ||
| } | ||
|
|
||
| func (m *mockConfigService) List() []ConfigEntry { | ||
| return m.listResult | ||
| } | ||
|
|
||
| func (m *mockConfigService) Describe(key string) (ConfigDescription, error) { | ||
| m.lastDescribeKey = key | ||
| return m.describeResult, m.describeErr | ||
| } |
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,98 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "github.com/pinecone-io/cli/internal/pkg/utils/exit" | ||
| "github.com/pinecone-io/cli/internal/pkg/utils/help" | ||
| "github.com/pinecone-io/cli/internal/pkg/utils/msg" | ||
| "github.com/pinecone-io/cli/internal/pkg/utils/presenters" | ||
| "github.com/pinecone-io/cli/internal/pkg/utils/text" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type DescribeCmdOptions struct { | ||
| reveal bool | ||
| json bool | ||
| } | ||
|
|
||
| func NewDescribeCmd() *cobra.Command { | ||
| options := DescribeCmdOptions{} | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "describe <key>", | ||
| Short: "Show detailed information about a configuration setting", | ||
| Example: help.Examples(` | ||
| pc config describe api-key | ||
| pc config describe environment | ||
| pc config describe color --json | ||
| `), | ||
| Args: cobra.ExactArgs(1), | ||
| ValidArgs: visibleKeys(), | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| svc := newDefaultConfigService() | ||
| if err := runDescribeCmd(svc, args[0], options); err != nil { | ||
| msg.FailJSON(options.json, "%s", err) | ||
| exit.ErrorMsg(err.Error()) | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().BoolVar(&options.reveal, "reveal", false, "Reveal the full value for sensitive settings like api-key") | ||
| cmd.Flags().BoolVarP(&options.json, "json", "j", false, "Output as JSON") | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func runDescribeCmd(svc ConfigService, keyName string, opts DescribeCmdOptions) error { | ||
| // --json output for the describe command | ||
| type describeOutput struct { | ||
| Key string `json:"key"` | ||
| Value string `json:"value"` | ||
| Description string `json:"description"` | ||
| LongDescription string `json:"long_description,omitempty"` | ||
| Sensitive bool `json:"sensitive"` | ||
| ValidValues []string `json:"valid_values,omitempty"` | ||
| } | ||
|
|
||
| desc, err := svc.Describe(keyName) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| value := desc.Value | ||
| if desc.Sensitive && !opts.reveal { | ||
| value = presenters.MaskHeadTail(value, 4, 4) | ||
| } | ||
|
|
||
| if opts.json { | ||
| fmt.Fprintln(os.Stdout, text.IndentJSON(describeOutput{ | ||
| Key: desc.Key, | ||
| Value: value, | ||
| Description: desc.Description, | ||
| LongDescription: desc.LongDescription, | ||
| Sensitive: desc.Sensitive, | ||
| ValidValues: desc.ValidValues, | ||
| })) | ||
| return nil | ||
| } | ||
|
|
||
| w := presenters.NewTabWriter() | ||
| fmt.Fprintf(w, "KEY\t%s\n", desc.Key) | ||
| fmt.Fprintf(w, "VALUE\t%s\n", displayValue(value)) | ||
| fmt.Fprintf(w, "SENSITIVE\t%s\n", text.BoolToString(desc.Sensitive)) | ||
| if len(desc.ValidValues) > 0 { | ||
| fmt.Fprintf(w, "VALID VALUES\t%s\n", strings.Join(desc.ValidValues, ", ")) | ||
| } | ||
| fmt.Fprintf(w, "DESCRIPTION\t%s\n", desc.Description) | ||
| w.Flush() | ||
|
|
||
| if desc.LongDescription != "" { | ||
| fmt.Fprintln(os.Stdout) | ||
| fmt.Fprintln(os.Stdout, desc.LongDescription) | ||
| } | ||
|
|
||
| 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,93 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/pinecone-io/cli/internal/pkg/cli/testutils" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func Test_runDescribeCmd_ReturnsErrorOnUnknownKey(t *testing.T) { | ||
| svc := &mockConfigService{describeErr: errors.New("unknown config key")} | ||
|
|
||
| err := runDescribeCmd(svc, "bad-key", DescribeCmdOptions{}) | ||
|
|
||
| assert.Error(t, err) | ||
| assert.Equal(t, "bad-key", svc.lastDescribeKey) | ||
| } | ||
|
|
||
| func Test_runDescribeCmd_TabularOutput(t *testing.T) { | ||
| svc := &mockConfigService{ | ||
| describeResult: ConfigDescription{ | ||
| Key: "environment", | ||
| Value: "production", | ||
| Description: "Pinecone environment", | ||
| Sensitive: false, | ||
| ValidValues: []string{"production", "staging"}, | ||
| }, | ||
| } | ||
|
|
||
| out := testutils.CaptureStdout(t, func() { | ||
| err := runDescribeCmd(svc, "environment", DescribeCmdOptions{}) | ||
| assert.NoError(t, err) | ||
| }) | ||
|
|
||
| assert.Contains(t, out, "environment") | ||
| assert.Contains(t, out, "production") | ||
| } | ||
|
|
||
| func Test_runDescribeCmd_JSONOutput(t *testing.T) { | ||
| svc := &mockConfigService{ | ||
| describeResult: ConfigDescription{ | ||
| Key: "environment", | ||
| Value: "production", | ||
| Description: "Pinecone environment", | ||
| Sensitive: false, | ||
| ValidValues: []string{"production", "staging"}, | ||
| }, | ||
| } | ||
|
|
||
| out := testutils.CaptureStdout(t, func() { | ||
| err := runDescribeCmd(svc, "environment", DescribeCmdOptions{json: true}) | ||
| assert.NoError(t, err) | ||
| }) | ||
|
|
||
| assert.Contains(t, out, `"environment"`) | ||
| assert.Contains(t, out, `"production"`) | ||
| assert.Contains(t, out, `"valid_values"`) | ||
| } | ||
|
|
||
| func Test_runDescribeCmd_MasksSensitiveKeyInJSON(t *testing.T) { | ||
| svc := &mockConfigService{ | ||
| describeResult: ConfigDescription{ | ||
| Key: "api-key", | ||
| Value: "supersecretvalue", | ||
| Sensitive: true, | ||
| }, | ||
| } | ||
|
|
||
| out := testutils.CaptureStdout(t, func() { | ||
| err := runDescribeCmd(svc, "api-key", DescribeCmdOptions{json: true, reveal: false}) | ||
| assert.NoError(t, err) | ||
| }) | ||
|
|
||
| assert.NotContains(t, out, "supersecretvalue") | ||
| } | ||
|
|
||
| func Test_runDescribeCmd_RevealsSensitiveKeyInJSON(t *testing.T) { | ||
| svc := &mockConfigService{ | ||
| describeResult: ConfigDescription{ | ||
| Key: "api-key", | ||
| Value: "supersecretvalue", | ||
| Sensitive: true, | ||
| }, | ||
| } | ||
|
|
||
| out := testutils.CaptureStdout(t, func() { | ||
| err := runDescribeCmd(svc, "api-key", DescribeCmdOptions{json: true, reveal: true}) | ||
| assert.NoError(t, err) | ||
| }) | ||
|
|
||
| assert.Contains(t, out, "supersecretvalue") | ||
| } |
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,73 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
|
|
||
| "github.com/pinecone-io/cli/internal/pkg/utils/exit" | ||
| "github.com/pinecone-io/cli/internal/pkg/utils/help" | ||
| "github.com/pinecone-io/cli/internal/pkg/utils/msg" | ||
| "github.com/pinecone-io/cli/internal/pkg/utils/presenters" | ||
| "github.com/pinecone-io/cli/internal/pkg/utils/style" | ||
| "github.com/pinecone-io/cli/internal/pkg/utils/text" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type GetCmdOptions struct { | ||
| reveal bool | ||
| json bool | ||
| } | ||
|
|
||
| func NewGetCmd() *cobra.Command { | ||
| options := GetCmdOptions{} | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "get <key>", | ||
| Short: "Get the current value of a configuration setting", | ||
| Example: help.Examples(` | ||
| pc config get api-key | ||
| pc config get api-key --reveal | ||
| pc config get environment | ||
| pc config get color | ||
| `), | ||
| Args: cobra.ExactArgs(1), | ||
| ValidArgs: visibleKeys(), | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| svc := newDefaultConfigService() | ||
| if err := runGetCmd(svc, args[0], options); err != nil { | ||
| msg.FailJSON(options.json, "%s", err) | ||
| exit.ErrorMsg(err.Error()) | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| cmd.Flags().BoolVar(&options.reveal, "reveal", false, "Reveal the full value for sensitive settings like api-key") | ||
| cmd.Flags().BoolVarP(&options.json, "json", "j", false, "Output as JSON") | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func runGetCmd(svc ConfigService, keyName string, opts GetCmdOptions) error { | ||
| // --json output for the get command | ||
| type getOutput struct { | ||
| Key string `json:"key"` | ||
| Value string `json:"value"` | ||
| } | ||
|
|
||
| value, sensitive, err := svc.Get(keyName) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if sensitive && !opts.reveal { | ||
| value = presenters.MaskHeadTail(value, 4, 4) | ||
| } | ||
|
|
||
| if opts.json { | ||
| fmt.Fprintln(os.Stdout, text.IndentJSON(getOutput{Key: keyName, Value: value})) | ||
| return nil | ||
| } | ||
|
|
||
| msg.InfoMsg("%s: %s", style.Emphasis(keyName), displayValue(value)) | ||
| 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
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.