Skip to content

Commit 01ee28e

Browse files
committed
Code grooming
1 parent 572ad4f commit 01ee28e

File tree

11 files changed

+268
-142
lines changed

11 files changed

+268
-142
lines changed

Makefile

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ GEN_CLI_DOCS := bin/gen-cli-docs
1919
GOLANGCI_LINT := bin/golangci-lint
2020
GORELEASER := bin/goreleaser
2121
GOTESTSUM := bin/gotestsum
22+
STRINGER := bin/stringer
2223

2324
# MISC
2425
COVERPROFILE := coverage.out
@@ -55,7 +56,7 @@ go-list-pkg-sources = $(GO) list -f '{{range .GoFiles}}{{$$.Dir}}/{{.}} {{end}}'
5556
go-pkg-sourcefiles = $(shell $(call go-list-pkg-sources,$(strip $1)))
5657

5758
.PHONY: all
58-
all: dep fmt lint test build man ## Run dep, fmt, lint, test, build and man
59+
all: dep generate fmt lint test build man ## Run dep, fmt, lint, test, build and man
5960

6061
.PHONY: build
6162
build: $(GORELEASER) dep.stamp $(call go-pkg-sourcefiles, ./...) ## Build the binaries
@@ -97,6 +98,9 @@ fmt: ## Format and simplify the source code using `gofmt`
9798
@echo ">> formatting code"
9899
@! $(GOFMT) -s -w $(shell find . -path -prune -o -name '*.go' -print) | grep '^'
99100

101+
.PHONY: generate
102+
generate: $(STRINGER) pkg/iofmt/format_string.go ## Generate code using `go generate`
103+
100104
.PHONY: install
101105
install: $(GOPATH)/bin/axiom ## Install the binary into the $GOPATH/bin directory
102106

@@ -123,6 +127,12 @@ tools: $(GEN_CLI_DOCS) $(GOLANGCI_LINT) $(GORELEASER) $(GOTESTSUM) ## Install al
123127
help:
124128
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
125129

130+
# GO GENERATE TARGETS
131+
132+
pkg/iofmt/%_string.go: pkg/iofmt/%.go
133+
@echo ">> generating $@ from $<"
134+
@$(GO) generate $<
135+
126136
# MISC TARGETS
127137

128138
$(COVERPROFILE):
@@ -151,3 +161,7 @@ $(GORELEASER): dep.stamp $(call go-pkg-sourcefiles, github.com/goreleaser/gorele
151161
$(GOTESTSUM): dep.stamp $(call go-pkg-sourcefiles, gotest.tools/gotestsum)
152162
@echo ">> installing gotestsum"
153163
@$(GO) install gotest.tools/gotestsum
164+
165+
$(STRINGER): dep.stamp $(call go-pkg-sourcefiles, golang.org/x/tools/cmd/stringer)
166+
@echo ">> installing stringer"
167+
@$(GO) install golang.org/x/tools/cmd/stringer

cmd/axiom/dataset/dataset.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,9 @@ import (
88
"github.com/spf13/cobra"
99

1010
"github.com/axiomhq/cli/internal/cmdutil"
11+
"github.com/axiomhq/cli/pkg/iofmt"
1112
)
1213

13-
const (
14-
formatJSON = "json"
15-
)
16-
17-
var validFormats = []string{formatJSON}
18-
1914
// NewDatasetCmd creates and returns the dataset command.
2015
func NewDatasetCmd(f *cmdutil.Factory) *cobra.Command {
2116
cmd := &cobra.Command{
@@ -74,10 +69,10 @@ func getDatasetNames(ctx context.Context, f *cmdutil.Factory) ([]string, error)
7469
}
7570

7671
func formatCompletion(_ *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
77-
res := make([]string, 0, len(validFormats))
78-
for _, validFormat := range validFormats {
79-
if strings.HasPrefix(validFormat, toComplete) {
80-
res = append(res, validFormat)
72+
res := make([]string, 0, len(iofmt.Formats()))
73+
for _, validFormat := range iofmt.Formats() {
74+
if strings.HasPrefix(validFormat.String(), toComplete) {
75+
res = append(res, validFormat.String())
8176
}
8277
}
8378
return res, cobra.ShellCompDirectiveNoFileComp

cmd/axiom/dataset/dataset_info.go

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ package dataset
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
6+
"io"
77
"strconv"
88
"time"
99

1010
"github.com/AlecAivazis/survey/v2"
1111
"github.com/MakeNowJust/heredoc"
12-
"github.com/nwidger/jsoncolor"
1312
"github.com/spf13/cobra"
1413

1514
"github.com/axiomhq/cli/internal/cmdutil"
16-
"github.com/axiomhq/cli/pkg/terminal"
15+
"github.com/axiomhq/cli/pkg/iofmt"
1716
)
1817

1918
type infoOptions struct {
@@ -32,7 +31,7 @@ func newInfoCmd(f *cmdutil.Factory) *cobra.Command {
3231
}
3332

3433
cmd := &cobra.Command{
35-
Use: "info [<dataset-name>] [(-f|--format=)json]",
34+
Use: "info [<dataset-name>] [(-f|--format=)json|table]",
3635
Short: "Get info about a dataset",
3736

3837
Args: cmdutil.PopulateFromArgs(f, &opts.Name),
@@ -56,7 +55,7 @@ func newInfoCmd(f *cmdutil.Factory) *cobra.Command {
5655
},
5756
}
5857

59-
cmd.Flags().StringVarP(&opts.Format, "format", "f", "", "Format to output data in")
58+
cmd.Flags().StringVarP(&opts.Format, "format", "f", iofmt.Table.String(), "Format to output data in")
6059

6160
_ = cmd.RegisterFlagCompletionFunc("format", formatCompletion)
6261

@@ -99,44 +98,35 @@ func runInfo(ctx context.Context, opts *infoOptions) error {
9998
}
10099
defer pagerStop()
101100

102-
if opts.Format == formatJSON {
103-
var enc interface {
104-
Encode(interface{}) error
105-
}
106-
if opts.IO.ColorEnabled() {
107-
enc = jsoncolor.NewEncoder(opts.IO.Out())
108-
} else {
109-
enc = json.NewEncoder(opts.IO.Out())
110-
}
111-
112-
return enc.Encode(dataset)
101+
if opts.Format == iofmt.JSON.String() {
102+
return iofmt.FormatToJSON(opts.IO.Out(), dataset, opts.IO.ColorEnabled())
113103
}
114104

115105
cs := opts.IO.ColorScheme()
116-
tp := terminal.NewTablePrinter(opts.IO)
117106

107+
var header iofmt.HeaderBuilderFunc
118108
if opts.IO.IsStdoutTTY() {
119-
fmt.Fprintf(opts.IO.Out(), "Showing info of dataset %s:\n\n", cs.Bold(dataset.Name))
120-
tp.AddField("Events", cs.Bold)
121-
tp.AddField("Blocks", cs.Bold)
122-
tp.AddField("Fields", cs.Bold)
123-
tp.AddField("Ingested Bytes", cs.Bold)
124-
tp.AddField("Compressed Bytes", cs.Bold)
125-
tp.AddField("Min Time", cs.Bold)
126-
tp.AddField("Max Time", cs.Bold)
127-
tp.EndRow()
128-
tp.AddField("", nil)
129-
tp.EndRow()
109+
header = func(w io.Writer, trb iofmt.TableRowBuilder) {
110+
fmt.Fprintf(opts.IO.Out(), "Showing info of dataset %s:\n\n", cs.Bold(dataset.Name))
111+
trb.AddField("Events", cs.Bold)
112+
trb.AddField("Blocks", cs.Bold)
113+
trb.AddField("Fields", cs.Bold)
114+
trb.AddField("Ingested Bytes", cs.Bold)
115+
trb.AddField("Compressed Bytes", cs.Bold)
116+
trb.AddField("Min Time", cs.Bold)
117+
trb.AddField("Max Time", cs.Bold)
118+
}
130119
}
131120

132-
tp.AddField(strconv.Itoa(int(dataset.NumEvents)), nil)
133-
tp.AddField(strconv.Itoa(int(dataset.NumBlocks)), nil)
134-
tp.AddField(strconv.Itoa(int(dataset.NumFields)), nil)
135-
tp.AddField(dataset.InputBytesHuman, nil)
136-
tp.AddField(dataset.CompressedBytesHuman, nil)
137-
tp.AddField(dataset.MinTime.Format(time.RFC1123), cs.Gray)
138-
tp.AddField(dataset.MaxTime.Format(time.RFC1123), cs.Gray)
139-
tp.EndRow()
121+
contentRow := func(trb iofmt.TableRowBuilder, _ int) {
122+
trb.AddField(strconv.Itoa(int(dataset.NumEvents)), nil)
123+
trb.AddField(strconv.Itoa(int(dataset.NumBlocks)), nil)
124+
trb.AddField(strconv.Itoa(int(dataset.NumFields)), nil)
125+
trb.AddField(dataset.InputBytesHuman, nil)
126+
trb.AddField(dataset.CompressedBytesHuman, nil)
127+
trb.AddField(dataset.MinTime.Format(time.RFC1123), cs.Gray)
128+
trb.AddField(dataset.MaxTime.Format(time.RFC1123), cs.Gray)
129+
}
140130

141-
return tp.Render()
131+
return iofmt.FormatToTable(opts.IO, 1, header, nil, contentRow)
142132
}

cmd/axiom/dataset/dataset_list.go

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@ package dataset
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
6+
"io"
77
"time"
88

99
"github.com/MakeNowJust/heredoc"
10-
"github.com/nwidger/jsoncolor"
1110
"github.com/spf13/cobra"
1211

1312
"github.com/axiomhq/cli/internal/cmdutil"
14-
"github.com/axiomhq/cli/pkg/terminal"
13+
"github.com/axiomhq/cli/pkg/iofmt"
1514
"github.com/axiomhq/cli/pkg/utils"
1615
)
1716

@@ -28,7 +27,7 @@ func newListCmd(f *cmdutil.Factory) *cobra.Command {
2827
}
2928

3029
cmd := &cobra.Command{
31-
Use: "list [(-f|--format=)json]",
30+
Use: "list [(-f|--format=)json|table]",
3231
Short: "List all datasets",
3332

3433
Aliases: []string{"ls"},
@@ -45,7 +44,7 @@ func newListCmd(f *cmdutil.Factory) *cobra.Command {
4544
},
4645
}
4746

48-
cmd.Flags().StringVarP(&opts.Format, "format", "f", "", "Format to output data in")
47+
cmd.Flags().StringVarP(&opts.Format, "format", "f", iofmt.Table.String(), "Format to output data in")
4948

5049
_ = cmd.RegisterFlagCompletionFunc("format", formatCompletion)
5150

@@ -72,37 +71,29 @@ func runList(ctx context.Context, opts *listOptions) error {
7271
}
7372
defer pagerStop()
7473

75-
if opts.Format == formatJSON {
76-
var enc interface {
77-
Encode(interface{}) error
78-
}
79-
if opts.IO.ColorEnabled() {
80-
enc = jsoncolor.NewEncoder(opts.IO.Out())
81-
} else {
82-
enc = json.NewEncoder(opts.IO.Out())
83-
}
84-
85-
return enc.Encode(datasets)
74+
if opts.Format == iofmt.JSON.String() {
75+
return iofmt.FormatToJSON(opts.IO.Out(), datasets, opts.IO.ColorEnabled())
8676
}
8777

8878
cs := opts.IO.ColorScheme()
89-
tp := terminal.NewTablePrinter(opts.IO)
9079

80+
var header iofmt.HeaderBuilderFunc
9181
if opts.IO.IsStdoutTTY() {
92-
fmt.Fprintf(opts.IO.Out(), "Showing %s:\n\n", utils.Pluralize(cs, "dataset", len(datasets)))
93-
tp.AddField("Name", cs.Bold)
94-
tp.AddField("Description", cs.Bold)
95-
tp.AddField("Created", cs.Bold)
96-
tp.EndRow()
97-
tp.AddField("", nil)
98-
tp.EndRow()
82+
header = func(w io.Writer, trb iofmt.TableRowBuilder) {
83+
fmt.Fprintf(opts.IO.Out(), "Showing %s:\n\n", utils.Pluralize(cs, "dataset", len(datasets)))
84+
trb.AddField("Name", cs.Bold)
85+
trb.AddField("Description", cs.Bold)
86+
trb.AddField("Created", cs.Bold)
87+
}
9988
}
10089

101-
for _, dataset := range datasets {
102-
tp.AddField(dataset.Name, nil)
103-
tp.AddField(dataset.Description, nil)
104-
tp.AddField(dataset.Created.Format(time.RFC1123), cs.Gray)
105-
tp.EndRow()
90+
contentRow := func(trb iofmt.TableRowBuilder, k int) {
91+
dataset := datasets[k]
92+
93+
trb.AddField(dataset.Name, nil)
94+
trb.AddField(dataset.Description, nil)
95+
trb.AddField(dataset.Created.Format(time.RFC1123), cs.Gray)
10696
}
107-
return tp.Render()
97+
98+
return iofmt.FormatToTable(opts.IO, len(datasets), header, nil, contentRow)
10899
}

cmd/axiom/dataset/dataset_stats.go

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ package dataset
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
6+
"io"
77
"strconv"
88
"time"
99

1010
"github.com/MakeNowJust/heredoc"
11-
"github.com/nwidger/jsoncolor"
1211
"github.com/spf13/cobra"
1312

1413
"github.com/axiomhq/cli/internal/cmdutil"
15-
"github.com/axiomhq/cli/pkg/terminal"
14+
"github.com/axiomhq/cli/pkg/iofmt"
1615
)
1716

1817
type statsOptions struct {
@@ -79,59 +78,48 @@ func runStats(ctx context.Context, opts *statsOptions) error {
7978
}
8079
defer pagerStop()
8180

82-
if opts.Format == formatJSON {
83-
var enc interface {
84-
Encode(interface{}) error
85-
}
86-
if opts.IO.ColorEnabled() {
87-
enc = jsoncolor.NewEncoder(opts.IO.Out())
88-
} else {
89-
enc = json.NewEncoder(opts.IO.Out())
90-
}
91-
92-
return enc.Encode(stats)
81+
if opts.Format == iofmt.JSON.String() {
82+
return iofmt.FormatToJSON(opts.IO.Out(), stats, opts.IO.ColorEnabled())
9383
}
9484

9585
cs := opts.IO.ColorScheme()
96-
tp := terminal.NewTablePrinter(opts.IO)
9786

87+
var header iofmt.HeaderBuilderFunc
9888
if opts.IO.IsStdoutTTY() {
99-
fmt.Fprintf(opts.IO.Out(), "Showing statistics of all dataset:\n\n")
100-
tp.AddField("Name", cs.Bold)
101-
tp.AddField("Events", cs.Bold)
102-
tp.AddField("Blocks", cs.Bold)
103-
tp.AddField("Fields", cs.Bold)
104-
tp.AddField("Ingested", cs.Bold)
105-
tp.AddField("Compressed", cs.Bold)
106-
tp.AddField("Min Time", cs.Bold)
107-
tp.AddField("Max Time", cs.Bold)
108-
tp.EndRow()
109-
tp.AddField("", nil)
110-
tp.EndRow()
89+
header = func(w io.Writer, trb iofmt.TableRowBuilder) {
90+
fmt.Fprintf(opts.IO.Out(), "Showing statistics of all dataset:\n\n")
91+
trb.AddField("Name", cs.Bold)
92+
trb.AddField("Events", cs.Bold)
93+
trb.AddField("Blocks", cs.Bold)
94+
trb.AddField("Fields", cs.Bold)
95+
trb.AddField("Ingested", cs.Bold)
96+
trb.AddField("Compressed", cs.Bold)
97+
trb.AddField("Min Time", cs.Bold)
98+
trb.AddField("Max Time", cs.Bold)
99+
}
111100
}
112101

113-
for _, dataset := range stats.Datasets {
114-
tp.AddField(dataset.Name, nil)
115-
tp.AddField(strconv.Itoa(int(dataset.NumEvents)), nil)
116-
tp.AddField(strconv.Itoa(int(dataset.NumBlocks)), nil)
117-
tp.AddField(strconv.Itoa(int(dataset.NumFields)), nil)
118-
tp.AddField(dataset.InputBytesHuman, cs.Green)
119-
tp.AddField(dataset.CompressedBytesHuman, cs.Green)
120-
tp.AddField(dataset.MinTime.Format(time.RFC1123), cs.Gray)
121-
tp.AddField(dataset.MaxTime.Format(time.RFC1123), cs.Gray)
122-
tp.EndRow()
102+
contentRow := func(trb iofmt.TableRowBuilder, k int) {
103+
dataset := stats.Datasets[k]
104+
105+
trb.AddField(dataset.Name, nil)
106+
trb.AddField(strconv.Itoa(int(dataset.NumEvents)), nil)
107+
trb.AddField(strconv.Itoa(int(dataset.NumBlocks)), nil)
108+
trb.AddField(strconv.Itoa(int(dataset.NumFields)), nil)
109+
trb.AddField(dataset.InputBytesHuman, cs.Green)
110+
trb.AddField(dataset.CompressedBytesHuman, cs.Green)
111+
trb.AddField(dataset.MinTime.Format(time.RFC1123), cs.Gray)
112+
trb.AddField(dataset.MaxTime.Format(time.RFC1123), cs.Gray)
123113
}
124114

125-
tp.AddField("", nil)
126-
tp.EndRow()
127-
128-
tp.AddField("Sum", cs.Bold)
129-
tp.AddField(strconv.Itoa(int(stats.NumEvents)), cs.Bold)
130-
tp.AddField(strconv.Itoa(int(stats.NumBlocks)), cs.Bold)
131-
tp.AddField("", nil)
132-
tp.AddField(stats.InputBytesHuman, func(s string) string { return cs.Bold(cs.Green(s)) })
133-
tp.AddField(stats.CompressedBytesHuman, func(s string) string { return cs.Bold(cs.Green(s)) })
134-
tp.EndRow()
115+
footer := func(_ io.Writer, trb iofmt.TableRowBuilder) {
116+
trb.AddField("Sum", cs.Bold)
117+
trb.AddField(strconv.Itoa(int(stats.NumEvents)), cs.Bold)
118+
trb.AddField(strconv.Itoa(int(stats.NumBlocks)), cs.Bold)
119+
trb.AddField("", nil)
120+
trb.AddField(stats.InputBytesHuman, func(s string) string { return cs.Bold(cs.Green(s)) })
121+
trb.AddField(stats.CompressedBytesHuman, func(s string) string { return cs.Bold(cs.Green(s)) })
122+
}
135123

136-
return tp.Render()
124+
return iofmt.FormatToTable(opts.IO, len(stats.Datasets), header, footer, contentRow)
137125
}

0 commit comments

Comments
 (0)