diff --git a/cmd/influx/cli/cli.go b/cmd/influx/cli/cli.go index b83505ceb4f..e84ebd95e40 100644 --- a/cmd/influx/cli/cli.go +++ b/cmd/influx/cli/cli.go @@ -322,8 +322,42 @@ 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 + response, err := c.Client.Query(client.Query{Command: "SHOW DATABASES"}) + 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 := 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) + } else { + fmt.Printf("ERR: Database %s doesn't exist. Run SHOW DATABASES for a list of existing databases.\n", d) + } } // SetPrecision sets client precision