Skip to content

Commit

Permalink
added use of gh cli as an option to authenticate if GITHUB_TOKEN is n…
Browse files Browse the repository at this point in the history
…ot set
  • Loading branch information
TylerMizuyabu committed May 23, 2024
1 parent eb990bc commit 1c773b3
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions organizations/tools/gh_foundations/cmd/check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"gh_foundations/internal/pkg/types"
"gh_foundations/internal/pkg/types/github"
"os"
"os/exec"
"strings"

"github.com/spf13/cobra"
)
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit 1c773b3

Please sign in to comment.