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 5 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
10 changes: 10 additions & 0 deletions components/local-app/BUILD.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ 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
# pipe completion
sudo /usr/bin/gitpod completion bash | sudo tee /usr/share/bash-completion/completions/gitpod
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