Skip to content

Commit 9ebfa80

Browse files
b4nstmpvl
authored andcommitted
feat: add completion command
Add a command to generate a completion script for bash, zsh, fish and powershell shells. In order to generate the fish script the cobra package has been bumped to 1.0.0 Change-Id: Ie38e4754e74bc6ea9784e5ea4375d8d41abf3b04 Reviewed-on: https://cue-review.googlesource.com/c/cue/+/6380 Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent 57f8242 commit 9ebfa80

File tree

4 files changed

+83
-3
lines changed

4 files changed

+83
-3
lines changed

cmd/cue/cmd/completion.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Copyright 2019 CUE Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"fmt"
19+
20+
"github.com/spf13/cobra"
21+
)
22+
23+
var validCompletionArgs = []string{"bash", "zsh", "fish", "powershell"}
24+
25+
const completionExample = `
26+
Bash:
27+
28+
$ source <(cue completion bash)
29+
30+
# To load completions for each session, execute once:
31+
Linux:
32+
$ cue completion bash > /etc/bash_completion.d/cue
33+
MacOS:
34+
$ cue completion bash > /usr/local/etc/bash_completion.d/cue
35+
36+
Zsh:
37+
38+
$ source <(cue completion zsh)
39+
40+
# To load completions for each session, execute once:
41+
$ cue completion zsh > "${fpath[1]}/_cue"
42+
43+
Fish:
44+
45+
$ cue completion fish | source
46+
47+
# To load completions for each session, execute once:
48+
$ cue completion fish > ~/.config/fish/completions/cue.fish
49+
`
50+
51+
func newCompletionCmd(c *Command) *cobra.Command {
52+
cmd := &cobra.Command{
53+
Use: fmt.Sprintf("completion %s", validCompletionArgs),
54+
Short: "Generate completion script",
55+
Long: ``,
56+
Example: completionExample,
57+
ValidArgs: validCompletionArgs,
58+
Args: cobra.ExactValidArgs(1),
59+
RunE: mkRunE(c, runCompletion),
60+
}
61+
return cmd
62+
}
63+
64+
func runCompletion(cmd *Command, args []string) error {
65+
w := cmd.OutOrStdout()
66+
switch args[0] {
67+
case "bash":
68+
cmd.Root().GenBashCompletion(w)
69+
case "zsh":
70+
cmd.Root().GenZshCompletion(w)
71+
case "fish":
72+
cmd.Root().GenFishCompletion(w, true)
73+
case "powershell":
74+
cmd.Root().GenPowerShellCompletion(w)
75+
default:
76+
return fmt.Errorf("%s is not a supported shell", args[0])
77+
}
78+
return nil
79+
}

cmd/cue/cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ For more information on writing CUE configuration files see cuelang.org.`,
8888

8989
subCommands := []*cobra.Command{
9090
cmdCmd,
91+
newCompletionCmd(c),
9192
newEvalCmd(c),
9293
newDefCmd(c),
9394
newExportCmd(c),

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ require (
1111
github.com/mpvl/unique v0.0.0-20150818121801-cbe035fff7de
1212
github.com/pkg/errors v0.8.1 // indirect
1313
github.com/rogpeppe/go-internal v1.6.0
14-
github.com/spf13/cobra v0.0.7
14+
github.com/spf13/cobra v1.0.0
1515
github.com/spf13/pflag v1.0.3
1616
github.com/stretchr/testify v1.2.2
1717
golang.org/x/exp v0.0.0-20200513190911-00229845015e

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4k
9999
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
100100
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
101101
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
102-
github.com/spf13/cobra v0.0.7 h1:FfTH+vuMXOas8jmfb5/M7dzEYx7LpcLb7a0LPe34uOU=
103-
github.com/spf13/cobra v0.0.7/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
102+
github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8=
103+
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
104104
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
105105
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
106106
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=

0 commit comments

Comments
 (0)