From 3f4949019db43c6229a199be1d006f7a3f92a602 Mon Sep 17 00:00:00 2001 From: Paulo Pires Date: Mon, 21 Dec 2015 09:34:40 +0000 Subject: [PATCH 1/2] USE databasename will work only for existing databases. Fixes #5174 --- cmd/influx/cli/cli.go | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/cmd/influx/cli/cli.go b/cmd/influx/cli/cli.go index b83505ceb4f..b936f11459f 100644 --- a/cmd/influx/cli/cli.go +++ b/cmd/influx/cli/cli.go @@ -322,8 +322,41 @@ func (c *CommandLine) use(cmd string) { return } d := args[1] - c.Database = d - fmt.Printf("Using database %s\n", d) + + // validate if specified database exists + query := "SHOW DATABASES" + response, err := c.Client.Query(client.Query{Command: query}) + if err != nil { + fmt.Printf("ERR: %s\n", err) + return + } + + if err := response.Error(); err != nil { + fmt.Printf("ERR: %s\n", err) + return + } + + // verify the provided database exists + databaseExists := false + for _, result := range response.Results { + for _, row := range result.Series { + if row.Name == "databases" { + for _, values := range row.Values { + for _, database := range values { + if database == d { + databaseExists = true + } + } + } + } + } + } + if databaseExists { + c.Database = d + fmt.Printf("Using database %s\n", d) + } else { + fmt.Printf("ERR: Database %s doesn't exist. Run SHOW DATABASES for a list of existing databases.\n", d) + } } // SetPrecision sets client precision From 3165c707fc4b87ac3101da97604b5e8f691931b3 Mon Sep 17 00:00:00 2001 From: Paulo Pires Date: Tue, 22 Dec 2015 08:55:32 +0000 Subject: [PATCH 2/2] Small improvements to USE command as per PR comments. --- cmd/influx/cli/cli.go | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/cmd/influx/cli/cli.go b/cmd/influx/cli/cli.go index b936f11459f..e84ebd95e40 100644 --- a/cmd/influx/cli/cli.go +++ b/cmd/influx/cli/cli.go @@ -324,8 +324,7 @@ func (c *CommandLine) use(cmd string) { d := args[1] // validate if specified database exists - query := "SHOW DATABASES" - response, err := c.Client.Query(client.Query{Command: query}) + response, err := c.Client.Query(client.Query{Command: "SHOW DATABASES"}) if err != nil { fmt.Printf("ERR: %s\n", err) return @@ -337,20 +336,22 @@ func (c *CommandLine) use(cmd string) { } // verify the provided database exists - databaseExists := false - for _, result := range response.Results { - for _, row := range result.Series { - if row.Name == "databases" { - for _, values := range row.Values { - for _, database := range values { - if database == d { - databaseExists = true + databaseExists := func() bool { + for _, result := range response.Results { + for _, row := range result.Series { + if row.Name == "databases" { + for _, values := range row.Values { + for _, database := range values { + if database == d { + return true + } } } } } } - } + return false + }() if databaseExists { c.Database = d fmt.Printf("Using database %s\n", d)