Skip to content

Commit

Permalink
Merge pull request #2240 from n1tr0g/exit_code
Browse files Browse the repository at this point in the history
improved exit code(s) for influx CLI
  • Loading branch information
otoolep committed Apr 13, 2015
2 parents 0a38117 + c232e21 commit 261ff11
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions cmd/influx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,19 @@ func main() {
c.connect("")

if c.ShouldDump {
c.dump()
return
if err := c.dump(); err != nil {
os.Exit(1)
} else {
os.Exit(0)
}
}

if c.Execute != "" {
c.executeQuery(c.Execute)
return
if err := c.executeQuery(c.Execute); err != nil {
os.Exit(1)
} else {
os.Exit(0)
}
}

fmt.Println("InfluxDB shell " + version)
Expand Down Expand Up @@ -264,33 +270,38 @@ func (c *CommandLine) SetFormat(cmd string) {
}
}

func (c *CommandLine) dump() {
func (c *CommandLine) dump() error {
response, err := c.Client.Dump(c.Database)
defer response.Close()
if err != nil {
fmt.Printf("Dump failed. %s\n", err)
return err
} else {
_, err := io.Copy(os.Stdout, response)
if err != nil {
fmt.Printf("Dump failed. %s\n", err)
return err
}
}
return nil
}

func (c *CommandLine) executeQuery(query string) {
func (c *CommandLine) executeQuery(query string) error {
response, err := c.Client.Query(client.Query{Command: query, Database: c.Database})
if err != nil {
fmt.Printf("ERR: %s\n", err)
return
return err
}
c.FormatResponse(response, os.Stdout)
if response.Error() != nil {
if err := response.Error(); err != nil {
fmt.Printf("ERR: %s\n", response.Error())
if c.Database == "" {
fmt.Println("Warning: It is possible this error is due to not setting a database.")
fmt.Println(`Please set a database with the command "use <database>".`)
}
return err
}
return nil
}

func (c *CommandLine) FormatResponse(response *client.Response, w io.Writer) {
Expand Down

0 comments on commit 261ff11

Please sign in to comment.