From 79f749a02b184d167d4db6050c0f9c1735ae1899 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 7 Apr 2020 16:46:36 +0200 Subject: [PATCH 1/2] Avoid checking for updates during `gh completion` This is due to the fact that `gh completion` can be programatically used during shell startup and we definitely don't want to 1. slow down people's startup time 2. show any upgrade notice until the user has invoked `gh` manually --- cmd/gh/main.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cmd/gh/main.go b/cmd/gh/main.go index a9e0837f3..17f2042f2 100644 --- a/cmd/gh/main.go +++ b/cmd/gh/main.go @@ -70,7 +70,11 @@ func printError(out io.Writer, err error, cmd *cobra.Command, debug bool) { } func shouldCheckForUpdate() bool { - return updaterEnabled != "" && utils.IsTerminal(os.Stderr) + return updaterEnabled != "" && !isCompletionCommand() && utils.IsTerminal(os.Stderr) +} + +func isCompletionCommand() bool { + return len(os.Args) > 1 && os.Args[1] == "completion" } func checkForUpdate(currentVersion string) (*update.ReleaseInfo, error) { From 2915abc2ba8e0110825812a4cf0fafe016f7dcda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 7 Apr 2020 16:58:50 +0200 Subject: [PATCH 2/2] Improve user-friendliness of `completion` command When ran directly in the terminal, the command now errors out with: $ gh completion error: the value for `--shell` is required see `gh completion --help` for more information This is to avoid the previously default bash code output confusing the user if they ran the command out of curiousity. A backwards compatibility layer is present here: if stdout is not a terminal, then output bash code like before. This is to support users who have already added a line like this to their bash profile: eval "$(gh completion)" --- command/completion.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/command/completion.go b/command/completion.go index 94c9ccf5d..319d103a2 100644 --- a/command/completion.go +++ b/command/completion.go @@ -1,15 +1,18 @@ package command import ( + "errors" "fmt" + "os" "github.com/cli/cli/internal/cobrafish" + "github.com/cli/cli/utils" "github.com/spf13/cobra" ) func init() { RootCmd.AddCommand(completionCmd) - completionCmd.Flags().StringP("shell", "s", "bash", "Shell type: {bash|zsh|fish|powershell}") + completionCmd.Flags().StringP("shell", "s", "", "Shell type: {bash|zsh|fish|powershell}") } var completionCmd = &cobra.Command{ @@ -17,9 +20,12 @@ var completionCmd = &cobra.Command{ 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)" + 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 @@ -31,6 +37,19 @@ Homebrew, see 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())