Skip to content

Commit

Permalink
Output format selection
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasmalkmus committed Feb 25, 2021
1 parent 440bf6a commit 572ad4f
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 23 deletions.
21 changes: 19 additions & 2 deletions cmd/axiom/dataset/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ package dataset

import (
"context"
"strings"

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

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

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 All @@ -17,10 +24,10 @@ func NewDatasetCmd(f *cmdutil.Factory) *cobra.Command {
Long: "Create, edit and delete datasets.",

Example: heredoc.Doc(`
$ axiom dataset create --name nginx-logs --description "All Nginx logs"
$ axiom dataset create --name=nginx-logs --description="All Nginx logs"
$ axiom dataset list
$ axiom dataset info nginx-logs
$ axiom dataset update nginx-logs --description "Some Nginx logs"
$ axiom dataset update nginx-logs --description="Some Nginx logs"
$ axiom dataset delete nginx-logs
`),

Expand Down Expand Up @@ -65,3 +72,13 @@ func getDatasetNames(ctx context.Context, f *cmdutil.Factory) ([]string, error)

return datasetNames, nil
}

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)
}
}
return res, cobra.ShellCompDirectiveNoFileComp
}
23 changes: 22 additions & 1 deletion cmd/axiom/dataset/dataset_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package dataset

import (
"context"
"encoding/json"
"fmt"
"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"
Expand All @@ -20,6 +22,8 @@ type infoOptions struct {
// Name of the dataset to fetch info of. If not supplied as an argument,
// which is optional, the user will be asked for it.
Name string
// Format to output data in. Defaults to tabular output.
Format string
}

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

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

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

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

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

return cmd
}

Expand Down Expand Up @@ -91,6 +99,19 @@ 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)
}

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

Expand Down
50 changes: 40 additions & 10 deletions cmd/axiom/dataset/dataset_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,33 @@ package dataset

import (
"context"
"encoding/json"
"fmt"
"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/utils"
)

type listOptions struct {
*cmdutil.Factory

// Format to output data in. Defaults to tabular output.
Format string
}

func newListCmd(f *cmdutil.Factory) *cobra.Command {
opts := &listOptions{
Factory: f,
}

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

Aliases: []string{"ls"},
Expand All @@ -28,38 +41,55 @@ func newListCmd(f *cmdutil.Factory) *cobra.Command {
PreRunE: cmdutil.NeedsDatasets(f),

RunE: func(cmd *cobra.Command, args []string) error {
return runList(cmd.Context(), f)
return runList(cmd.Context(), opts)
},
}

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

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

return cmd
}

func runList(ctx context.Context, f *cmdutil.Factory) error {
client, err := f.Client()
func runList(ctx context.Context, opts *listOptions) error {
client, err := opts.Client()
if err != nil {
return err
}

progStop := f.IO.StartActivityIndicator()
progStop := opts.IO.StartActivityIndicator()
datasets, err := client.Datasets.List(ctx)
if err != nil {
progStop()
return err
}
progStop()

pagerStop, err := f.IO.StartPager(ctx)
pagerStop, err := opts.IO.StartPager(ctx)
if err != nil {
return err
}
defer pagerStop()

cs := f.IO.ColorScheme()
tp := terminal.NewTablePrinter(f.IO)
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)
}

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

if f.IO.IsStdoutTTY() {
fmt.Fprintf(f.IO.Out(), "Showing %s:\n\n", utils.Pluralize(cs, "dataset", len(datasets)))
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)
Expand Down
50 changes: 40 additions & 10 deletions cmd/axiom/dataset/dataset_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,33 @@ package dataset

import (
"context"
"encoding/json"
"fmt"
"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"
)

type statsOptions struct {
*cmdutil.Factory

// Format to output data in. Defaults to tabular output.
Format string
}

func newStatsCmd(f *cmdutil.Factory) *cobra.Command {
opts := &statsOptions{
Factory: f,
}

cmd := &cobra.Command{
Use: "stats",
Use: "stats [(-f|--format=)json]",
Short: "Get statistics about all datasets",
Long: heredoc.Doc(`
Get statistics about all datasets.
Expand All @@ -35,38 +48,55 @@ func newStatsCmd(f *cmdutil.Factory) *cobra.Command {
PreRunE: cmdutil.NeedsDatasets(f),

RunE: func(cmd *cobra.Command, _ []string) error {
return runStats(cmd.Context(), f)
return runStats(cmd.Context(), opts)
},
}

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

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

return cmd
}

func runStats(ctx context.Context, f *cmdutil.Factory) error {
client, err := f.Client()
func runStats(ctx context.Context, opts *statsOptions) error {
client, err := opts.Client()
if err != nil {
return err
}

progStop := f.IO.StartActivityIndicator()
progStop := opts.IO.StartActivityIndicator()
stats, err := client.Datasets.Stats(ctx)
if err != nil {
progStop()
return err
}
progStop()

pagerStop, err := f.IO.StartPager(ctx)
pagerStop, err := opts.IO.StartPager(ctx)
if err != nil {
return err
}
defer pagerStop()

cs := f.IO.ColorScheme()
tp := terminal.NewTablePrinter(f.IO)
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)
}

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

if f.IO.IsStdoutTTY() {
fmt.Fprintf(f.IO.Out(), "Showing statistics of all dataset:\n\n")
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)
Expand Down

0 comments on commit 572ad4f

Please sign in to comment.