Skip to content

Commit

Permalink
Code grooming
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasmalkmus committed Feb 25, 2021
1 parent 572ad4f commit 01ee28e
Show file tree
Hide file tree
Showing 11 changed files with 268 additions and 142 deletions.
16 changes: 15 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ GEN_CLI_DOCS := bin/gen-cli-docs
GOLANGCI_LINT := bin/golangci-lint
GORELEASER := bin/goreleaser
GOTESTSUM := bin/gotestsum
STRINGER := bin/stringer

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

.PHONY: all
all: dep fmt lint test build man ## Run dep, fmt, lint, test, build and man
all: dep generate fmt lint test build man ## Run dep, fmt, lint, test, build and man

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

.PHONY: generate
generate: $(STRINGER) pkg/iofmt/format_string.go ## Generate code using `go generate`

.PHONY: install
install: $(GOPATH)/bin/axiom ## Install the binary into the $GOPATH/bin directory

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

# GO GENERATE TARGETS

pkg/iofmt/%_string.go: pkg/iofmt/%.go
@echo ">> generating $@ from $<"
@$(GO) generate $<

# MISC TARGETS

$(COVERPROFILE):
Expand Down Expand Up @@ -151,3 +161,7 @@ $(GORELEASER): dep.stamp $(call go-pkg-sourcefiles, github.com/goreleaser/gorele
$(GOTESTSUM): dep.stamp $(call go-pkg-sourcefiles, gotest.tools/gotestsum)
@echo ">> installing gotestsum"
@$(GO) install gotest.tools/gotestsum

$(STRINGER): dep.stamp $(call go-pkg-sourcefiles, golang.org/x/tools/cmd/stringer)
@echo ">> installing stringer"
@$(GO) install golang.org/x/tools/cmd/stringer
15 changes: 5 additions & 10 deletions cmd/axiom/dataset/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@ import (
"github.com/spf13/cobra"

"github.com/axiomhq/cli/internal/cmdutil"
"github.com/axiomhq/cli/pkg/iofmt"
)

const (
formatJSON = "json"
)

var validFormats = []string{formatJSON}

// NewDatasetCmd creates and returns the dataset command.
func NewDatasetCmd(f *cmdutil.Factory) *cobra.Command {
cmd := &cobra.Command{
Expand Down Expand Up @@ -74,10 +69,10 @@ func getDatasetNames(ctx context.Context, f *cmdutil.Factory) ([]string, error)
}

func formatCompletion(_ *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) {
res := make([]string, 0, len(validFormats))
for _, validFormat := range validFormats {
if strings.HasPrefix(validFormat, toComplete) {
res = append(res, validFormat)
res := make([]string, 0, len(iofmt.Formats()))
for _, validFormat := range iofmt.Formats() {
if strings.HasPrefix(validFormat.String(), toComplete) {
res = append(res, validFormat.String())
}
}
return res, cobra.ShellCompDirectiveNoFileComp
Expand Down
64 changes: 27 additions & 37 deletions cmd/axiom/dataset/dataset_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package dataset

import (
"context"
"encoding/json"
"fmt"
"io"
"strconv"
"time"

"github.com/AlecAivazis/survey/v2"
"github.com/MakeNowJust/heredoc"
"github.com/nwidger/jsoncolor"
"github.com/spf13/cobra"

"github.com/axiomhq/cli/internal/cmdutil"
"github.com/axiomhq/cli/pkg/terminal"
"github.com/axiomhq/cli/pkg/iofmt"
)

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

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

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

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

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

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

if opts.Format == formatJSON {
var enc interface {
Encode(interface{}) error
}
if opts.IO.ColorEnabled() {
enc = jsoncolor.NewEncoder(opts.IO.Out())
} else {
enc = json.NewEncoder(opts.IO.Out())
}

return enc.Encode(dataset)
if opts.Format == iofmt.JSON.String() {
return iofmt.FormatToJSON(opts.IO.Out(), dataset, opts.IO.ColorEnabled())
}

cs := opts.IO.ColorScheme()
tp := terminal.NewTablePrinter(opts.IO)

var header iofmt.HeaderBuilderFunc
if opts.IO.IsStdoutTTY() {
fmt.Fprintf(opts.IO.Out(), "Showing info of dataset %s:\n\n", cs.Bold(dataset.Name))
tp.AddField("Events", cs.Bold)
tp.AddField("Blocks", cs.Bold)
tp.AddField("Fields", cs.Bold)
tp.AddField("Ingested Bytes", cs.Bold)
tp.AddField("Compressed Bytes", cs.Bold)
tp.AddField("Min Time", cs.Bold)
tp.AddField("Max Time", cs.Bold)
tp.EndRow()
tp.AddField("", nil)
tp.EndRow()
header = func(w io.Writer, trb iofmt.TableRowBuilder) {
fmt.Fprintf(opts.IO.Out(), "Showing info of dataset %s:\n\n", cs.Bold(dataset.Name))
trb.AddField("Events", cs.Bold)
trb.AddField("Blocks", cs.Bold)
trb.AddField("Fields", cs.Bold)
trb.AddField("Ingested Bytes", cs.Bold)
trb.AddField("Compressed Bytes", cs.Bold)
trb.AddField("Min Time", cs.Bold)
trb.AddField("Max Time", cs.Bold)
}
}

tp.AddField(strconv.Itoa(int(dataset.NumEvents)), nil)
tp.AddField(strconv.Itoa(int(dataset.NumBlocks)), nil)
tp.AddField(strconv.Itoa(int(dataset.NumFields)), nil)
tp.AddField(dataset.InputBytesHuman, nil)
tp.AddField(dataset.CompressedBytesHuman, nil)
tp.AddField(dataset.MinTime.Format(time.RFC1123), cs.Gray)
tp.AddField(dataset.MaxTime.Format(time.RFC1123), cs.Gray)
tp.EndRow()
contentRow := func(trb iofmt.TableRowBuilder, _ int) {
trb.AddField(strconv.Itoa(int(dataset.NumEvents)), nil)
trb.AddField(strconv.Itoa(int(dataset.NumBlocks)), nil)
trb.AddField(strconv.Itoa(int(dataset.NumFields)), nil)
trb.AddField(dataset.InputBytesHuman, nil)
trb.AddField(dataset.CompressedBytesHuman, nil)
trb.AddField(dataset.MinTime.Format(time.RFC1123), cs.Gray)
trb.AddField(dataset.MaxTime.Format(time.RFC1123), cs.Gray)
}

return tp.Render()
return iofmt.FormatToTable(opts.IO, 1, header, nil, contentRow)
}
51 changes: 21 additions & 30 deletions cmd/axiom/dataset/dataset_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package dataset

import (
"context"
"encoding/json"
"fmt"
"io"
"time"

"github.com/MakeNowJust/heredoc"
"github.com/nwidger/jsoncolor"
"github.com/spf13/cobra"

"github.com/axiomhq/cli/internal/cmdutil"
"github.com/axiomhq/cli/pkg/terminal"
"github.com/axiomhq/cli/pkg/iofmt"
"github.com/axiomhq/cli/pkg/utils"
)

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

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

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

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

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

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

if opts.Format == formatJSON {
var enc interface {
Encode(interface{}) error
}
if opts.IO.ColorEnabled() {
enc = jsoncolor.NewEncoder(opts.IO.Out())
} else {
enc = json.NewEncoder(opts.IO.Out())
}

return enc.Encode(datasets)
if opts.Format == iofmt.JSON.String() {
return iofmt.FormatToJSON(opts.IO.Out(), datasets, opts.IO.ColorEnabled())
}

cs := opts.IO.ColorScheme()
tp := terminal.NewTablePrinter(opts.IO)

var header iofmt.HeaderBuilderFunc
if opts.IO.IsStdoutTTY() {
fmt.Fprintf(opts.IO.Out(), "Showing %s:\n\n", utils.Pluralize(cs, "dataset", len(datasets)))
tp.AddField("Name", cs.Bold)
tp.AddField("Description", cs.Bold)
tp.AddField("Created", cs.Bold)
tp.EndRow()
tp.AddField("", nil)
tp.EndRow()
header = func(w io.Writer, trb iofmt.TableRowBuilder) {
fmt.Fprintf(opts.IO.Out(), "Showing %s:\n\n", utils.Pluralize(cs, "dataset", len(datasets)))
trb.AddField("Name", cs.Bold)
trb.AddField("Description", cs.Bold)
trb.AddField("Created", cs.Bold)
}
}

for _, dataset := range datasets {
tp.AddField(dataset.Name, nil)
tp.AddField(dataset.Description, nil)
tp.AddField(dataset.Created.Format(time.RFC1123), cs.Gray)
tp.EndRow()
contentRow := func(trb iofmt.TableRowBuilder, k int) {
dataset := datasets[k]

trb.AddField(dataset.Name, nil)
trb.AddField(dataset.Description, nil)
trb.AddField(dataset.Created.Format(time.RFC1123), cs.Gray)
}
return tp.Render()

return iofmt.FormatToTable(opts.IO, len(datasets), header, nil, contentRow)
}
84 changes: 36 additions & 48 deletions cmd/axiom/dataset/dataset_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package dataset

import (
"context"
"encoding/json"
"fmt"
"io"
"strconv"
"time"

"github.com/MakeNowJust/heredoc"
"github.com/nwidger/jsoncolor"
"github.com/spf13/cobra"

"github.com/axiomhq/cli/internal/cmdutil"
"github.com/axiomhq/cli/pkg/terminal"
"github.com/axiomhq/cli/pkg/iofmt"
)

type statsOptions struct {
Expand Down Expand Up @@ -79,59 +78,48 @@ func runStats(ctx context.Context, opts *statsOptions) error {
}
defer pagerStop()

if opts.Format == formatJSON {
var enc interface {
Encode(interface{}) error
}
if opts.IO.ColorEnabled() {
enc = jsoncolor.NewEncoder(opts.IO.Out())
} else {
enc = json.NewEncoder(opts.IO.Out())
}

return enc.Encode(stats)
if opts.Format == iofmt.JSON.String() {
return iofmt.FormatToJSON(opts.IO.Out(), stats, opts.IO.ColorEnabled())
}

cs := opts.IO.ColorScheme()
tp := terminal.NewTablePrinter(opts.IO)

var header iofmt.HeaderBuilderFunc
if opts.IO.IsStdoutTTY() {
fmt.Fprintf(opts.IO.Out(), "Showing statistics of all dataset:\n\n")
tp.AddField("Name", cs.Bold)
tp.AddField("Events", cs.Bold)
tp.AddField("Blocks", cs.Bold)
tp.AddField("Fields", cs.Bold)
tp.AddField("Ingested", cs.Bold)
tp.AddField("Compressed", cs.Bold)
tp.AddField("Min Time", cs.Bold)
tp.AddField("Max Time", cs.Bold)
tp.EndRow()
tp.AddField("", nil)
tp.EndRow()
header = func(w io.Writer, trb iofmt.TableRowBuilder) {
fmt.Fprintf(opts.IO.Out(), "Showing statistics of all dataset:\n\n")
trb.AddField("Name", cs.Bold)
trb.AddField("Events", cs.Bold)
trb.AddField("Blocks", cs.Bold)
trb.AddField("Fields", cs.Bold)
trb.AddField("Ingested", cs.Bold)
trb.AddField("Compressed", cs.Bold)
trb.AddField("Min Time", cs.Bold)
trb.AddField("Max Time", cs.Bold)
}
}

for _, dataset := range stats.Datasets {
tp.AddField(dataset.Name, nil)
tp.AddField(strconv.Itoa(int(dataset.NumEvents)), nil)
tp.AddField(strconv.Itoa(int(dataset.NumBlocks)), nil)
tp.AddField(strconv.Itoa(int(dataset.NumFields)), nil)
tp.AddField(dataset.InputBytesHuman, cs.Green)
tp.AddField(dataset.CompressedBytesHuman, cs.Green)
tp.AddField(dataset.MinTime.Format(time.RFC1123), cs.Gray)
tp.AddField(dataset.MaxTime.Format(time.RFC1123), cs.Gray)
tp.EndRow()
contentRow := func(trb iofmt.TableRowBuilder, k int) {
dataset := stats.Datasets[k]

trb.AddField(dataset.Name, nil)
trb.AddField(strconv.Itoa(int(dataset.NumEvents)), nil)
trb.AddField(strconv.Itoa(int(dataset.NumBlocks)), nil)
trb.AddField(strconv.Itoa(int(dataset.NumFields)), nil)
trb.AddField(dataset.InputBytesHuman, cs.Green)
trb.AddField(dataset.CompressedBytesHuman, cs.Green)
trb.AddField(dataset.MinTime.Format(time.RFC1123), cs.Gray)
trb.AddField(dataset.MaxTime.Format(time.RFC1123), cs.Gray)
}

tp.AddField("", nil)
tp.EndRow()

tp.AddField("Sum", cs.Bold)
tp.AddField(strconv.Itoa(int(stats.NumEvents)), cs.Bold)
tp.AddField(strconv.Itoa(int(stats.NumBlocks)), cs.Bold)
tp.AddField("", nil)
tp.AddField(stats.InputBytesHuman, func(s string) string { return cs.Bold(cs.Green(s)) })
tp.AddField(stats.CompressedBytesHuman, func(s string) string { return cs.Bold(cs.Green(s)) })
tp.EndRow()
footer := func(_ io.Writer, trb iofmt.TableRowBuilder) {
trb.AddField("Sum", cs.Bold)
trb.AddField(strconv.Itoa(int(stats.NumEvents)), cs.Bold)
trb.AddField(strconv.Itoa(int(stats.NumBlocks)), cs.Bold)
trb.AddField("", nil)
trb.AddField(stats.InputBytesHuman, func(s string) string { return cs.Bold(cs.Green(s)) })
trb.AddField(stats.CompressedBytesHuman, func(s string) string { return cs.Bold(cs.Green(s)) })
}

return tp.Render()
return iofmt.FormatToTable(opts.IO, len(stats.Datasets), header, footer, contentRow)
}
Loading

0 comments on commit 01ee28e

Please sign in to comment.