Repository navigation
feat(workspace): separate display label and SSH host alias from workspace ID #1741
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
18 commits
Select commit
Hold shift + click to select a range
29e427c
feat(workspace): separate display label and SSH host alias from works…
skevetter d315909
refactor(desktop): split workspace presentation helpers to reduce met…
skevetter ed5be1f
refactor(desktop): extract secret protection helpers and mock present…
skevetter 34e8e4a
fix(workspace): satisfy lint on presentation and ssh alias code
skevetter a3d1000
fix(workspace): isolate presentation tests and fix rename e2e flows
skevetter 109a64c
test(desktop): allow subpixel rounding in wizard footer overlap check
skevetter 1b85ba3
chore: merge main into feat/workspace-label-ssh-alias
skevetter 48b246c
fix(workspace): address review findings on alias collisions, locking …
skevetter 1df0dda
fix(workspace): validate derived workspace id after resolve and prefe…
skevetter 51d75e2
fix(workspace): address review findings on ssh parsing, locking, clie…
skevetter d97c85b
fix(workspace): address second round of review findings on ssh lockin…
skevetter 049b184
fix(workspace): address third round of review findings
skevetter 5178006
chore: merge main into feat/workspace-label-ssh-alias
skevetter ad035bd
fix(workspace): address fourth round of review findings
skevetter 8580207
feat(workspace): ownership-aware ssh blocks, staged import validation…
skevetter 06ec1f6
chore: merge main into feat/workspace-label-ssh-alias
skevetter 54afdf3
fix(workspace): claim legacy tunnel blocks and serialize import with …
skevetter 49ba931
fix(workspace): stamp ownership when an alias edit claims a legacy ss…
skevetter 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
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,212 @@ | ||
| package workspace | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
|
|
||
| "github.com/devsy-org/devsy/cmd/completion" | ||
| "github.com/devsy-org/devsy/cmd/flags" | ||
| "github.com/devsy-org/devsy/pkg/clierr" | ||
| "github.com/devsy-org/devsy/pkg/config" | ||
| cliflags "github.com/devsy-org/devsy/pkg/flags" | ||
| "github.com/devsy-org/devsy/pkg/flags/names" | ||
| "github.com/devsy-org/devsy/pkg/output" | ||
| "github.com/devsy-org/devsy/pkg/table" | ||
| workspace "github.com/devsy-org/devsy/pkg/workspace" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type editPresentationFunc func(workspace.PresentationRequest) (*workspace.PresentationResult, error) | ||
|
|
||
| type EditCmd struct { | ||
| *flags.GlobalFlags | ||
|
|
||
| DisplayName string | ||
| SSHHostAlias string | ||
| ExpectedUID string | ||
| ExpectedDisplayName string | ||
| ExpectedSSHHostAlias string | ||
|
|
||
| displayNameSet bool | ||
| sshHostAliasSet bool | ||
| expectedDisplayNameSet bool | ||
| expectedSSHHostAliasSet bool | ||
|
|
||
| edit editPresentationFunc | ||
| } | ||
|
|
||
| func NewEditCmd(globalFlags *flags.GlobalFlags) *cobra.Command { | ||
| return newEditCmd(globalFlags, workspace.EditPresentation) | ||
| } | ||
|
|
||
| func newEditCmd(globalFlags *flags.GlobalFlags, edit editPresentationFunc) *cobra.Command { | ||
| cmd := &EditCmd{ | ||
| GlobalFlags: globalFlags, | ||
| edit: edit, | ||
| } | ||
|
|
||
| editCmd := &cobra.Command{ | ||
| Use: "edit <workspace> [--display-name <label>] [--ssh-host-alias <alias>]", | ||
| Short: "Edit a workspace's display label and preferred SSH host alias", | ||
| Long: `Edit how a workspace is presented without changing its ID. | ||
|
|
||
| --display-name sets a local human-facing label. --ssh-host-alias sets an | ||
| additional SSH host name (without the .devsy suffix) that resolves to the | ||
| workspace; the canonical <id>.devsy host keeps working. Pass an empty value to | ||
| clear either override. Label edits never touch the SSH config.`, | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: func(cobraCmd *cobra.Command, args []string) error { | ||
| cmd.displayNameSet = cobraCmd.Flags().Changed(names.DisplayName) | ||
| cmd.sshHostAliasSet = cobraCmd.Flags().Changed(names.SSHHostAlias) | ||
| cmd.expectedDisplayNameSet = cobraCmd.Flags().Changed(names.ExpectedDisplayName) | ||
| cmd.expectedSSHHostAliasSet = cobraCmd.Flags().Changed(names.ExpectedSSHHostAlias) | ||
| return cmd.Run(cobraCmd.OutOrStdout(), args[0]) | ||
| }, | ||
| ValidArgsFunction: func( | ||
| rootCmd *cobra.Command, args []string, toComplete string, | ||
| ) ([]string, cobra.ShellCompDirective) { | ||
| if len(args) == 0 { | ||
| return completion.GetWorkspaceSuggestions( | ||
| rootCmd, | ||
| completion.SuggestionRequest{ | ||
| Context: cmd.Context, | ||
| Provider: cmd.Provider, | ||
| Args: args, | ||
| ToComplete: toComplete, | ||
| Owner: cmd.Owner, | ||
| }, | ||
| ) | ||
| } | ||
| return nil, cobra.ShellCompDirectiveNoFileComp | ||
| }, | ||
| } | ||
|
|
||
| cliflags.Add( | ||
| editCmd, | ||
| cliflags.String(&cmd.DisplayName, names.DisplayName, "", | ||
| "Local display label for the workspace (empty clears the label)"), | ||
| cliflags.String(&cmd.SSHHostAlias, names.SSHHostAlias, "", | ||
| "Preferred SSH host alias without the .devsy suffix (empty clears the alias)"), | ||
| cliflags.String(&cmd.ExpectedUID, names.ExpectedUID, "", | ||
| "Fail unless the workspace still has this UID").Hidden(), | ||
| cliflags.String(&cmd.ExpectedDisplayName, names.ExpectedDisplayName, "", | ||
| "Fail unless the current display label matches").Hidden(), | ||
| cliflags.String(&cmd.ExpectedSSHHostAlias, names.ExpectedSSHHostAlias, "", | ||
| "Fail unless the current SSH host alias matches").Hidden(), | ||
| ) | ||
| return editCmd | ||
| } | ||
|
|
||
| func (cmd *EditCmd) Run(out io.Writer, id string) error { | ||
| if !cmd.displayNameSet && !cmd.sshHostAliasSet { | ||
| return fmt.Errorf( | ||
| "at least one of --%s or --%s is required", | ||
| names.DisplayName, | ||
| names.SSHHostAlias, | ||
| ) | ||
| } | ||
|
|
||
| mode, err := output.ResolveMode(cmd.ResultFormat) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| devsyConfig, err := config.LoadConfig(cmd.Context, cmd.Provider) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| result, err := cmd.edit(cmd.request(devsyConfig, id)) | ||
| if err != nil { | ||
| return presentationCLIError(err) | ||
| } | ||
|
|
||
| return renderEdit(out, mode, result) | ||
| } | ||
|
|
||
| func (cmd *EditCmd) request( | ||
| devsyConfig *config.Config, | ||
| id string, | ||
| ) workspace.PresentationRequest { | ||
| req := workspace.PresentationRequest{ | ||
| DevsyConfig: devsyConfig, | ||
| ID: id, | ||
| ExpectedUID: cmd.ExpectedUID, | ||
| } | ||
| if cmd.displayNameSet { | ||
| req.DisplayName = &cmd.DisplayName | ||
| } | ||
| if cmd.sshHostAliasSet { | ||
| req.SSHHostAlias = &cmd.SSHHostAlias | ||
| } | ||
| if cmd.expectedDisplayNameSet { | ||
| req.ExpectedDisplayName = &cmd.ExpectedDisplayName | ||
| } | ||
| if cmd.expectedSSHHostAliasSet { | ||
| req.ExpectedSSHHostAlias = &cmd.ExpectedSSHHostAlias | ||
| } | ||
| return req | ||
| } | ||
|
|
||
| func presentationCLIError(err error) error { | ||
| presentationErr, ok := errors.AsType[*workspace.PresentationError](err) | ||
| if !ok { | ||
| return err | ||
| } | ||
|
|
||
| var errContext map[string]string | ||
| if presentationErr.Field != "" || presentationErr.Conflict != "" { | ||
| errContext = map[string]string{} | ||
| } | ||
| if presentationErr.Field != "" { | ||
| errContext["field"] = presentationErr.Field | ||
| } | ||
| if presentationErr.Conflict != "" { | ||
| errContext["conflict"] = presentationErr.Conflict | ||
| } | ||
|
|
||
| message := presentationErr.Message | ||
| if message == "" { | ||
| message = presentationErr.Error() | ||
| } | ||
| if joined := err.Error(); joined != presentationErr.Error() { | ||
| message = joined | ||
| } | ||
|
|
||
| return &clierr.CLIError{ | ||
| Code: clierr.Code(presentationErr.Code), | ||
| Message: message, | ||
| Context: errContext, | ||
| } | ||
| } | ||
|
|
||
| func renderEdit(out io.Writer, mode string, result *workspace.PresentationResult) error { | ||
| switch mode { | ||
| case output.ModeJSON: | ||
| encoded, err := json.Marshal(result) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| _, err = fmt.Fprint(out, string(encoded)) | ||
| return err | ||
| case output.ModePlain: | ||
| return writeEditRows(out, result) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func writeEditRows(out io.Writer, result *workspace.PresentationResult) error { | ||
| rows := [][]string{ | ||
| {"ID", result.ID}, | ||
| {"Label", result.EffectiveDisplayName}, | ||
| {"SSH Host", result.PreferredSSHHost}, | ||
| } | ||
| if result.CanonicalSSHHost != result.PreferredSSHHost { | ||
| rows = append(rows, []string{"Canonical SSH Host", result.CanonicalSSHHost}) | ||
| } | ||
| _, err := fmt.Fprintln(out, table.Render([]string{"Field", "Value"}, rows)) | ||
| return err | ||
| } |
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.