- Notifications
You must be signed in to change notification settings - Fork1k
feat(cli): add dynamic completions for ssh command#20171
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -109,6 +109,42 @@ func (r *RootCmd) ssh() *serpent.Command { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
CompletionHandler: func(inv *serpent.Invocation) []string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
client, err := r.InitClient(inv) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return []string{"# Error: Unable to initialize client - " + err.Error()} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
res, err := client.Workspaces(inv.Context(), codersdk.WorkspaceFilter{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Owner: "me", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Could be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return []string{"# Error: Unable to fetch workspaces - " + err.Error()} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var completions []string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for _, ws := range res.Workspaces { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if ws.LatestBuild.Status != codersdk.WorkspaceStatusRunning { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
continue | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines +127 to +129 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Can we show workspaces that are not running? Could be convenient, otherwise you have to start the workspace you want first, and then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var agents []codersdk.WorkspaceAgent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for _, resource := range ws.LatestBuild.Resources { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
agents = append(agents, resource.Agents...) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines +131 to +134 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. If we decide to allow workspaces that are not running, since those will not have agents in the response we would have to add a call to template version resources. We could take inspiration from the function we used to have in Lines 147 to 195 in0b2ba96
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if len(agents) == 1 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
completions = append(completions, ws.Name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. What do you think about always adding this completion? Without the agent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
for _, agent := range agents { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
completions = append(completions, fmt.Sprintf("%s.%s", agent.Name, ws.Name)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
slices.Sort(completions) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return completions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Handler: func(inv *serpent.Invocation) (retErr error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
client, err := r.InitClient(inv) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -2447,3 +2447,103 @@ func tempDirUnixSocket(t *testing.T) string { | ||
return t.TempDir() | ||
} | ||
func TestSSH_Completion(t *testing.T) { | ||
t.Parallel() | ||
t.Run("SingleAgent", func(t *testing.T) { | ||
t.Parallel() | ||
client, workspace, agentToken := setupWorkspaceForAgent(t) | ||
_ = agenttest.New(t, client.URL, agentToken) | ||
coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID) | ||
var stdout bytes.Buffer | ||
inv, root := clitest.New(t, "ssh", "") | ||
inv.Stdout = &stdout | ||
inv.Environ.Set("COMPLETION_MODE", "1") | ||
clitest.SetupConfig(t, client, root) | ||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium) | ||
defer cancel() | ||
err := inv.WithContext(ctx).Run() | ||
// Completion handlers may fail gracefully, so we don't assert error | ||
_ = err | ||
Comment on lines +2470 to +2472 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I am not quite following, why can we not require.NoError(err)? Looks like the handler never errors, but if it did that would be a problem we should catch right? | ||
// For single-agent workspaces, the workspace name should be suggested | ||
// as an alias | ||
output := stdout.String() | ||
t.Logf("Completion output: %q", output) | ||
require.Contains(t, output, workspace.Name) | ||
}) | ||
t.Run("MultiAgent", func(t *testing.T) { | ||
t.Parallel() | ||
client, store := coderdtest.NewWithDatabase(t, nil) | ||
first := coderdtest.CreateFirstUser(t, client) | ||
userClient, user := coderdtest.CreateAnotherUserMutators(t, client, first.OrganizationID, nil, func(r *codersdk.CreateUserRequestWithOrgs) { | ||
r.Username = "multiuser" | ||
}) | ||
// Create a workspace with multiple agents | ||
r := dbfake.WorkspaceBuild(t, store, database.WorkspaceTable{ | ||
Name: "multiworkspace", | ||
OrganizationID: first.OrganizationID, | ||
OwnerID: user.ID, | ||
}).WithAgent(func(agents []*proto.Agent) []*proto.Agent { | ||
return []*proto.Agent{ | ||
{ | ||
Name: "agent1", | ||
Auth: &proto.Agent_Token{}, | ||
}, | ||
{ | ||
Name: "agent2", | ||
Auth: &proto.Agent_Token{}, | ||
}, | ||
} | ||
}).Do() | ||
var stdout bytes.Buffer | ||
inv, root := clitest.New(t, "ssh", "") | ||
inv.Stdout = &stdout | ||
inv.Environ.Set("COMPLETION_MODE", "1") | ||
clitest.SetupConfig(t, userClient, root) | ||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium) | ||
defer cancel() | ||
err := inv.WithContext(ctx).Run() | ||
// Completion handlers may fail gracefully, so we don't assert error | ||
_ = err | ||
Comment on lines +2517 to +2519 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Same here | ||
// For multi-agent workspaces, completions should include agent.workspace format | ||
// but NOT the bare workspace name | ||
output := stdout.String() | ||
t.Logf("Completion output: %q", output) | ||
lines := strings.Split(strings.TrimSpace(output), "\n") | ||
require.NotContains(t, lines, r.Workspace.Name) | ||
require.Contains(t, output, "agent1."+r.Workspace.Name) | ||
require.Contains(t, output, "agent2."+r.Workspace.Name) | ||
}) | ||
t.Run("NetworkError", func(t *testing.T) { | ||
t.Parallel() | ||
var stdout bytes.Buffer | ||
inv, _ := clitest.New(t, "ssh", "") | ||
inv.Stdout = &stdout | ||
inv.Environ.Set("COMPLETION_MODE", "1") | ||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort) | ||
defer cancel() | ||
err := inv.WithContext(ctx).Run() | ||
_ = err | ||
Comment on lines +2542 to +2543 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. And here | ||
output := stdout.String() | ||
t.Logf("Completion output with error: %q", output) | ||
require.Contains(t, output, "# Error:") | ||
}) | ||
} |
Uh oh!
There was an error while loading.Please reload this page.