Skip to content

Commit

Permalink
標準出力に色をつける
Browse files Browse the repository at this point in the history
  • Loading branch information
kumak1 committed Mar 29, 2023
1 parent f3e959d commit 021dcf3
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 11 deletions.
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ require github.com/cli/go-gh v1.2.1
require (
github.com/cli/safeexec v1.0.0 // indirect
github.com/cli/shurcooL-graphql v0.0.2 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/henvic/httpretty v0.0.6 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/muesli/termenv v0.12.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/cli/shurcooL-graphql v0.0.2 h1:rwP5/qQQ2fM0TzkUTwtt6E2LbIYf6R+39cUXTa
github.com/cli/shurcooL-graphql v0.0.2/go.mod h1:tlrLmw/n5Q/+4qSvosT+9/W5zc8ZMjnJeYBxSdb4nWA=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/henvic/httpretty v0.0.6 h1:JdzGzKZBajBfnvlMALXXMVQWxWMF/ofTy8C3/OSUTxs=
github.com/henvic/httpretty v0.0.6/go.mod h1:X38wLjWXHkXT7r2+uK8LjCMne9rsuNaBLJ+5cU2/Pmo=
Expand All @@ -15,9 +17,13 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
Expand All @@ -31,6 +37,8 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e h1:BuzhfgfWQbX0dWzYzT1zsORLnHRv3bcRcsaUk0VmXA8=
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e/go.mod h1:/Tnicc6m/lsJE0irFMA0LfIwTBo4QP7A8IfyIv4zZKI=
github.com/xeonx/timeago v1.0.0-rc5 h1:pwcQGpaH3eLfPtXeyPA4DmHWjoQt0Ea7/++FwpxqLxg=
github.com/xeonx/timeago v1.0.0-rc5/go.mod h1:qDLrYEFynLO7y5Ho7w3GwgtYgpy5UfhcXIIQvMKVDkA=
golang.org/x/net v0.0.0-20220923203811-8be639271d50/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
Expand All @@ -42,6 +50,8 @@ golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
Expand Down
85 changes: 76 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ package main
import (
"fmt"
"github.com/cli/go-gh"
"github.com/fatih/color"
flags "github.com/spf13/pflag"
"log"
"net/url"
"os"
"strings"
"text/tabwriter"
"time"
)

func main() {
initArguments()

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

initArguments()

// Queryを追加
u := &url.URL{}
u.Path = fmt.Sprintf("/repos/%s/%s/dependabot/alerts", repo.Owner(), repo.Name())
Expand All @@ -34,30 +38,93 @@ func main() {
if state != "" {
q.Set("state", state)
}
q.Set("per_page", fmt.Sprint(perPage))

u.RawQuery = q.Encode()

args := []string{"api", "--hostname", repo.Host(), "--jq", jq, u.String()}
stdOut, stdErr, err := gh.Exec(args...)
if err != nil {
log.Fatal(err)
if jq == "" {
args := []string{"api", "--hostname", repo.Host(), "--jq", ".[] | [.number, .security_advisory.severity, .dependency.package.ecosystem, .dependency.package.name, .html_url, .created_at] | @tsv", u.String()}
stdOut, stdErr, err := gh.Exec(args...)
if err != nil {
log.Fatal(err)
}
stdOutString := stdOut.String()
stdErrString := stdErr.String()

if stdOutString != "" {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 4, 8, 1, '\t', 0)
for _, columns := range strings.Split(stdOutString, "\n") {
var cols = strings.Split(columns, "\t")
if cols[0] == "" {
break
}
cols[0] = formatIndex(cols[0])
cols[1] = formatSeverity(cols[1])
cols[5] = formatDate(cols[5])
_, _ = fmt.Fprintln(w, strings.Join(cols, "\t"))
}
_ = w.Flush()
}
if stdErrString != "" {
fmt.Print(stdErrString)
}
} else {
args := []string{"api", "--hostname", repo.Host(), "--jq", jq, u.String()}
stdOut, stdErr, err := gh.Exec(args...)
if err != nil {
log.Fatal(err)
}
stdOutString := stdOut.String()
stdErrString := stdErr.String()

if stdOutString != "" {
fmt.Print(stdOutString)
}
if stdErrString != "" {
fmt.Print(stdErrString)
}
}
}

func formatIndex(index string) string {
return color.GreenString("#" + index)
}

func formatDate(date string) string {
t, _ := time.Parse(time.RFC3339, date)
return color.WhiteString(t.Format(time.DateOnly))

Check failure on line 96 in main.go

View workflow job for this annotation

GitHub Actions / release

undefined: time.DateOnly
}

func formatSeverity(severity string) string {
switch severity {
case "low":
return color.WhiteString(severity)
case "medium":
return color.YellowString(severity)
case "high":
return color.RedString(severity)
case "critical":
return color.HiRedString(severity)
default:
return severity
}
fmt.Println(stdOut.String())
fmt.Println(stdErr.String())
}

var ecosystem string
var scope string
var severity string
var state string
var perPage int
var jq string

func initArguments() {
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")
flags.StringVar(&state, "state", "", "specify comma-separated list. can be: dismissed, fixed, open")
flags.StringVarP(&jq, "jq", "q", ".[] | [.created_at, .security_advisory.severity, .dependency.package.name, .html_url] | @csv", "Query to select values from the response using jq syntax")
flags.IntVar(&perPage, "per_page", 30, "The number of results per page (max 100).")
flags.StringVarP(&jq, "jq", "q", "", "Query to select values from the response using jq syntax")

var help bool
flags.BoolVarP(&help, "help", "h", false, "help")
Expand Down

0 comments on commit 021dcf3

Please sign in to comment.