Skip to content

Commit b8a96bb

Browse files
committed
[gpctl] Updated bash completion
1 parent b083e6e commit b8a96bb

File tree

5 files changed

+93
-78
lines changed

5 files changed

+93
-78
lines changed

dev/gpctl/cmd/completion.go

Lines changed: 51 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,71 @@
44

55
package cmd
66

7-
//go:generate sh -c "cd .. && echo \\# generated using go generate in components/gpctl > ../../devops/images/workspaces/gitpod-dev/gpctl_completion; go run main.go completion bash >> ../../devops/images/workspaces/gitpod-dev/gpctl_completion"
8-
97
import (
108
"os"
119

1210
"github.com/spf13/cobra"
1311
)
1412

15-
const (
16-
bashCompletionFunc = `__gpctl_parse_get_workspace_url()
17-
{
18-
local gpctl_output out
19-
if gpctl_output=$(gpctl workspaces list --output-template '{{ range .Status }}{{ .Id }} {{ .Spec.Url | trimPrefix "http://" | trimPrefix "https://" }} {{ end }}' | tr " " "\n" 2>/dev/null); then
20-
out=($(echo "${gpctl_output}" | awk '{print $1}'))
21-
COMPREPLY=( $( compgen -W "${out[*]}" -- "$cur" ) )
22-
fi
23-
}
24-
__gpctl_get_workspace_url()
25-
{
26-
__gpctl_parse_get_workspace_url
27-
if [[ $? -eq 0 ]]; then
28-
return 0
29-
fi
30-
}
31-
__gpctl_parse_get_imagebuild_ref()
32-
{
33-
local gpctl_output out
34-
if gpctl_output=$(gpctl imagebuilds list -o jsonpath | tr " " "\n" 2>/dev/null); then
35-
out=($(echo "${gpctl_output}" | awk '{print $1}'))
36-
COMPREPLY=( $( compgen -W "${out[*]}" -- "$cur" ) )
37-
fi
38-
}
39-
__gpctl_get_imagebuild_ref()
40-
{
41-
__gpctl_parse_get_imagebuild_ref
42-
if [[ $? -eq 0 ]]; then
43-
return 0
44-
fi
45-
}
46-
__gpctl_custom_func() {
47-
case ${last_command} in
48-
gpctl_workspaces_describe | gpctl_workspaces_stop | gpctl_workspaces_snapshot)
49-
__gpctl_get_workspace_url
50-
return
51-
;;
52-
gpctl_imagebuilds_logs)
53-
__gpctl_get_imagebuild_ref
54-
return
55-
;;
56-
*)
57-
;;
58-
esac
59-
}
60-
`
61-
)
13+
var completionCmd = &cobra.Command{
14+
Use: "completion [bash|zsh|fish|powershell]",
15+
Short: "Generate completion script",
16+
Long: `To load completions:
17+
18+
Bash:
19+
20+
$ source <(gpctl completion bash)
21+
22+
# To load completions for each session, execute once:
23+
# Linux:
24+
$ gpctl completion bash > /etc/bash_completion.d/gpctl
25+
# macOS:
26+
$ gpctl completion bash > /usr/local/etc/bash_completion.d/gpctl
27+
28+
Zsh:
29+
30+
# If shell completion is not already enabled in your environment,
31+
# you will need to enable it. You can execute the following once:
32+
33+
$ echo "autoload -U compinit; compinit" >> ~/.zshrc
34+
35+
# To load completions for each session, execute once:
36+
$ gpctl completion zsh > "${fpath[1]}/_gpctl"
37+
38+
# You will need to start a new shell for this setup to take effect.
39+
40+
fish:
41+
42+
$ gpctl completion fish | source
43+
44+
# To load completions for each session, execute once:
45+
$ gpctl completion fish > ~/.config/fish/completions/gpctl.fish
46+
47+
PowerShell:
48+
49+
PS> gpctl completion powershell | Out-String | Invoke-Expression
6250
63-
// bashCompletionCmd represents the bashCompletion command
64-
var bashCompletionCmd = &cobra.Command{
65-
Use: "completion bash|zsh",
66-
Short: "Provides shell completion for gpctl. Use with `. <(gpctl completion)`",
67-
Hidden: true,
68-
Args: cobra.ExactValidArgs(1),
69-
ValidArgs: []string{"bash", "zsh"},
51+
# To load completions for every new session, run:
52+
PS> gpctl completion powershell > gpctl.ps1
53+
# and source this file from your PowerShell profile.
54+
`,
55+
DisableFlagsInUseLine: true,
56+
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
57+
Args: cobra.ExactValidArgs(1),
7058
Run: func(cmd *cobra.Command, args []string) {
7159
switch args[0] {
7260
case "bash":
73-
rootCmd.GenBashCompletion(os.Stdout)
61+
cmd.Root().GenBashCompletion(os.Stdout)
7462
case "zsh":
75-
rootCmd.GenZshCompletion(os.Stdout)
63+
cmd.Root().GenZshCompletion(os.Stdout)
64+
case "fish":
65+
cmd.Root().GenFishCompletion(os.Stdout, true)
66+
case "powershell":
67+
cmd.Root().GenPowerShellCompletionWithDesc(os.Stdout)
7668
}
7769
},
7870
}
7971

8072
func init() {
81-
rootCmd.AddCommand(bashCompletionCmd)
73+
rootCmd.AddCommand(completionCmd)
8274
}

dev/gpctl/cmd/root.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ import (
2020

2121
// rootCmd represents the base command when called without any subcommands
2222
var rootCmd = &cobra.Command{
23-
Use: "gpctl",
24-
Short: "Gpctl controls a Gitpod installation",
25-
Args: cobra.MinimumNArgs(1),
26-
BashCompletionFunction: bashCompletionFunc,
23+
Use: "gpctl",
24+
Short: "Gpctl controls a Gitpod installation",
25+
Args: cobra.MinimumNArgs(1),
2726
}
2827

2928
// Execute adds all child commands to the root command and sets flags appropriately.

dev/gpctl/cmd/workspaces-start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func init() {
9393
}
9494

9595
workspacesCmd.AddCommand(workspacesStartCmd)
96-
workspacesStartCmd.Flags().StringVarP(&startWorkspaceReq.ServicePrefix, "service-prefix", "p", "", "use a service prefix different from the workspace ID")
96+
workspacesStartCmd.Flags().StringVar(&startWorkspaceReq.ServicePrefix, "service-prefix", "", "use a service prefix different from the workspace ID")
9797
workspacesStartCmd.Flags().StringVar(&startWorkspaceReq.Metadata.Owner, "owner", "gpctl", "set the workspace owner")
9898
workspacesStartCmd.Flags().StringVar(&startWorkspaceReq.Metadata.MetaId, "workspace-id", wsid, "set the workspace ID")
9999
workspacesStartCmd.Flags().IntP("count", "n", 1, "start multiple workspaces with the same spec - useful for load tests")

dev/gpctl/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require (
1616
github.com/huandu/xstrings v1.2.0 // indirect
1717
github.com/mitchellh/copystructure v1.0.0 // indirect
1818
github.com/mitchellh/go-homedir v1.1.0
19-
github.com/spf13/cobra v0.0.5
19+
github.com/spf13/cobra v1.1.3
2020
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
2121
google.golang.org/grpc v1.37.0
2222
google.golang.org/protobuf v1.26.0

0 commit comments

Comments
 (0)