From 28a8b7a04cebd1e6df913e47b647983400950c13 Mon Sep 17 00:00:00 2001 From: Edgar Albalate Date: Tue, 3 Feb 2026 18:49:28 +0100 Subject: [PATCH 1/2] feat: brows tries to get token from gh cli if the env var is not set also moved GitHub token retrieval to util package --- brows.go | 7 +++---- util/github.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 util/github.go diff --git a/brows.go b/brows.go index 0387a66..6d94270 100644 --- a/brows.go +++ b/brows.go @@ -1,5 +1,7 @@ package main +import "github.com/rubysolo/brows/util" + import ( "context" "fmt" @@ -464,10 +466,7 @@ func main() { repo = parts[1] } - token := os.Getenv("GITHUB_OAUTH_TOKEN") - if token == "" { - log.Fatal("no GITHUB_OAUTH_TOKEN provided.") - } + token := util.GetGHToken() ctx := context.Background() ts := oauth2.StaticTokenSource( diff --git a/util/github.go b/util/github.go new file mode 100644 index 0000000..99a0d1b --- /dev/null +++ b/util/github.go @@ -0,0 +1,34 @@ +package util + +import ( + "log" + "os" + "os/exec" + "strings" +) + +const GITHUB_TOKEN_ENV = "GITHUB_OAUTH_TOKEN" +const GH_CLI_COMMAND = "gh auth token" + +// GetToken retrieves a GitHub access token from the environment variable `GITHUB_OAUTH_TOKEN`. +// If the environment variable is not set, it attempts to obtain the token by executing +// the GitHub CLI `gh auth token` command. If both methods fail, the function terminates +// the program with a fatal error, providing guidance for the user. +func GetGHToken() string { + token := os.Getenv(GITHUB_TOKEN_ENV) + if token != "" { + return token + } + + + command_arr := strings.Split(GH_CLI_COMMAND, " ") + out, err := exec.Command(command_arr[0], command_arr[1:]...).Output() + if err != nil { + log.Fatal("GITHUB_OAUTH_TOKEN not set and failed to retrieve token from gh CLI: ", err, "\nPlease set GITHUB_OAUTH_TOKEN or run 'gh auth login'") + } + trimmed := strings.TrimSpace(string(out)) + if trimmed == "" { + log.Fatal("GITHUB_OAUTH_TOKEN not set and gh CLI did not return a token.\nPlease set GITHUB_OAUTH_TOKEN or run 'gh auth login'") + } + return trimmed +} From f1233cb3983a4b8cda3a01aeb095d6ad23cccde3 Mon Sep 17 00:00:00 2001 From: Edgar Albalate Date: Tue, 3 Feb 2026 18:51:19 +0100 Subject: [PATCH 2/2] docs: clarify GitHub auth token requirements in README --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 6056ea2..1f7978f 100644 --- a/README.md +++ b/README.md @@ -20,8 +20,7 @@ brew install brows ``` ## Configuration: - - * (Required) Set the `GITHUB_OAUTH_TOKEN` environment variable to a [GitHub PAT](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) for access to the GitHub API. + * (Required) A GitHub auth token is required. It can be set by the `GITHUB_OAUTH_TOKEN` environment variable to a [GitHub PAT](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) or, if the environment variable is not set, Brows will attempt to use the token from the [GitHub CLI](https://cli.github.com/) (`gh auth token`) if available (CLI installed and logged in). * (Optional) Create a config file at `$HOME/.config/brows.yml` and set a `default_org` key, like: ```