Skip to content

Commit

Permalink
Merge pull request #115 from batchcorp/blinktag/list_replays
Browse files Browse the repository at this point in the history
List replays and JSON output from batch list commands
  • Loading branch information
blinktag authored Jun 15, 2021
2 parents ae43107 + 34491ee commit dc7f762
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 9 deletions.
17 changes: 16 additions & 1 deletion backends/batch/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}

Expand Down Expand Up @@ -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))
}
2 changes: 1 addition & 1 deletion backends/batch/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}))
})
})
Expand Down
17 changes: 11 additions & 6 deletions backends/batch/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions backends/batch/replays.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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 (
Expand Down Expand Up @@ -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,
Expand All @@ -86,6 +93,7 @@ func (b *Batch) listReplays() ([]ReplayOutput, error) {
Collection: r.ReplayCollection.Name,
Destination: r.ReplayDestination.Name,
Paused: r.Paused,
Status: r.Status,
})
}

Expand Down
10 changes: 10 additions & 0 deletions cli/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type BatchOptions struct {
Notes string
Query string
Page int
OutputType string

// Collection specific
CollectionName string
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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().
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit dc7f762

Please sign in to comment.