diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a0d88c0fd..d0fb0dbd90 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ - [#5955](https://github.com/influxdata/influxdb/issues/5955): Make regex work on field and dimension keys in SELECT clause. - [#7470](https://github.com/influxdata/influxdb/pull/7470): Reduce map allocations when computing the TagSet of a measurement. - [#6894](https://github.com/influxdata/influxdb/issues/6894): Support `INFLUX_USERNAME` and `INFLUX_PASSWORD` for setting username/password in the CLI. +- [#6896](https://github.com/influxdata/influxdb/issues/6896): Correctly read in input from a non-interactive stream for the CLI. ### Bugfixes diff --git a/cmd/influx/cli/cli.go b/cmd/influx/cli/cli.go index 0d115e9572..68a8d85d35 100644 --- a/cmd/influx/cli/cli.go +++ b/cmd/influx/cli/cli.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "io" + "io/ioutil" "net" "net/url" "os" @@ -18,6 +19,8 @@ import ( "syscall" "text/tabwriter" + "golang.org/x/crypto/ssh/terminal" + "github.com/influxdata/influxdb/client" "github.com/influxdata/influxdb/importer/v8" "github.com/influxdata/influxdb/models" @@ -58,6 +61,7 @@ type CommandLine struct { Chunked bool Quit chan struct{} IgnoreSignals bool // Ignore signals normally caught by this process (used primarily for testing) + ForceTTY bool // Force the CLI to act as if it were connected to a TTY osSignals chan os.Signal historyFilePath string } @@ -73,6 +77,22 @@ func New(version string) *CommandLine { // Run executes the CLI func (c *CommandLine) Run() error { + // If we are not running in an interactive terminal, read stdin completely + // and execute a query. Do not allow meta commands. + if !c.ForceTTY && !terminal.IsTerminal(int(os.Stdin.Fd())) { + if err := c.Connect(""); err != nil { + return fmt.Errorf( + "Failed to connect to %s\nPlease check your connection settings and ensure 'influxd' is running.", + c.Client.Addr()) + } + + cmd, err := ioutil.ReadAll(os.Stdin) + if err != nil { + return err + } + return c.ExecuteQuery(string(cmd)) + } + if !c.IgnoreSignals { // register OS signals for graceful termination signal.Notify(c.osSignals, syscall.SIGINT, syscall.SIGTERM) diff --git a/cmd/influx/cli/cli_test.go b/cmd/influx/cli/cli_test.go index c2c7edae7d..2e1816acda 100644 --- a/cmd/influx/cli/cli_test.go +++ b/cmd/influx/cli/cli_test.go @@ -48,6 +48,7 @@ func TestRunCLI(t *testing.T) { c.Host = h c.Port, _ = strconv.Atoi(p) c.IgnoreSignals = true + c.ForceTTY = true go func() { close(c.Quit) }() @@ -69,6 +70,7 @@ func TestRunCLI_ExecuteInsert(t *testing.T) { c.Precision = "ms" c.Execute = "INSERT sensor,floor=1 value=2" c.IgnoreSignals = true + c.ForceTTY = true if err := c.Run(); err != nil { t.Fatalf("Run failed with error: %s", err) }