Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cmd/influx): add --file option to query and task commands #17595

Merged
merged 6 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

1. [17490](https://github.com/influxdata/influxdb/pull/17490): `influx config -`, to switch back to previous activated configuration
1. [17581](https://github.com/influxdata/influxdb/pull/17581): Introduce new navigation menu
1. [17595](https://github.com/influxdata/influxdb/pull/17595): Add -f (--file) option to `influx query` and `influx task` commands

### Bug Fixes

Expand Down
50 changes: 43 additions & 7 deletions cmd/influx/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,66 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/influxdata/flux"
"github.com/influxdata/flux/repl"
_ "github.com/influxdata/flux/stdlib"
_ "github.com/influxdata/influxdb/v2/query/stdlib"
"github.com/spf13/cobra"
)

var queryFlags struct {
org organization
org organization
file string
}

func cmdQuery(f *globalFlags, opts genericCLIOpts) *cobra.Command {
cmd := opts.newCmd("query [query literal or @/path/to/query.flux]", fluxQueryF, true)
cmd := opts.newCmd("query [query literal or -f /path/to/query.flux]", fluxQueryF, true)
cmd.Short = "Execute a Flux query"
cmd.Long = `Execute a literal Flux query provided as a string,
or execute a literal Flux query contained in a file by specifying the file prefixed with an @ sign.`
cmd.Args = cobra.ExactArgs(1)
cmd.Long = `Execute a Flux query provided via the first argument or a file or stdin`
cmd.Args = cobra.MaximumNArgs(1)

queryFlags.org.register(cmd, true)
cmd.Flags().StringVarP(&queryFlags.file, "file", "f", "", "Path to Flux query file")

return cmd
}

// readFluxQuery returns first argument, file contents or stdin
func readFluxQuery(args []string, file string) (string, error) {
// backward compatibility
if len(args) > 0 {
if strings.HasPrefix(args[0], "@") {
file = args[0][1:]
args = args[:0]
} else if args[0] == "-" {
file = ""
args = args[:0]
}
}

var query string
switch {
case len(args) > 0:
query = args[0]
case len(file) > 0:
content, err := ioutil.ReadFile(file)
if err != nil {
return "", err
}
query = string(content)
default:
content, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return "", err
}
query = string(content)
}
return query, nil
}

func fluxQueryF(cmd *cobra.Command, args []string) error {
if flags.local {
return fmt.Errorf("local flag not supported for query command")
Expand All @@ -35,7 +71,7 @@ func fluxQueryF(cmd *cobra.Command, args []string) error {
return err
}

q, err := repl.LoadQuery(args[0])
q, err := readFluxQuery(args, queryFlags.file)
if err != nil {
return fmt.Errorf("failed to load query: %v", err)
}
Expand Down
20 changes: 13 additions & 7 deletions cmd/influx/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"time"

"github.com/influxdata/flux/repl"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/cmd/influx/internal"
"github.com/influxdata/influxdb/v2/http"
Expand Down Expand Up @@ -45,14 +44,17 @@ var taskPrintFlags struct {
}

var taskCreateFlags struct {
org organization
org organization
file string
}

func taskCreateCmd(opt genericCLIOpts) *cobra.Command {
cmd := opt.newCmd("create [query literal or @/path/to/query.flux]", taskCreateF, true)
cmd.Args = cobra.ExactArgs(1)
cmd := opt.newCmd("create [script literal or -f /path/to/script.flux]", taskCreateF, true)
cmd.Args = cobra.MaximumNArgs(1)
cmd.Short = "Create task"
cmd.Long = `Create a task with a Flux script provided via the first argument or a file or stdin`

cmd.Flags().StringVarP(&taskCreateFlags.file, "file", "f", "", "Path to Flux script file")
taskCreateFlags.org.register(cmd, false)
registerPrintOptions(cmd, &taskPrintFlags.hideHeaders, &taskPrintFlags.json)

Expand All @@ -74,7 +76,7 @@ func taskCreateF(cmd *cobra.Command, args []string) error {
InsecureSkipVerify: flags.skipVerify,
}

flux, err := repl.LoadQuery(args[0])
flux, err := readFluxQuery(args, taskCreateFlags.file)
if err != nil {
return fmt.Errorf("error parsing flux script: %s", err)
}
Expand Down Expand Up @@ -207,15 +209,18 @@ func taskFindF(cmd *cobra.Command, args []string) error {
var taskUpdateFlags struct {
id string
status string
file string
}

func taskUpdateCmd(opt genericCLIOpts) *cobra.Command {
cmd := opt.newCmd("update", taskUpdateF, true)
cmd.Short = "Update task"
cmd.Long = `Update task status or script. Provide a Flux script via the first argument or a file. Use '-' argument to read from stdin.`

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.Flags().StringVarP(&taskUpdateFlags.file, "file", "f", "", "Path to Flux script file")
cmd.MarkFlagRequired("id")

return cmd
Expand All @@ -242,8 +247,9 @@ func taskUpdateF(cmd *cobra.Command, args []string) error {
update.Status = &taskUpdateFlags.status
}

if len(args) > 0 {
flux, err := repl.LoadQuery(args[0])
// update flux script only if first arg or file is supplied
if (len(args) > 0 && len(args[0]) > 0) || len(taskUpdateFlags.file) > 0 {
flux, err := readFluxQuery(args, taskUpdateFlags.file)
if err != nil {
return fmt.Errorf("error parsing flux script: %s", err)
}
Expand Down