From 97d8285b5870d478a22bbbc949ff663e77043141 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Mon, 16 Aug 2021 23:19:20 +0200 Subject: [PATCH] Do not require GITHUB_TOKEN for merely viewing command help --- cmd/ghcs/main.go | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/cmd/ghcs/main.go b/cmd/ghcs/main.go index 00e8be894..58037437a 100644 --- a/cmd/ghcs/main.go +++ b/cmd/ghcs/main.go @@ -1,33 +1,46 @@ package main import ( + "errors" "fmt" + "io" "os" "github.com/spf13/cobra" ) func main() { - Execute() + if err := rootCmd.Execute(); err != nil { + explainError(os.Stderr, err) + os.Exit(1) + } } var Version = "DEV" var rootCmd = &cobra.Command{ - Use: "ghcs", - Short: "Unofficial GitHub Codespaces CLI.", - Long: "Unofficial CLI tool to manage and interact with GitHub Codespaces.", + Use: "ghcs", + Long: `Unofficial CLI tool to manage GitHub Codespaces. + +Running commands requires the GITHUB_TOKEN environment variable to be set to a +token to access the GitHub API with.`, Version: Version, + + PersistentPreRunE: func(cmd *cobra.Command, args []string) error { + if os.Getenv("GITHUB_TOKEN") == "" { + return tokenError + } + return nil + }, } -func Execute() { - if os.Getenv("GITHUB_TOKEN") == "" { - fmt.Println("The GITHUB_TOKEN environment variable is required. Create a Personal Access Token at https://github.com/settings/tokens/new?scopes=repo and make sure to enable SSO for the GitHub organization after creating the token.") - os.Exit(1) - } +var tokenError = errors.New("GITHUB_TOKEN is missing") - if err := rootCmd.Execute(); err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(1) +func explainError(w io.Writer, err error) { + if errors.Is(err, tokenError) { + fmt.Fprintln(w, "The GITHUB_TOKEN environment variable is required. Create a Personal Access Token at https://github.com/settings/tokens/new?scopes=repo") + fmt.Fprintln(w, "Make sure to enable SSO for your organizations after creating the token.") + return } + // fmt.Fprintf(w, "%v\n", err) }