From 410a389c52eb700106c611765ac60dd02d391b0b Mon Sep 17 00:00:00 2001 From: Johnny Steenbergen Date: Fri, 27 Mar 2020 11:52:43 -0700 Subject: [PATCH] feat(influx): add jsonout and hide headers support to task cmd --- CHANGELOG.md | 1 + cmd/influx/task.go | 206 ++++++++++++++++++++++++--------------------- 2 files changed, 111 insertions(+), 96 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 486d9be48a7..46db8315e74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/cmd/influx/task.go b/cmd/influx/task.go index d3f39f54159..d492b4d6105 100644 --- a/cmd/influx/task.go +++ b/cmd/influx/task.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "io" "os" "time" @@ -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 } @@ -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 } @@ -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 { @@ -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") @@ -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 { @@ -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") @@ -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 } @@ -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 { @@ -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") @@ -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 } @@ -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") @@ -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 } @@ -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", @@ -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, @@ -552,7 +567,6 @@ func taskRunFindF(cmd *cobra.Command, args []string) error { "RequestedAt": requestedAt, }) } - w.Flush() return nil }