Skip to content

Commit 0f90f50

Browse files
committed
Provide modes for search results
1 parent c4e3c8a commit 0f90f50

File tree

4 files changed

+66
-22
lines changed

4 files changed

+66
-22
lines changed

cmd/namigo/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ func main() {
2222
Usage: "Max number of results to display",
2323
Value: 10,
2424
},
25+
&cli.StringFlag{
26+
Name: "mode",
27+
Usage: "Output mode: text/json",
28+
Value: "text",
29+
},
2530
},
2631
Subcommands: []*cli.Command{
2732
{

cmd/namigo/sub/search.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func SearchPackageAction(c *cli.Context) error {
2020
return ErrMissingTerm
2121
}
2222
maxResults := c.Int("max")
23+
outputMode := getOutputMode(c.String("mode"))
2324

2425
ptf := newSearchPortfolio()
2526
ptfErrorCount := 0
@@ -73,9 +74,9 @@ func SearchPackageAction(c *cli.Context) error {
7374

7475
f := &searchFormatter{}
7576

76-
util.PrintResults(ptf.results.golang, "Golang", f.formatGo)
77-
util.PrintResults(ptf.results.npm, "NPM", f.formatNPM)
78-
util.PrintResults(ptf.results.pypi, "PyPI", f.formatPyPI)
77+
util.PrintResults(ptf.results.golang, "Golang", f.formatGo, outputMode)
78+
util.PrintResults(ptf.results.npm, "NPM", f.formatNPM, outputMode)
79+
util.PrintResults(ptf.results.pypi, "PyPI", f.formatPyPI, outputMode)
7980

8081
return nil
8182
}
@@ -87,6 +88,7 @@ func SearchDNSAction(c *cli.Context) error {
8788
return ErrMissingTerm
8889
}
8990
maxResults := c.Int("max")
91+
outputMode := getOutputMode(c.String("mode"))
9092

9193
ptf := newSearchPortfolio()
9294
ptfErrorCount := 0
@@ -118,7 +120,6 @@ func SearchDNSAction(c *cli.Context) error {
118120

119121
f := &searchFormatter{}
120122

121-
util.PrintResults(ptf.results.dns, "DNS", f.formatDNS)
122-
123+
util.PrintResults(ptf.results.dns, "DNS", f.formatDNS, outputMode)
123124
return nil
124125
}

cmd/namigo/sub/search_priv.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"sync"
66

77
"github.com/huangsam/namigo/internal/model"
8+
"github.com/huangsam/namigo/internal/util"
89
)
910

1011
// searchFormatter formats different types of results.
@@ -90,3 +91,15 @@ func (p *searchPortfolio) wait() {
9091
func (p *searchPortfolio) count() int {
9192
return p.c
9293
}
94+
95+
// getOutputMode returns an OutputMode instance.
96+
func getOutputMode(mode string) util.OutputMode {
97+
switch mode {
98+
case "text":
99+
return util.TextMode
100+
case "json":
101+
return util.JSONMode
102+
default:
103+
return util.TextMode
104+
}
105+
}

internal/util/output.go

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,63 @@ package util
33
import (
44
"fmt"
55

6+
"encoding/json"
7+
68
"github.com/huangsam/namigo/internal/model"
79
)
810

9-
func PrintResults(results any, label string, format func(any) string) {
11+
type OutputMode int
12+
13+
const (
14+
TextMode OutputMode = iota
15+
JSONMode
16+
)
17+
18+
func (o OutputMode) String() string {
19+
switch o {
20+
case TextMode:
21+
return "PlainText"
22+
case JSONMode:
23+
return "JSON"
24+
default:
25+
return "Unknown"
26+
}
27+
}
28+
29+
func PrintResults(results any, label string, format func(any) string, mode OutputMode) {
1030
switch res := results.(type) {
1131
case []model.GoPackageResult:
1232
if len(res) > 0 {
13-
fmt.Printf("%d %s results found:\n", len(res), label)
14-
for _, r := range res {
15-
fmt.Println(format(r))
16-
}
33+
printResults(res, label, format, mode)
1734
}
1835
case []model.NPMPackageResult:
1936
if len(res) > 0 {
20-
fmt.Printf("%d %s results found:\n", len(res), label)
21-
for _, r := range res {
22-
fmt.Println(format(r))
23-
}
37+
printResults(res, label, format, mode)
2438
}
2539
case []model.PyPIPackageResult:
2640
if len(res) > 0 {
27-
fmt.Printf("%d %s results found:\n", len(res), label)
28-
for _, r := range res {
29-
fmt.Println(format(r))
30-
}
41+
printResults(res, label, format, mode)
3142
}
3243
case []model.DNSResult:
3344
if len(res) > 0 {
34-
fmt.Printf("%d %s results found:\n", len(res), label)
35-
for _, r := range res {
36-
fmt.Println(format(r))
37-
}
45+
printResults(res, label, format, mode)
46+
}
47+
}
48+
}
49+
50+
func printResults[T any](results []T, label string, format func(any) string, mode OutputMode) {
51+
switch mode {
52+
case JSONMode:
53+
jsonData, err := json.MarshalIndent(results, "", " ")
54+
if err != nil {
55+
fmt.Printf("Cannot print %s for %s: %v\n", mode, label, err)
56+
return
57+
}
58+
fmt.Printf("%s: %s\n", label, jsonData)
59+
case TextMode:
60+
fmt.Printf("%d %s results found:\n", len(results), label)
61+
for _, r := range results {
62+
fmt.Println(format(r))
3863
}
3964
}
4065
}

0 commit comments

Comments
 (0)