diff --git a/backends/batch/batch.go b/backends/batch/batch.go index 53af618c..e36a4f9f 100644 --- a/backends/batch/batch.go +++ b/backends/batch/batch.go @@ -63,11 +63,16 @@ var errNotAuthenticated = errors.New("you are not authenticated. run `plumber ba // New creates a new instance of a Batch struct with defaults func New(opts *cli.Options) *Batch { + printer := printTable + if opts.Batch.OutputType == "json" { + printer = printJSON + } + b := &Batch{ Log: logrus.WithField("pkg", "batch"), Opts: opts, Client: &http.Client{}, - Printer: printTable, + Printer: printer, ApiUrl: "https://api.batch.sh", } @@ -229,3 +234,13 @@ func printTable(v interface{}) { printer.Print(v) } + +// printJSON displays output from batch commands as JSON. Needed for automation purposes +func printJSON(v interface{}) { + output, err := json.Marshal(v) + if err != nil { + fmt.Sprintf(`{"error": "%s"}`, err.Error()) + } + + fmt.Println(string(output)) +} diff --git a/backends/batch/batch_test.go b/backends/batch/batch_test.go index 1f943b16..038fd6cc 100644 --- a/backends/batch/batch_test.go +++ b/backends/batch/batch_test.go @@ -48,7 +48,7 @@ func BatchWithMockResponse(httpCode int, responseBody string) *Batch { var _ = Describe("Batch", func() { Context("New", func() { It("returns a new instance of Batch struct", func() { - b := New(&cli.Options{}) + b := New(&cli.Options{Batch: &cli.BatchOptions{}}) Expect(b).To(BeAssignableToTypeOf(&Batch{})) }) }) diff --git a/backends/batch/collections.go b/backends/batch/collections.go index 48c95604..06fdf1ea 100644 --- a/backends/batch/collections.go +++ b/backends/batch/collections.go @@ -22,17 +22,19 @@ type Collection struct { Name string `json:"name" header:"Name"` Token string `json:"token"` Paused bool `json:"paused" header:"Is Paused?"` + Archived bool `json:"archived" header:"Archived"` *CollectionSchema `json:"schema"` } // CollectionOutput is used for displaying collections as a table type CollectionOutput struct { - Name string `header:"Name"` - ID string `header:"ID"` - Token string `header:"Token"` - Paused bool `header:"Is Paused"` - SchemaName string `header:"Schema Name"` - SchemaType string `header:"Schema Type"` + Name string `header:"Name" json:"name"` + ID string `header:"ID" json:"id"` + Token string `header:"Token" json:"token"` + Paused bool `header:"Is Paused" json:"paused"` + Archived bool `header:"Archived" json:"archived"` + SchemaName string `header:"Schema Name" json:"schema_name"` + SchemaType string `header:"Schema Type" json:"schema_type"` } // SearchResult is used to unmarshal the JSON results of a search API call @@ -87,6 +89,9 @@ func (b *Batch) listCollections() ([]CollectionOutput, error) { output := make([]CollectionOutput, 0) for _, c := range collections { + if c.Archived { + continue + } output = append(output, CollectionOutput{ ID: c.ID, Name: c.Name, diff --git a/backends/batch/replays.go b/backends/batch/replays.go index 804a7441..52869d5e 100644 --- a/backends/batch/replays.go +++ b/backends/batch/replays.go @@ -25,6 +25,8 @@ type Replay struct { Type string `header:"Type" json:"type"` Query string `header:"Query" json:"query"` Paused bool `header:"Is Paused" json:"paused"` + Archived bool `header:"Archived" json:"archived"` + Status string `header:"Status" json:"status"` *ReplayDestination `json:"destination"` *ReplayCollection `json:"collection"` } @@ -38,6 +40,7 @@ type ReplayOutput struct { Collection string `header:"Collection Name"` Destination string `header:"Destination Name"` Paused bool `header:"Is Paused" json:"paused"` + Status string `header:"Status" json:"status"` } var ( @@ -78,6 +81,10 @@ func (b *Batch) listReplays() ([]ReplayOutput, error) { output := make([]ReplayOutput, 0) for _, r := range replays { + if r.Archived { + continue + } + output = append(output, ReplayOutput{ ID: r.ID, Name: r.Name, @@ -86,6 +93,7 @@ func (b *Batch) listReplays() ([]ReplayOutput, error) { Collection: r.ReplayCollection.Name, Destination: r.ReplayDestination.Name, Paused: r.Paused, + Status: r.Status, }) } diff --git a/cli/batch.go b/cli/batch.go index 17d8cf85..68ebdab0 100644 --- a/cli/batch.go +++ b/cli/batch.go @@ -13,6 +13,7 @@ type BatchOptions struct { Notes string Query string Page int + OutputType string // Collection specific CollectionName string @@ -66,6 +67,9 @@ func HandleBatchFlags(batchCmd *kingpin.CmdClause, opts *Options) { listCmd.Command("destination", "Replay destinations") listCmd.Command("schema", "Schemas") listCmd.Command("collection", "Collections") + listCmd.Command("replay", "Replays") + + handleListFlags(listCmd, opts) // Create createCmd := batchCmd.Command("create", "Create a batch resource") @@ -96,6 +100,12 @@ func HandleBatchFlags(batchCmd *kingpin.CmdClause, opts *Options) { IntVar(&opts.Batch.Page) } +func handleListFlags(cmd *kingpin.CmdClause, opts *Options) { + cmd.Flag("output", "Output type"). + Default("table"). + EnumVar(&opts.Batch.OutputType, "table", "json") +} + func handleArchiveReplayFlags(cmd *kingpin.CmdClause, opts *Options) { cmd.Flag("replay-id", "Replay ID"). Required(). diff --git a/main.go b/main.go index 31c33809..3aa194be 100644 --- a/main.go +++ b/main.go @@ -50,7 +50,7 @@ func main() { } // In container mode, force JSON and don't print logo - if !terminal.IsTerminal(int(os.Stderr.Fd())) { + if !terminal.IsTerminal(int(os.Stderr.Fd())) || opts.Batch.OutputType == "json" { logrus.SetFormatter(&logrus.JSONFormatter{}) } else { printer.PrintLogo()