Skip to content

Commit d7eae21

Browse files
authored
[LocalCLI] add completion for --class and --editor flags (#19019)
* init * 1 * 1 * Remove completion in help * 💄 * Address feedback * add completion to workspace up
1 parent 8feb819 commit d7eae21

File tree

6 files changed

+85
-1
lines changed

6 files changed

+85
-1
lines changed

.gitpod.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ tasks:
4242
gp env -u GCP_ADC_FILE
4343
fi
4444
exit 0
45+
- name: Install `gitpod` CLI
46+
command: |
47+
leeway run components/local-app:install-cli
48+
leeway run components/local-app:cli-completion
49+
exit 0
4550
# This task takes care of configuring your workspace so it can manage and interact
4651
# with preview environments.
4752
- name: Preview environment configuration

components/local-app/BUILD.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,15 @@ packages:
1313
image:
1414
- ${imageRepoBase}/local-app:${version}
1515
- ${imageRepoBase}/local-app:commit-${__git_commit}
16+
17+
scripts:
18+
- name: install-cli
19+
description: "Install gitpod-cli as `gitpod` command and add auto-completion. Usage: '. $(leeway run components/local-app:install-cli)'"
20+
script: |
21+
go build -o gitpod ./main/gitpod-cli
22+
sudo mv gitpod /usr/bin/gitpod
23+
sudo chmod +x /usr/bin/gitpod
24+
- name: cli-completion
25+
description: "Add completion of gitpod-cli to bash-completion. Usage: '. $(leeway run components/local-app:cli-completion)'"
26+
script: |
27+
sudo /usr/bin/gitpod completion bash | sudo tee /usr/share/bash-completion/completions/gitpod > /dev/null

components/local-app/README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# local-app
1+
# local-app
22

33
## gitpod-cli
44

@@ -27,6 +27,20 @@ Start by logging in with `gitpod login`, which will also create a default contex
2727

2828
To develop the CLI with Gitpod, you can run it just like locally, but in Gitpod workspaces, a browser and a keyring are not available. To log in despite these limitations, provide a PAT via the `GITPOD_TOKEN` environment variable, or use the `--token` flag with the login command.
2929

30+
#### in Gitpod workspace
31+
32+
[![Open in Gitpod](https://www.gitpod.io/svg/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/gitpod-io/gitpod)
33+
34+
You will have gitpod-cli ready as `gitpod` in your CDE (Cloud Development Environments) workspace, will commands below you can install again easily.
35+
36+
```
37+
# Install as `gitpod` again
38+
leeway run components/local-app:install-cli
39+
40+
# Add completion again
41+
leeway run components/local-app:cli-completion
42+
```
43+
3044
## local-app
3145

3246
**Beware**: this is very much work in progress and will likely break things.

components/local-app/cmd/root.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,13 @@ var rootTestingOpts struct {
101101
WriterOut io.Writer
102102
}
103103

104+
var clientCache *client.Gitpod
105+
104106
func getGitpodClient(ctx context.Context) (*client.Gitpod, error) {
107+
// There will be only one client in a command context right now
108+
if clientCache != nil {
109+
return clientCache, nil
110+
}
105111
cfg := config.FromContext(ctx)
106112
gpctx, err := cfg.GetActiveContext()
107113
if err != nil {
@@ -154,6 +160,7 @@ func getGitpodClient(ctx context.Context) (*client.Gitpod, error) {
154160
if err != nil {
155161
return nil, err
156162
}
163+
clientCache = res
157164

158165
return res, nil
159166
}

components/local-app/cmd/workspace-create.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,53 @@ var workspaceCreateOpts struct {
143143
Editor string
144144
}
145145

146+
func classCompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
147+
ctx := cmd.Context()
148+
gitpod, err := getGitpodClient(ctx)
149+
if err != nil {
150+
return nil, cobra.ShellCompDirectiveError
151+
}
152+
resp, err := gitpod.Workspaces.ListWorkspaceClasses(ctx, connect.NewRequest(&v1.ListWorkspaceClassesRequest{}))
153+
if err != nil {
154+
return nil, cobra.ShellCompDirectiveError
155+
}
156+
items := resp.Msg.GetResult()
157+
completionStr := []string{}
158+
for _, cls := range items {
159+
defaultDesc := ""
160+
if cls.IsDefault {
161+
defaultDesc = "(default)"
162+
}
163+
completionStr = append(completionStr, fmt.Sprintf("%s\t%s%s - %s", cls.Id, cls.DisplayName, defaultDesc, cls.Description))
164+
}
165+
return completionStr, cobra.ShellCompDirectiveNoFileComp
166+
}
167+
168+
func editorCompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
169+
ctx := cmd.Context()
170+
gitpod, err := getGitpodClient(ctx)
171+
if err != nil {
172+
return nil, cobra.ShellCompDirectiveError
173+
}
174+
resp, err := gitpod.Editors.ListEditorOptions(ctx, connect.NewRequest(&v1.ListEditorOptionsRequest{}))
175+
if err != nil {
176+
return nil, cobra.ShellCompDirectiveError
177+
}
178+
items := resp.Msg.GetResult()
179+
completionStr := []string{}
180+
for _, editor := range items {
181+
completionStr = append(completionStr, fmt.Sprintf("%s\t%s", editor.Id, editor.Title))
182+
}
183+
return completionStr, cobra.ShellCompDirectiveNoFileComp
184+
}
185+
146186
func init() {
147187
workspaceCmd.AddCommand(workspaceCreateCmd)
148188
addWorkspaceStartOptions(workspaceCreateCmd, &workspaceCreateOpts.StartOpts)
149189

150190
workspaceCreateCmd.Flags().StringVar(&workspaceCreateOpts.WorkspaceClass, "class", "", "the workspace class")
151191
workspaceCreateCmd.Flags().StringVar(&workspaceCreateOpts.Editor, "editor", "code", "the editor to use")
192+
193+
_ = workspaceCreateCmd.RegisterFlagCompletionFunc("class", classCompletionFunc)
194+
_ = workspaceCreateCmd.RegisterFlagCompletionFunc("editor", editorCompletionFunc)
152195
}

components/local-app/cmd/workspace-up.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,7 @@ func init() {
291291

292292
workspaceUpCmd.Flags().StringVar(&workspaceCreateOpts.WorkspaceClass, "class", "", "the workspace class")
293293
workspaceUpCmd.Flags().StringVar(&workspaceCreateOpts.Editor, "editor", "code", "the editor to use")
294+
295+
_ = workspaceUpCmd.RegisterFlagCompletionFunc("class", classCompletionFunc)
296+
_ = workspaceUpCmd.RegisterFlagCompletionFunc("editor", editorCompletionFunc)
294297
}

0 commit comments

Comments
 (0)