Skip to content

Commit

Permalink
hostname, owner を指定できるようにして、カレントディレクトリがどこでも実行できるようにする
Browse files Browse the repository at this point in the history
  • Loading branch information
kumak1 committed Mar 29, 2023
1 parent 9b66582 commit bc075e3
Showing 1 changed file with 50 additions and 27 deletions.
77 changes: 50 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"github.com/cli/go-gh"
"github.com/cli/go-gh/pkg/repository"
"github.com/fatih/color"
flags "github.com/spf13/pflag"
"log"
Expand All @@ -14,25 +15,31 @@ import (
"time"
)

var (
repo repository.Repository
repositories []string
hostname string
owner string
ecosystem string
scope string
severity string
state string
perPage int
jq string
)

func main() {
initArguments()

repository, err := gh.CurrentRepository()
if err != nil {
fmt.Println(err)
}

args := []string{"api", "--hostname", repository.Host(), "--jq", outputQuery()}
var owner = repository.Owner()
var repos = targetRepos(repository.Name())
var showRepositoryName = len(repos) > 1
args := []string{"api", "--hostname", targetHostname(), "--jq", outputQuery()}

var repos, showRepoName = targetRepos()
for _, repoName := range repos {
if showRepositoryName {
if showRepoName {
fmt.Println(repoName)
}

stdOut, stdErr, err := gh.Exec(append(args, []string{targetPath(owner, repoName)}...)...)
stdOut, stdErr, err := gh.Exec(append(args, []string{targetPath(repoName)}...)...)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -75,17 +82,33 @@ func printError(stdErr bytes.Buffer) {
}
}

func targetRepos(defaultRepoName string) []string {
if len(repositories) == 0 {
return []string{defaultRepoName}
func targetRepos() ([]string, bool) {
if len(repositories) == 0 && repo != nil {
return []string{repo.Name()}, false
} else {
return repositories, len(repositories) > 1
}
}

func targetHostname() string {
if hostname == "" && repo != nil {
return repo.Host()
} else {
return hostname
}
}

func targetOwner() string {
if owner == "" && repo != nil {
return repo.Owner()
} else {
return repositories
return owner
}
}

func targetPath(owner string, repoName string) string {
func targetPath(repoName string) string {
u := &url.URL{}
u.Path = fmt.Sprintf("/repos/%s/%s/dependabot/alerts", owner, repoName)
u.Path = fmt.Sprintf("/repos/%s/%s/dependabot/alerts", targetOwner(), repoName)
q := u.Query()

if ecosystem != "" {
Expand Down Expand Up @@ -139,18 +162,10 @@ func formatSeverity(severity string) string {
}
}

var (
repositories []string
ecosystem string
scope string
severity string
state string
perPage int
jq string
)

func initArguments() {
flags.StringArrayVarP(&repositories, "repo", "r", []string{}, "")
flags.StringVar(&hostname, "hostname", "", "specify github hostname")
flags.StringVarP(&owner, "owner", "o", "", "specify github owner")
flags.StringVarP(&ecosystem, "ecosystem", "e", "", "specify comma-separated list. can be: composer, go, maven, npm, nuget, pip, pub, rubygems, rust")
flags.StringVar(&scope, "scope", "", "specify comma-separated list. can be: development, runtime")
flags.StringVar(&severity, "severity", "", "specify comma-separated list. can be: low, medium, high, critical")
Expand All @@ -166,6 +181,14 @@ func initArguments() {
flags.PrintDefaults()
os.Exit(1)
}

if hostname == "" || owner == "" {
currentRepository, err := gh.CurrentRepository()
if err != nil {
fmt.Println(err)
}
repo = currentRepository
}
}

// For more examples of using go-gh, see:
Expand Down

0 comments on commit bc075e3

Please sign in to comment.