forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompletion.go
More file actions
65 lines (54 loc) · 1.7 KB
/
completion.go
File metadata and controls
65 lines (54 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package command
import (
"errors"
"fmt"
"os"
"github.com/cli/cli/utils"
"github.com/spf13/cobra"
)
func init() {
RootCmd.AddCommand(completionCmd)
completionCmd.Flags().StringP("shell", "s", "", "Shell type: {bash|zsh|fish|powershell}")
}
var completionCmd = &cobra.Command{
Use: "completion",
Short: "Generate shell completion scripts",
Long: `Generate shell completion scripts for GitHub CLI commands.
The output of this command will be computer code and is meant to be saved to a
file or immediately evaluated by an interactive shell.
For example, for bash you could add this to your '~/.bash_profile':
eval "$(gh completion -s bash)"
When installing GitHub CLI through a package manager, however, it's possible that
no additional shell configuration is necessary to gain completion support. For
Homebrew, see <https://docs.brew.sh/Shell-Completion>
`,
RunE: func(cmd *cobra.Command, args []string) error {
shellType, err := cmd.Flags().GetString("shell")
if err != nil {
return err
}
if shellType == "" {
out := cmd.OutOrStdout()
isTTY := false
if outFile, isFile := out.(*os.File); isFile {
isTTY = utils.IsTerminal(outFile)
}
if isTTY {
return errors.New("error: the value for `--shell` is required\nsee `gh help completion` for more information")
}
shellType = "bash"
}
switch shellType {
case "bash":
return RootCmd.GenBashCompletion(cmd.OutOrStdout())
case "zsh":
return RootCmd.GenZshCompletion(cmd.OutOrStdout())
case "powershell":
return RootCmd.GenPowerShellCompletion(cmd.OutOrStdout())
case "fish":
return RootCmd.GenFishCompletion(cmd.OutOrStdout(), true)
default:
return fmt.Errorf("unsupported shell type %q", shellType)
}
},
}