From e28236a447ab0aa89e520abcf65faaa19afb8002 Mon Sep 17 00:00:00 2001 From: Shaharyar Ahmed Date: Wed, 11 Aug 2021 15:40:15 +0500 Subject: [PATCH] Check path for git executable before auth There was a bug where if git was not installed then gh would do its authentication and try to configure git but would then find out that the git executable was not in PATH. Now gh checks to see if the git executable is in PATH before authenticating the user. If the git executable is in PATH the authentication continues as normal, if it is not in PATH then it prints out an error to the console: $ git executable not found in $PATH Resolves: #3818 --- pkg/cmd/auth/login/login.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pkg/cmd/auth/login/login.go b/pkg/cmd/auth/login/login.go index 995405021..7866682bf 100644 --- a/pkg/cmd/auth/login/login.go +++ b/pkg/cmd/auth/login/login.go @@ -5,6 +5,7 @@ import ( "fmt" "io/ioutil" "net/http" + "os/exec" "strings" "github.com/AlecAivazis/survey/v2" @@ -70,6 +71,10 @@ func NewCmdLogin(f *cmdutil.Factory, runF func(*LoginOptions) error) *cobra.Comm $ gh auth login --hostname enterprise.internal `), RunE: func(cmd *cobra.Command, args []string) error { + if !isGitInPath() { + return errors.New("git executable not found in $PATH") + } + if !opts.IO.CanPrompt() && !(tokenStdin || opts.Web) { return &cmdutil.FlagError{Err: errors.New("--web or --with-token required when not running interactively")} } @@ -119,6 +124,11 @@ func NewCmdLogin(f *cmdutil.Factory, runF func(*LoginOptions) error) *cobra.Comm return cmd } +func isGitInPath() bool { + _, err := exec.LookPath("git") + return err == nil +} + func loginRun(opts *LoginOptions) error { cfg, err := opts.Config() if err != nil {