-
Notifications
You must be signed in to change notification settings - Fork 92
feat(context): allow renaming contexts #1115
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
3 commits
Select commit
Hold shift + click to select a range
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,29 @@ | ||
| ## hcloud context rename | ||
|
|
||
| Rename a context | ||
|
|
||
| ``` | ||
| hcloud context rename <context> <name> | ||
| ``` | ||
|
|
||
| ### Options | ||
|
|
||
| ``` | ||
| -h, --help help for rename | ||
| ``` | ||
|
|
||
| ### Options inherited from parent commands | ||
|
|
||
| ``` | ||
| --config string Config file path (default "~/.config/hcloud/cli.toml") | ||
| --context string Currently active context | ||
| --debug Enable debug output | ||
| --debug-file string File to write debug output to | ||
| --endpoint string Hetzner Cloud API endpoint (default "https://api.hetzner.cloud/v1") | ||
| --poll-interval duration Interval at which to poll information, for example action progress (default 500ms) | ||
| --quiet If true, only print error messages | ||
| ``` | ||
|
|
||
| ### SEE ALSO | ||
|
|
||
| * [hcloud context](hcloud_context.md) - Manage contexts |
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,45 @@ | ||
| package context | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/hetznercloud/cli/internal/cmd/cmpl" | ||
| "github.com/hetznercloud/cli/internal/cmd/util" | ||
| "github.com/hetznercloud/cli/internal/state" | ||
| "github.com/hetznercloud/cli/internal/state/config" | ||
| ) | ||
|
|
||
| func NewRenameCommand(s state.State) *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "rename <context> <name>", | ||
| Short: "Rename a context", | ||
| Args: util.Validate, | ||
| ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidates(config.ContextNames(s.Config())...)), | ||
| TraverseChildren: true, | ||
| DisableFlagsInUseLine: true, | ||
| SilenceUsage: true, | ||
| RunE: state.Wrap(s, runRename), | ||
| } | ||
| return cmd | ||
| } | ||
|
|
||
| func runRename(s state.State, _ *cobra.Command, args []string) error { | ||
| originalName, newName := args[0], args[1] | ||
| cfg := s.Config() | ||
| context := config.ContextByName(cfg, originalName) | ||
| if context == nil { | ||
| return fmt.Errorf("context not found: %v", originalName) | ||
| } | ||
| isActive := cfg.ActiveContext() == context | ||
| if config.ContextByName(cfg, newName) != nil { | ||
| return fmt.Errorf("context with name %v already exists", newName) | ||
| } | ||
| config.RenameContext(context, newName) | ||
| if isActive { | ||
| // re-set the active context to ensure the name is updated | ||
| cfg.SetActiveContext(context) | ||
| } | ||
| return cfg.Write(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,99 @@ | ||
| package context_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/hetznercloud/cli/internal/cmd/context" | ||
| "github.com/hetznercloud/cli/internal/testutil" | ||
| ) | ||
|
|
||
| func TestRename(t *testing.T) { | ||
| testConfig := ` | ||
| active_context = "my-context" | ||
|
|
||
| [[contexts]] | ||
| name = "my-context" | ||
| token = "super secret token" | ||
|
|
||
| [[contexts]] | ||
| name = "my-other-context" | ||
| token = "super secret token" | ||
| ` | ||
|
|
||
| type testCase struct { | ||
| name string | ||
| args []string | ||
| config string | ||
| err string | ||
| expErr string | ||
| expOut string | ||
| } | ||
|
|
||
| testCases := []testCase{ | ||
| { | ||
| name: "rename active context", | ||
| args: []string{"my-context", "my-renamed-context"}, | ||
| config: testConfig, | ||
| expOut: `active_context = "my-renamed-context" | ||
|
|
||
| [[contexts]] | ||
| name = "my-renamed-context" | ||
| token = "super secret token" | ||
|
|
||
| [[contexts]] | ||
| name = "my-other-context" | ||
| token = "super secret token" | ||
| `, | ||
| }, | ||
| { | ||
| name: "rename inactive context", | ||
| args: []string{"my-other-context", "my-other-renamed-context"}, | ||
| config: testConfig, | ||
| expOut: `active_context = "my-context" | ||
|
|
||
| [[contexts]] | ||
| name = "my-context" | ||
| token = "super secret token" | ||
|
|
||
| [[contexts]] | ||
| name = "my-other-renamed-context" | ||
| token = "super secret token" | ||
| `, | ||
| }, | ||
| { | ||
| name: "rename non-existing context", | ||
| args: []string{"non-existing-context", "non-existing-renamed-context"}, | ||
| config: testConfig, | ||
| err: "context not found: non-existing-context", | ||
| expErr: "Error: context not found: non-existing-context\n", | ||
| }, | ||
| { | ||
| name: "rename to existing context", | ||
| args: []string{"my-context", "my-other-context"}, | ||
| config: testConfig, | ||
| err: "context with name my-other-context already exists", | ||
| expErr: "Error: context with name my-other-context already exists\n", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range testCases { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| fx := testutil.NewFixtureWithConfigFile(t, []byte(tt.config)) | ||
| defer fx.Finish() | ||
|
|
||
| cmd := context.NewRenameCommand(fx.State()) | ||
| out, errOut, err := fx.Run(cmd, tt.args) | ||
|
|
||
| if tt.err == "" { | ||
| require.NoError(t, err) | ||
| } else { | ||
| require.EqualError(t, err, tt.err) | ||
| } | ||
| assert.Equal(t, tt.expErr, errOut) | ||
| assert.Equal(t, tt.expOut, out) | ||
| }) | ||
| } | ||
| } |
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
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.