Skip to content

Commit ae3c6ff

Browse files
committed
refactor: replace fatih/color with go-pretty
1 parent 418ad70 commit ae3c6ff

File tree

5 files changed

+47
-40
lines changed

5 files changed

+47
-40
lines changed

go.mod

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ go 1.21
44

55
require (
66
github.com/BurntSushi/toml v1.3.2
7-
github.com/fatih/color v1.16.0
87
github.com/google/go-cmp v0.6.0
8+
github.com/jedib0t/go-pretty/v6 v6.6.7
99
github.com/tidwall/jsonc v0.3.2
10-
golang.org/x/mod v0.14.0
10+
golang.org/x/mod v0.17.0
1111
gopkg.in/yaml.v3 v3.0.1
1212
)
1313

1414
require (
15-
github.com/mattn/go-colorable v0.1.13 // indirect
16-
github.com/mattn/go-isatty v0.0.20 // indirect
17-
golang.org/x/sys v0.14.0 // indirect
15+
github.com/mattn/go-runewidth v0.0.16 // indirect
16+
github.com/rivo/uniseg v0.4.7 // indirect
17+
golang.org/x/sys v0.30.0 // indirect
18+
golang.org/x/text v0.22.0 // indirect
1819
)

go.sum

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
22
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
3-
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
4-
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
3+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
4+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
55
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
66
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
7-
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
8-
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
9-
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
10-
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
11-
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
7+
github.com/jedib0t/go-pretty/v6 v6.6.7 h1:m+LbHpm0aIAPLzLbMfn8dc3Ht8MW7lsSO4MPItz/Uuo=
8+
github.com/jedib0t/go-pretty/v6 v6.6.7/go.mod h1:YwC5CE4fJ1HFUDeivSV1r//AmANFHyqczZk+U6BDALU=
9+
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
10+
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
11+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
12+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
13+
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
14+
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
15+
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
16+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
17+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
1218
github.com/tidwall/jsonc v0.3.2 h1:ZTKrmejRlAJYdn0kcaFqRAKlxxFIC21pYq8vLa4p2Wc=
1319
github.com/tidwall/jsonc v0.3.2/go.mod h1:dw+3CIxqHi+t8eFSpzzMlcVYxKp08UP5CD8/uSFCyJE=
14-
golang.org/x/mod v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0=
15-
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
16-
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
17-
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
18-
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
19-
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
20+
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
21+
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
22+
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
23+
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
24+
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
25+
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
2026
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2127
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2228
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

internal/reporter/report.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"fmt"
55
"strings"
66

7-
"github.com/fatih/color"
87
"github.com/g-rath/osv-detector/internal"
98
"github.com/g-rath/osv-detector/pkg/database"
109
"github.com/g-rath/osv-detector/pkg/lockfile"
10+
"github.com/jedib0t/go-pretty/v6/text"
1111
)
1212

1313
type PackageDetailsWithVulnerabilities struct {
@@ -61,14 +61,14 @@ func (r Report) formatLineByLine() string {
6161

6262
lines = append(lines, fmt.Sprintf(
6363
" %s %s",
64-
color.YellowString("%s@%s", pkg.Name, pkg.Version),
65-
color.RedString("is affected by the following vulnerabilities:"),
64+
text.FgYellow.Sprintf("%s@%s", pkg.Name, pkg.Version),
65+
text.FgRed.Sprintf("is affected by the following vulnerabilities:"),
6666
))
6767

6868
for _, vulnerability := range pkg.Vulnerabilities {
6969
lines = append(lines, fmt.Sprintf(
7070
" %s %s",
71-
color.CyanString("%s:", vulnerability.ID),
71+
text.FgCyan.Sprintf("%s:", vulnerability.ID),
7272
vulnerability.Describe(),
7373
))
7474
}
@@ -92,7 +92,7 @@ func (r Report) describeIgnores() string {
9292
return ""
9393
}
9494

95-
return color.YellowString(
95+
return text.FgYellow.Sprintf(
9696
" (%d %s ignored)",
9797
count,
9898
Form(count, "was", "were"),
@@ -111,7 +111,7 @@ func (r Report) String() string {
111111
if count == 0 {
112112
return fmt.Sprintf(
113113
" %s%s\n",
114-
color.GreenString("no %s vulnerabilities found", word),
114+
text.FgGreen.Sprintf("no %s vulnerabilities found", word),
115115
ignoreMsg,
116116
)
117117
}
@@ -120,7 +120,7 @@ func (r Report) String() string {
120120
out += "\n"
121121

122122
out += fmt.Sprintf("\n %s%s\n",
123-
color.RedString(
123+
text.FgRed.Sprintf(
124124
"%d %s %s found in %s",
125125
count,
126126
word,

internal/reporter/reporter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"fmt"
77
"io"
88

9-
"github.com/fatih/color"
109
"github.com/g-rath/osv-detector/pkg/database"
1110
"github.com/g-rath/osv-detector/pkg/lockfile"
11+
"github.com/jedib0t/go-pretty/v6/text"
1212
)
1313

1414
type Reporter struct {
@@ -82,10 +82,10 @@ func (r *Reporter) PrintDatabaseLoadErr(err error) {
8282
msg := err.Error()
8383

8484
if errors.Is(err, database.ErrOfflineDatabaseNotFound) {
85-
msg = color.RedString("no local version of the database was found, and --offline flag was set")
85+
msg = text.FgRed.Sprintf("no local version of the database was found, and --offline flag was set")
8686
}
8787

88-
r.PrintErrorf(" %s\n", color.RedString("failed: %s", msg))
88+
r.PrintErrorf(" %s\n", text.FgRed.Sprintf("failed: %s", msg))
8989
}
9090

9191
func (r *Reporter) PrintKnownEcosystems() {

main.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import (
1010
"sort"
1111
"strings"
1212

13-
"github.com/fatih/color"
1413
"github.com/g-rath/osv-detector/internal"
1514
"github.com/g-rath/osv-detector/internal/configer"
1615
"github.com/g-rath/osv-detector/internal/reporter"
1716
"github.com/g-rath/osv-detector/pkg/database"
1817
"github.com/g-rath/osv-detector/pkg/lockfile"
18+
"github.com/jedib0t/go-pretty/v6/text"
1919
)
2020

2121
// these come from goreleaser
@@ -88,11 +88,11 @@ func (dbs OSVDatabases) check(r *reporter.Reporter, lockf lockfile.Lockfile, ign
8888
results, err := db.Check(lockf.Packages)
8989

9090
if err != nil {
91-
r.PrintErrorf(color.RedString(fmt.Sprintf(
91+
r.PrintErrorf(text.FgRed.Sprintf(
9292
" an api error occurred while trying to check the packages listed in %s: %v\n",
9393
lockf.FilePath,
9494
err,
95-
)))
95+
))
9696

9797
continue
9898
}
@@ -157,13 +157,13 @@ func uniqueDBConfigs(configs []*configer.Config) []database.Config {
157157
func describeDB(db database.DB) string {
158158
switch tt := db.(type) {
159159
case *database.APIDB:
160-
return "using batches of " + color.YellowString("%d", tt.BatchSize)
160+
return "using batches of " + text.FgYellow.Sprintf("%d", tt.BatchSize)
161161
case *database.ZipDB:
162162
count := tt.VulnerabilitiesCount
163163

164164
return fmt.Sprintf(
165165
"%s %s, including withdrawn - last updated %s",
166-
color.YellowString("%d", count),
166+
text.FgYellow.Sprintf("%d", count),
167167
reporter.Form(count, "vulnerability", "vulnerabilities"),
168168
tt.UpdatedAt,
169169
)
@@ -172,7 +172,7 @@ func describeDB(db database.DB) string {
172172

173173
return fmt.Sprintf(
174174
"%s %s, including withdrawn",
175-
color.YellowString("%d", count),
175+
text.FgYellow.Sprintf("%d", count),
176176
reporter.Form(count, "vulnerability", "vulnerabilities"),
177177
)
178178
}
@@ -626,8 +626,8 @@ This flag can be passed multiple times to ignore different vulnerabilities`)
626626

627627
r.PrintTextf(
628628
"%s: found %s %s\n",
629-
color.MagentaString("%s", lockf.FilePath),
630-
color.YellowString("%d", len(lockf.Packages)),
629+
text.FgMagenta.Sprintf(lockf.FilePath),
630+
text.FgYellow.Sprintf("%d", len(lockf.Packages)),
631631
reporter.Form(len(lockf.Packages), "package", "packages"),
632632
)
633633

@@ -652,15 +652,15 @@ This flag can be passed multiple times to ignore different vulnerabilities`)
652652
ignoresStr = "skipping any ignores"
653653
} else {
654654
ignores = append(ignores, config.Ignore...)
655-
ignoresStr = color.YellowString("%d %s",
655+
ignoresStr = text.FgYellow.Sprintf("%d %s",
656656
len(config.Ignore),
657657
reporter.Form(len(config.Ignore), "ignore", "ignores"),
658658
)
659659
}
660660

661661
r.PrintTextf(
662662
" Using config at %s (%s)\n",
663-
color.MagentaString(config.FilePath),
663+
text.FgMagenta.Sprintf(config.FilePath),
664664
ignoresStr,
665665
)
666666
}
@@ -677,7 +677,7 @@ This flag can be passed multiple times to ignore different vulnerabilities`)
677677

678678
r.PrintTextf(
679679
" Using db %s%s\n",
680-
color.HiCyanString(db.Name()),
680+
text.FgHiCyan.Sprintf(db.Name()),
681681
desc,
682682
)
683683
}
@@ -740,7 +740,7 @@ func writeUpdatedConfigs(r *reporter.Reporter, vulnsPerConfig map[string]map[str
740740
if err == nil {
741741
lines = append(lines, fmt.Sprintf(
742742
"Updated %s with %d %s\n",
743-
color.MagentaString(configPath),
743+
text.FgMagenta.Sprintf(configPath),
744744
len(vulns),
745745
reporter.Form(len(vulns), "vulnerability", "vulnerabilities"),
746746
))

0 commit comments

Comments
 (0)