From 1dfdc303771273bbc9ba39f325e1f79cce296aa0 Mon Sep 17 00:00:00 2001 From: liketic Date: Fri, 8 Sep 2017 23:20:54 +0800 Subject: [PATCH] Influx CLI: Make cli case insensitive --- cmd/influx/cli/cli.go | 11 +++++++---- cmd/influx/cli/cli_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/cmd/influx/cli/cli.go b/cmd/influx/cli/cli.go index 0307227870..5477f81131 100644 --- a/cmd/influx/cli/cli.go +++ b/cmd/influx/cli/cli.go @@ -294,6 +294,9 @@ func (c *CommandLine) ParseCommand(cmd string) error { // Connect connects to a server. func (c *CommandLine) Connect(cmd string) error { + // normalize cmd + cmd = strings.ToLower(cmd) + // Remove the "connect" keyword if it exists addr := strings.TrimSpace(strings.Replace(cmd, "connect", "", -1)) if addr == "" { @@ -551,10 +554,10 @@ func (c *CommandLine) SetPrecision(cmd string) { // SetFormat sets output format. func (c *CommandLine) SetFormat(cmd string) { - // Remove the "format" keyword if it exists - cmd = strings.TrimSpace(strings.Replace(cmd, "format", "", -1)) // normalize cmd cmd = strings.ToLower(cmd) + // Remove the "format" keyword if it exists + cmd = strings.TrimSpace(strings.Replace(cmd, "format", "", -1)) switch cmd { case "json", "csv", "column": @@ -566,10 +569,10 @@ func (c *CommandLine) SetFormat(cmd string) { // SetWriteConsistency sets write consistency level. func (c *CommandLine) SetWriteConsistency(cmd string) { - // Remove the "consistency" keyword if it exists - cmd = strings.TrimSpace(strings.Replace(cmd, "consistency", "", -1)) // normalize cmd cmd = strings.ToLower(cmd) + // Remove the "consistency" keyword if it exists + cmd = strings.TrimSpace(strings.Replace(cmd, "consistency", "", -1)) _, err := models.ParseConsistencyLevel(cmd) if err != nil { diff --git a/cmd/influx/cli/cli_test.go b/cmd/influx/cli/cli_test.go index 41f7b2437f..738e5f8ca9 100644 --- a/cmd/influx/cli/cli_test.go +++ b/cmd/influx/cli/cli_test.go @@ -147,6 +147,16 @@ func TestSetPrecision(t *testing.T) { if c.ClientConfig.Precision != p { t.Fatalf("Precision is %s but should be %s", c.ClientConfig.Precision, p) } + up := "NS" + c.SetPrecision("PRECISION " + up) + if c.ClientConfig.Precision != p { + t.Fatalf("Precision is %s but should be %s", c.ClientConfig.Precision, p) + } + mixed := "ns" + c.SetPrecision("PRECISION " + mixed) + if c.ClientConfig.Precision != p { + t.Fatalf("Precision is %s but should be %s", c.ClientConfig.Precision, p) + } // validate set default precision which equals empty string p = "rfc3339" @@ -154,6 +164,11 @@ func TestSetPrecision(t *testing.T) { if c.ClientConfig.Precision != "" { t.Fatalf("Precision is %s but should be empty", c.ClientConfig.Precision) } + p = "RFC3339" + c.SetPrecision("precision " + p) + if c.ClientConfig.Precision != "" { + t.Fatalf("Precision is %s but should be empty", c.ClientConfig.Precision) + } } func TestSetFormat(t *testing.T) { @@ -169,6 +184,17 @@ func TestSetFormat(t *testing.T) { if c.Format != f { t.Fatalf("Format is %s but should be %s", c.Format, f) } + + uf := "JSON" + c.SetFormat("format " + uf) + if c.Format != f { + t.Fatalf("Format is %s but should be %s", c.Format, f) + } + mixed := "json" + c.SetFormat("FORMAT " + mixed) + if c.Format != f { + t.Fatalf("Format is %s but should be %s", c.Format, f) + } } func Test_SetChunked(t *testing.T) { @@ -263,6 +289,18 @@ func TestSetWriteConsistency(t *testing.T) { t.Fatalf("WriteConsistency is %s but should be %s", c.ClientConfig.WriteConsistency, consistency) } + consistency = "QUORUM" + c.SetWriteConsistency("consistency " + consistency) + if c.ClientConfig.WriteConsistency != "quorum" { + t.Fatalf("WriteConsistency is %s but should be %s", c.ClientConfig.WriteConsistency, "quorum") + } + + consistency = "quorum" + c.SetWriteConsistency("CONSISTENCY " + consistency) + if c.ClientConfig.WriteConsistency != consistency { + t.Fatalf("WriteConsistency is %s but should be %s", c.ClientConfig.WriteConsistency, consistency) + } + // set invalid write consistency and verify there was no change invalidConsistency := "invalid_consistency" c.SetWriteConsistency("consistency " + invalidConsistency)