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
This commit is contained in:
Shaharyar Ahmed 2021-08-11 15:40:15 +05:00 committed by Mislav Marohnić
parent 4fa984a333
commit e28236a447

View file

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