Skip to content

Commit

Permalink
feat(influx): add jsonout and hide headers support to task cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
jsteenb2 committed Mar 27, 2020
1 parent 361777a commit 410a389
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 96 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
1. [17396](https://github.com/influxdata/influxdb/pull/17396): Add module to write line data to specified url, org, and bucket
1. [17448](https://github.com/influxdata/influxdb/pull/17448): Add foundation for pkger stacks, stateful package management
1. [17462](https://github.com/influxdata/influxdb/pull/17462): Flag to disable scheduling of tasks
1. [17470](https://github.com/influxdata/influxdb/pull/17470): Add ability to output cli output as json and hide table headers

### Bug Fixes

Expand Down
206 changes: 110 additions & 96 deletions cmd/influx/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"io"
"os"
"time"

Expand Down Expand Up @@ -38,6 +39,11 @@ func cmdTask(f *globalFlags, opt genericCLIOpts) *cobra.Command {
return cmd
}

var taskPrintFlags struct {
json bool
hideHeaders bool
}

var taskCreateFlags struct {
org organization
}
Expand All @@ -48,6 +54,7 @@ func taskCreateCmd(opt genericCLIOpts) *cobra.Command {
cmd.Short = "Create task"

taskCreateFlags.org.register(cmd, false)
registerPrintOptions(cmd, &taskPrintFlags.hideHeaders, &taskPrintFlags.json)

return cmd
}
Expand Down Expand Up @@ -93,29 +100,14 @@ func taskCreateF(cmd *cobra.Command, args []string) error {
return err
}

w := internal.NewTabWriter(os.Stdout)
w.WriteHeaders(
"ID",
"Name",
"OrganizationID",
"Organization",
"AuthorizationID",
"Status",
"Every",
"Cron",
return printTasks(
cmd.OutOrStdout(),
taskPrintOpts{
hideHeaders: taskPrintFlags.hideHeaders,
json: taskPrintFlags.json,
task: t,
},
)
w.Write(map[string]interface{}{
"ID": t.ID.String(),
"Name": t.Name,
"OrganizationID": t.OrganizationID.String(),
"Organization": t.Organization,
"Status": t.Status,
"Every": t.Every,
"Cron": t.Cron,
})
w.Flush()

return nil
}

var taskFindFlags struct {
Expand All @@ -132,6 +124,7 @@ func taskFindCmd(opt genericCLIOpts) *cobra.Command {
cmd.Aliases = []string{"find", "ls"}

taskFindFlags.org.register(cmd, false)
registerPrintOptions(cmd, &taskPrintFlags.hideHeaders, &taskPrintFlags.json)
cmd.Flags().StringVarP(&taskFindFlags.id, "id", "i", "", "task ID")
cmd.Flags().StringVarP(&taskFindFlags.user, "user-id", "n", "", "task owner ID")
cmd.Flags().IntVarP(&taskFindFlags.limit, "limit", "", influxdb.TaskDefaultPageSize, "the number of tasks to find")
Expand Down Expand Up @@ -201,32 +194,14 @@ func taskFindF(cmd *cobra.Command, args []string) error {
}
}

w := internal.NewTabWriter(os.Stdout)
w.HideHeaders(!taskFindFlags.headers)
w.WriteHeaders(
"ID",
"Name",
"OrganizationID",
"Organization",
"AuthorizationID",
"Status",
"Every",
"Cron",
return printTasks(
cmd.OutOrStdout(),
taskPrintOpts{
hideHeaders: taskPrintFlags.hideHeaders,
json: taskPrintFlags.json,
tasks: tasks,
},
)
for _, t := range tasks {
w.Write(map[string]interface{}{
"ID": t.ID.String(),
"Name": t.Name,
"OrganizationID": t.OrganizationID.String(),
"Organization": t.Organization,
"Status": t.Status,
"Every": t.Every,
"Cron": t.Cron,
})
}
w.Flush()

return nil
}

var taskUpdateFlags struct {
Expand All @@ -238,6 +213,7 @@ func taskUpdateCmd(opt genericCLIOpts) *cobra.Command {
cmd := opt.newCmd("update", taskUpdateF, true)
cmd.Short = "Update task"

registerPrintOptions(cmd, &taskPrintFlags.hideHeaders, &taskPrintFlags.json)
cmd.Flags().StringVarP(&taskUpdateFlags.id, "id", "i", "", "task ID (required)")
cmd.Flags().StringVarP(&taskUpdateFlags.status, "status", "", "", "update task status")
cmd.MarkFlagRequired("id")
Expand All @@ -261,7 +237,7 @@ func taskUpdateF(cmd *cobra.Command, args []string) error {
return err
}

update := influxdb.TaskUpdate{}
var update influxdb.TaskUpdate
if taskUpdateFlags.status != "" {
update.Status = &taskUpdateFlags.status
}
Expand All @@ -279,29 +255,14 @@ func taskUpdateF(cmd *cobra.Command, args []string) error {
return err
}

w := internal.NewTabWriter(os.Stdout)
w.WriteHeaders(
"ID",
"Name",
"OrganizationID",
"Organization",
"AuthorizationID",
"Status",
"Every",
"Cron",
return printTasks(
cmd.OutOrStdout(),
taskPrintOpts{
hideHeaders: taskPrintFlags.hideHeaders,
json: taskPrintFlags.json,
task: t,
},
)
w.Write(map[string]interface{}{
"ID": t.ID.String(),
"Name": t.Name,
"OrganizationID": t.OrganizationID.String(),
"Organization": t.Organization,
"Status": t.Status,
"Every": t.Every,
"Cron": t.Cron,
})
w.Flush()

return nil
}

var taskDeleteFlags struct {
Expand All @@ -312,6 +273,7 @@ func taskDeleteCmd(opt genericCLIOpts) *cobra.Command {
cmd := opt.newCmd("delete", taskDeleteF, true)
cmd.Short = "Delete task"

registerPrintOptions(cmd, &taskPrintFlags.hideHeaders, &taskPrintFlags.json)
cmd.Flags().StringVarP(&taskDeleteFlags.id, "id", "i", "", "task id (required)")
cmd.MarkFlagRequired("id")

Expand Down Expand Up @@ -345,27 +307,62 @@ func taskDeleteF(cmd *cobra.Command, args []string) error {
return err
}

w := internal.NewTabWriter(os.Stdout)
w.WriteHeaders(
return printTasks(
cmd.OutOrStdout(),
taskPrintOpts{
hideHeaders: taskPrintFlags.hideHeaders,
json: taskPrintFlags.json,
task: t,
},
)
}

type taskPrintOpts struct {
hideHeaders bool
json bool
task *http.Task
tasks []http.Task
}

func printTasks(w io.Writer, opts taskPrintOpts) error {
if opts.json {
var v interface{} = opts.tasks
if opts.task != nil {
v = opts.task
}
return writeJSON(w, v)
}

tabW := internal.NewTabWriter(os.Stdout)
defer tabW.Flush()

tabW.HideHeaders(opts.hideHeaders)

tabW.WriteHeaders(
"ID",
"Name",
"OrganizationID",
"Organization ID",
"Organization",
"AuthorizationID",
"Status",
"Every",
"Cron",
)
w.Write(map[string]interface{}{
"ID": t.ID.String(),
"Name": t.Name,
"OrganizationID": t.OrganizationID.String(),
"Organization": t.Organization,
"Status": t.Status,
"Every": t.Every,
"Cron": t.Cron,
})
w.Flush()

if opts.task != nil {
opts.tasks = append(opts.tasks, *opts.task)
}

for _, t := range opts.tasks {
tabW.Write(map[string]interface{}{
"ID": t.ID.String(),
"Name": t.Name,
"Organization ID": t.OrganizationID.String(),
"Organization": t.Organization,
"Status": t.Status,
"Every": t.Every,
"Cron": t.Cron,
})
}

return nil
}
Expand All @@ -392,6 +389,7 @@ func taskLogFindCmd(opt genericCLIOpts) *cobra.Command {
cmd.Short = "List logs for task"
cmd.Aliases = []string{"find", "ls"}

registerPrintOptions(cmd, &taskPrintFlags.hideHeaders, &taskPrintFlags.json)
cmd.Flags().StringVarP(&taskLogFindFlags.taskID, "task-id", "", "", "task id (required)")
cmd.Flags().StringVarP(&taskLogFindFlags.runID, "run-id", "", "", "run id")
cmd.MarkFlagRequired("task-id")
Expand Down Expand Up @@ -431,20 +429,24 @@ func taskLogFindF(cmd *cobra.Command, args []string) error {
return err
}

w := internal.NewTabWriter(os.Stdout)
w.WriteHeaders(
"RunID",
"Time",
"Message",
)
w := cmd.OutOrStdout()
if taskPrintFlags.json {
return writeJSON(w, logs)
}

tabW := internal.NewTabWriter(w)
defer tabW.Flush()

tabW.HideHeaders(taskPrintFlags.hideHeaders)

tabW.WriteHeaders("RunID", "Time", "Message")
for _, log := range logs {
w.Write(map[string]interface{}{
tabW.Write(map[string]interface{}{
"RunID": log.RunID,
"Time": log.Time,
"Message": log.Message,
})
}
w.Flush()

return nil
}
Expand Down Expand Up @@ -525,8 +527,21 @@ func taskRunFindF(cmd *cobra.Command, args []string) error {
}
}

w := internal.NewTabWriter(os.Stdout)
w.WriteHeaders(
w := cmd.OutOrStdout()
if taskPrintFlags.json {
if runs == nil {
// guarantee we never return a null value from CLI
runs = make([]*influxdb.Run, 0)
}
return writeJSON(w, runs)
}

tabW := internal.NewTabWriter(w)
defer tabW.Flush()

tabW.HideHeaders(taskPrintFlags.hideHeaders)

tabW.WriteHeaders(
"ID",
"TaskID",
"Status",
Expand All @@ -542,7 +557,7 @@ func taskRunFindF(cmd *cobra.Command, args []string) error {
finishedAt := r.FinishedAt.Format(time.RFC3339Nano)
requestedAt := r.RequestedAt.Format(time.RFC3339Nano)

w.Write(map[string]interface{}{
tabW.Write(map[string]interface{}{
"ID": r.ID,
"TaskID": r.TaskID,
"Status": r.Status,
Expand All @@ -552,7 +567,6 @@ func taskRunFindF(cmd *cobra.Command, args []string) error {
"RequestedAt": requestedAt,
})
}
w.Flush()

return nil
}
Expand Down

0 comments on commit 410a389

Please sign in to comment.