Skip to content

Commit

Permalink
name the FlagSet for the shell and add a version flag
Browse files Browse the repository at this point in the history
  • Loading branch information
neonstalwart committed May 21, 2015
1 parent de77ee1 commit 7ee92cf
Showing 1 changed file with 34 additions and 22 deletions.
56 changes: 34 additions & 22 deletions cmd/influx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,25 @@ const (
)

type CommandLine struct {
Client *client.Client
Line *liner.State
Host string
Port int
Username string
Password string
Database string
Version string
Pretty bool // controls pretty print for json
Format string // controls the output format. Valid values are json, csv, or column
ShouldDump bool
Execute string
Client *client.Client
Line *liner.State
Host string
Port int
Username string
Password string
Database string
Version string
Pretty bool // controls pretty print for json
Format string // controls the output format. Valid values are json, csv, or column
ShouldDump bool
Execute string
ShowVersion bool
}

func main() {
c := CommandLine{}

fs := flag.NewFlagSet("default", flag.ExitOnError)
fs := flag.NewFlagSet("InfluxDB shell version "+version, flag.ExitOnError)
fs.StringVar(&c.Host, "host", default_host, "influxdb host to connect to")
fs.IntVar(&c.Port, "port", default_port, "influxdb port to connect to")
fs.StringVar(&c.Username, "username", c.Username, "username to connect to the server.")
Expand All @@ -58,18 +59,21 @@ func main() {
fs.BoolVar(&c.Pretty, "pretty", false, "turns on pretty print for the json format")
fs.BoolVar(&c.ShouldDump, "dump", false, "dump the contents of the given database to stdout")
fs.StringVar(&c.Execute, "execute", c.Execute, "Execute command and quit.")
fs.BoolVar(&c.ShowVersion, "version", false, "Displays the InfluxDB version.")

// Define our own custom usage to print
fs.Usage = func() {
fmt.Println(`Usage of influx:
-version
Display the version and exit.
-host 'host name'
Host to connect to.
-port 'port #'
Port to connect to.
-database 'database name'
Database to connect to the server.
-password 'password'
Password to connect to the server. Leaving blank will prompt for password (--password '')
Password to connect to the server. Leaving blank will prompt for password (--password '')
-username 'username'
Username to connect to the server.
-dump
Expand All @@ -83,19 +87,23 @@ func main() {
Examples:
# Use influx in a non-interactive mode to query the database "metrics" and pretty print json
$ influx -database 'metrics' -execute 'select * from cpu' -format 'json' -pretty
# Use influx in a non-interactive mode to query the database "metrics" and pretty print json
$ influx -database 'metrics' -execute 'select * from cpu' -format 'json' -pretty
# Dumping out your data
$ influx -database 'metrics' -dump
# Dumping out your data
$ influx -database 'metrics' -dump
# Connect to a specific database on startup and set database context
$ influx -database 'metrics' -host 'localhost' -port '8086'
# Connect to a specific database on startup and set database context
$ influx -database 'metrics' -host 'localhost' -port '8086'
`)

}
fs.Parse(os.Args[1:])

if c.ShowVersion {
showVersion()
os.Exit(0)
}

var promptForPassword bool
// determine if they set the password flag but provided no value
for _, v := range os.Args {
Expand Down Expand Up @@ -138,7 +146,7 @@ Examples:
}
}

fmt.Println("InfluxDB shell " + version)
showVersion()

var historyFile string
usr, err := user.Current()
Expand Down Expand Up @@ -172,6 +180,10 @@ Examples:
}
}

func showVersion() {
fmt.Println("InfluxDB shell " + version)
}

func (c *CommandLine) ParseCommand(cmd string) bool {
lcmd := strings.TrimSpace(strings.ToLower(cmd))
switch {
Expand Down

0 comments on commit 7ee92cf

Please sign in to comment.