From cb29b11ab207d1bb2ef6479d77c9991501e5a675 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Mon, 19 Jul 2021 18:10:15 +0100 Subject: [PATCH] cmd/ghcs/main: Fail gracefully if `GITHUB_TOKEN` entirely unset MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - I have my GitHub API token in my environment as `HOMEBREW_GITHUB_API_TOKEN`, so with things that need `GITHUB_TOKEN` I have to remember to `export GITHUB_TOKEN=$HOMEBREW_GITHUB_API_TOKEN`. - I didn't for this tool, and got this unfriendly error message: ``` ❯ ghcs list Error: error getting user: Bad credentials Usage: ghcs list [flags] Flags: -h, --help help for list error getting user: Bad credentials ``` - This moves the "do you have a `GITHUB_TOKEN`" question to the very beginning (no guarantees about org SSO access, just a string that exists), erroring out with a nice message if users don't have that envvar set: ``` issyl0 in cetus in ~/repos/github/ghcs/cmd/ghcs on gracefully-fail-if-token-envvar-unset ❯ ./ghcs list The GITHUB_TOKEN environment variable is required. Create a Personal Access Token with org SSO access at https://github.com/settings/tokens/new. issyl0 in cetus in ~/repos/github/ghcs/cmd/ghcs on gracefully-fail-if-token-envvar-unset ❯ export GITHUB_TOKEN=$HOMEBREW_GITHUB_API_TOKEN ❯ ./ghcs list +--------------------------------+--------------------+------------------------------------+----------+---------------------------+ | NAME | REPOSITORY | BRANCH | STATE | CREATED AT | +--------------------------------+--------------------+------------------------------------+----------+---------------------------+ | issyl0-github-cat-ggrpj5fvwvr | github/cat | dependabot/bundler/graphql-1.12.13 | Shutdown | 2021-07-13T12:36:53+01:00 | +--------------------------------+--------------------+------------------------------------+----------+---------------------------+ ``` --- cmd/ghcs/main.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cmd/ghcs/main.go b/cmd/ghcs/main.go index 400f5324c..ce8ec91e6 100644 --- a/cmd/ghcs/main.go +++ b/cmd/ghcs/main.go @@ -22,6 +22,11 @@ var rootCmd = &cobra.Command{ } func Execute() { + if os.Getenv("GITHUB_TOKEN") == "" { + fmt.Println("The GITHUB_TOKEN environment variable is required. Create a Personal Access Token with org SSO access at https://github.com/settings/tokens/new.") + os.Exit(1) + } + if err := rootCmd.Execute(); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1)