Correctly read in input from a non-interactive stream for the CLI

If you pipe in a file to the `influx` CLI, it will not try to open the
interactive line reader, but instead just send the contents of the
entire file to the server.
pull/7440/head
Jonathan A. Sternberg 2016-10-10 10:57:07 -05:00
parent bf0ac921f7
commit 2f5f995782
3 changed files with 23 additions and 0 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)
}