From 1c773b347dd3c235119cd79151e69456e1441d73 Mon Sep 17 00:00:00 2001 From: Tyler Mizuyabu Date: Thu, 23 May 2024 13:55:09 -0400 Subject: [PATCH] added use of gh cli as an option to authenticate if GITHUB_TOKEN is not set --- .../tools/gh_foundations/cmd/check/check.go | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/organizations/tools/gh_foundations/cmd/check/check.go b/organizations/tools/gh_foundations/cmd/check/check.go index c1dccac..646ce0f 100644 --- a/organizations/tools/gh_foundations/cmd/check/check.go +++ b/organizations/tools/gh_foundations/cmd/check/check.go @@ -6,6 +6,8 @@ import ( "gh_foundations/internal/pkg/types" "gh_foundations/internal/pkg/types/github" "os" + "os/exec" + "strings" "github.com/spf13/cobra" ) @@ -23,13 +25,18 @@ var CheckCmd = &cobra.Command{ return nil }, Run: func(cmd *cobra.Command, args []string) { + var err error reports := make([]types.CheckReport, 0) slug := args[0] authToken, set := os.LookupEnv("GITHUB_TOKEN") if !set { - cmd.PrintErr("GITHUB_TOKEN environment variable not set") - return + authToken, err = getTokenFromGhCli() + if err != nil { + cmd.PrintErr("GITHUB_TOKEN environment variable not set and unable to authenticate with gh cli") + return + } } + gs := github.NewGithubService(authToken) org, err := gs.GetOrganization(slug) if err == nil { @@ -61,3 +68,13 @@ var CheckCmd = &cobra.Command{ file.Write(bytes) }, } + +func getTokenFromGhCli() (string, error) { + cmd := "gh" + out, err := exec.Command(cmd, "auth", "token").Output() + if err != nil { + return "", errors.New("unable to authenticate with gh cli") + } + + return strings.TrimSpace(string(out)), nil +}