Skip to content

Commit

Permalink
[LocalCLI] add completion for --class and --editor flags (#19019)
Browse files Browse the repository at this point in the history
* init

* 1

* 1

* Remove completion in help

* 💄

* Address feedback

* add completion to workspace up
  • Loading branch information
mustard-mh authored Nov 7, 2023
1 parent 8feb819 commit d7eae21
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ tasks:
gp env -u GCP_ADC_FILE
fi
exit 0
- name: Install `gitpod` CLI
command: |
leeway run components/local-app:install-cli
leeway run components/local-app:cli-completion
exit 0
# This task takes care of configuring your workspace so it can manage and interact
# with preview environments.
- name: Preview environment configuration
Expand Down
12 changes: 12 additions & 0 deletions components/local-app/BUILD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@ packages:
image:
- ${imageRepoBase}/local-app:${version}
- ${imageRepoBase}/local-app:commit-${__git_commit}

scripts:
- name: install-cli
description: "Install gitpod-cli as `gitpod` command and add auto-completion. Usage: '. $(leeway run components/local-app:install-cli)'"
script: |
go build -o gitpod ./main/gitpod-cli
sudo mv gitpod /usr/bin/gitpod
sudo chmod +x /usr/bin/gitpod
- name: cli-completion
description: "Add completion of gitpod-cli to bash-completion. Usage: '. $(leeway run components/local-app:cli-completion)'"
script: |
sudo /usr/bin/gitpod completion bash | sudo tee /usr/share/bash-completion/completions/gitpod > /dev/null
16 changes: 15 additions & 1 deletion components/local-app/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# local-app
# local-app

## gitpod-cli

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

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.

#### in Gitpod workspace

[![Open in Gitpod](https://www.gitpod.io/svg/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/gitpod-io/gitpod)

You will have gitpod-cli ready as `gitpod` in your CDE (Cloud Development Environments) workspace, will commands below you can install again easily.

```
# Install as `gitpod` again
leeway run components/local-app:install-cli
# Add completion again
leeway run components/local-app:cli-completion
```

## local-app

**Beware**: this is very much work in progress and will likely break things.
Expand Down
7 changes: 7 additions & 0 deletions components/local-app/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,13 @@ var rootTestingOpts struct {
WriterOut io.Writer
}

var clientCache *client.Gitpod

func getGitpodClient(ctx context.Context) (*client.Gitpod, error) {
// There will be only one client in a command context right now
if clientCache != nil {
return clientCache, nil
}
cfg := config.FromContext(ctx)
gpctx, err := cfg.GetActiveContext()
if err != nil {
Expand Down Expand Up @@ -154,6 +160,7 @@ func getGitpodClient(ctx context.Context) (*client.Gitpod, error) {
if err != nil {
return nil, err
}
clientCache = res

return res, nil
}
Expand Down
43 changes: 43 additions & 0 deletions components/local-app/cmd/workspace-create.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,53 @@ var workspaceCreateOpts struct {
Editor string
}

func classCompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
ctx := cmd.Context()
gitpod, err := getGitpodClient(ctx)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
resp, err := gitpod.Workspaces.ListWorkspaceClasses(ctx, connect.NewRequest(&v1.ListWorkspaceClassesRequest{}))
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
items := resp.Msg.GetResult()
completionStr := []string{}
for _, cls := range items {
defaultDesc := ""
if cls.IsDefault {
defaultDesc = "(default)"
}
completionStr = append(completionStr, fmt.Sprintf("%s\t%s%s - %s", cls.Id, cls.DisplayName, defaultDesc, cls.Description))
}
return completionStr, cobra.ShellCompDirectiveNoFileComp
}

func editorCompletionFunc(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
ctx := cmd.Context()
gitpod, err := getGitpodClient(ctx)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
resp, err := gitpod.Editors.ListEditorOptions(ctx, connect.NewRequest(&v1.ListEditorOptionsRequest{}))
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
items := resp.Msg.GetResult()
completionStr := []string{}
for _, editor := range items {
completionStr = append(completionStr, fmt.Sprintf("%s\t%s", editor.Id, editor.Title))
}
return completionStr, cobra.ShellCompDirectiveNoFileComp
}

func init() {
workspaceCmd.AddCommand(workspaceCreateCmd)
addWorkspaceStartOptions(workspaceCreateCmd, &workspaceCreateOpts.StartOpts)

workspaceCreateCmd.Flags().StringVar(&workspaceCreateOpts.WorkspaceClass, "class", "", "the workspace class")
workspaceCreateCmd.Flags().StringVar(&workspaceCreateOpts.Editor, "editor", "code", "the editor to use")

_ = workspaceCreateCmd.RegisterFlagCompletionFunc("class", classCompletionFunc)
_ = workspaceCreateCmd.RegisterFlagCompletionFunc("editor", editorCompletionFunc)
}
3 changes: 3 additions & 0 deletions components/local-app/cmd/workspace-up.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,7 @@ func init() {

workspaceUpCmd.Flags().StringVar(&workspaceCreateOpts.WorkspaceClass, "class", "", "the workspace class")
workspaceUpCmd.Flags().StringVar(&workspaceCreateOpts.Editor, "editor", "code", "the editor to use")

_ = workspaceUpCmd.RegisterFlagCompletionFunc("class", classCompletionFunc)
_ = workspaceUpCmd.RegisterFlagCompletionFunc("editor", editorCompletionFunc)
}

0 comments on commit d7eae21

Please sign in to comment.