From 5771fcb35e32353605db8ae6e572a39c9a98d02e Mon Sep 17 00:00:00 2001 From: Spenser Black Date: Tue, 14 Jun 2022 08:27:11 -0400 Subject: [PATCH] Print auth help message based on environment (#5781) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This will check if `gh` is running in a CI environment. If so, it will suggest using environment variables to set the auth token. Additionally, if the CI environment is detected to be GitHub Actions, it will provide some example code. If it is not a CI environment, it will print the standard help message. Co-authored-by: Mislav Marohnić --- cmd/gh/main.go | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/cmd/gh/main.go b/cmd/gh/main.go index 38c6b2fa1..4ed2dc941 100644 --- a/cmd/gh/main.go +++ b/cmd/gh/main.go @@ -13,6 +13,7 @@ import ( surveyCore "github.com/AlecAivazis/survey/v2/core" "github.com/AlecAivazis/survey/v2/terminal" + "github.com/MakeNowJust/heredoc" "github.com/cli/cli/v2/api" "github.com/cli/cli/v2/git" "github.com/cli/cli/v2/internal/build" @@ -205,15 +206,11 @@ func mainRun() exitCode { return results, cobra.ShellCompDirectiveNoFileComp } - cs := cmdFactory.IOStreams.ColorScheme() - authError := errors.New("authError") rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { // require that the user is authenticated before running most commands if cmdutil.IsAuthCheckEnabled(cmd) && !cmdutil.CheckAuth(cfg) { - fmt.Fprintln(stderr, cs.Bold("Welcome to GitHub CLI!")) - fmt.Fprintln(stderr) - fmt.Fprintln(stderr, "To authenticate, please run `gh auth login`.") + fmt.Fprint(stderr, authHelp()) return authError } @@ -319,6 +316,27 @@ func printError(out io.Writer, err error, cmd *cobra.Command, debug bool) { } } +func authHelp() string { + if os.Getenv("GITHUB_ACTIONS") == "true" { + return heredoc.Doc(` + gh: To use GitHub CLI in a GitHub Actions workflow, set the GH_TOKEN environment variable. Example: + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + `) + } + + if os.Getenv("CI") != "" { + return heredoc.Doc(` + gh: To use GitHub CLI in automation, set the GH_TOKEN environment variable. + `) + } + + return heredoc.Doc(` + To get started with GitHub CLI, please run: gh auth login + Alternatively, populate the GH_TOKEN environment variable with a GitHub API authentication token. + `) +} + func shouldCheckForUpdate() bool { if os.Getenv("GH_NO_UPDATE_NOTIFIER") != "" { return false