Merge pull request #761 from cli/completion-fixes

Improve `completion` command
This commit is contained in:
Mislav Marohnić 2020-04-09 19:10:36 +02:00 committed by GitHub
commit 7f9aef6c0d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 3 deletions

View file

@ -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) {

View file

@ -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 <https://docs.brew.sh/Shell-Completion>
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())