Skip to content
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

[LocalCLI] add completion for --class and --editor flags #19019

Merged
merged 7 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we do this automatically in the .gitpod.yml file for the initial install? Would maybe help with testing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add this to the README as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, will do it 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@filiptronicek Updated README.md feel free to update it again XD

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mustard-mh could we decouple this from here and move into the helpers package 🙏?

Copy link
Contributor Author

@mustard-mh mustard-mh Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@filiptronicek completion (api call + simple fmt.Sprintf) will only be used in cmd, if we move them to helpers, we need to move getGitpodClient and rootTestingOpts (using in unit test) out too. Not that worth to do it.

We can call classCompletionFunc any where in cmd package, so it's reusable now (i.e. add them to workspace up cmd 07ef7b7 )

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could follow the approach of OpenWorkspaceInPreferredEditor, where we pass in the client (easier for testing with client mocks). Feel like it's good to avoid coupling commands together through functions.

I think like this it's also alright. Leaving it up to ya

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)
filiptronicek marked this conversation as resolved.
Show resolved Hide resolved
}
Loading