cmd/ghcs/main: Fail gracefully if GITHUB_TOKEN entirely unset

- 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 |
+--------------------------------+--------------------+------------------------------------+----------+---------------------------+
```
This commit is contained in:
Issy Long 2021-07-19 18:10:15 +01:00
parent 570a407bac
commit cb29b11ab2

View file

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