moby: Add verbose and quiet flags to moby commandline

These set the log level to Debug and Error. The default
log level is set to Info.

Signed-off-by: Rolf Neugebauer <rolf.neugebauer@docker.com>
This commit is contained in:
Rolf Neugebauer 2017-03-28 21:40:17 +01:00
parent 463438d1f0
commit adb0f9db3d

View File

@ -4,18 +4,25 @@ import (
"flag" "flag"
"fmt" "fmt"
"os" "os"
log "github.com/Sirupsen/logrus"
) )
func main() { func main() {
flag.Usage = func() { flag.Usage = func() {
fmt.Printf("USAGE: %s COMMAND\n\n", os.Args[0]) fmt.Printf("USAGE: %s [options] COMMAND\n\n", os.Args[0])
fmt.Printf("Commands:\n") fmt.Printf("Commands:\n")
fmt.Printf(" build Build a Moby image from a YAML file\n") fmt.Printf(" build Build a Moby image from a YAML file\n")
fmt.Printf(" run Run a Moby image on a local hypervisor\n") fmt.Printf(" run Run a Moby image on a local hypervisor\n")
fmt.Printf(" help Print this message\n") fmt.Printf(" help Print this message\n")
fmt.Printf("\n") fmt.Printf("\n")
fmt.Printf("Run '%s COMMAND --help' for more information on the command\n", os.Args[0]) fmt.Printf("Run '%s COMMAND --help' for more information on the command\n", os.Args[0])
fmt.Printf("\n")
fmt.Printf("Options:\n")
flag.PrintDefaults()
} }
flagQuiet := flag.Bool("q", false, "Quiet execution")
flagVerbose := flag.Bool("v", false, "Verbose execution")
buildCmd := flag.NewFlagSet("build", flag.ExitOnError) buildCmd := flag.NewFlagSet("build", flag.ExitOnError)
buildCmd.Usage = func() { buildCmd.Usage = func() {
@ -44,23 +51,39 @@ func main() {
runDiskSz := runCmd.Int("disk-size", 0, "Size of Disk in MB") runDiskSz := runCmd.Int("disk-size", 0, "Size of Disk in MB")
runDisk := runCmd.String("disk", "", "Path to disk image to used") runDisk := runCmd.String("disk", "", "Path to disk image to used")
if len(os.Args) < 2 { // Set up logging
log.SetFormatter(&log.TextFormatter{})
log.SetLevel(log.InfoLevel)
flag.Parse()
if *flagQuiet && *flagVerbose {
fmt.Printf("Can't set quiet and verbose flag at the same time\n")
os.Exit(1)
}
if *flagQuiet {
log.SetLevel(log.ErrorLevel)
}
if *flagVerbose {
log.SetLevel(log.DebugLevel)
}
args := flag.Args()
if len(args) < 1 {
fmt.Printf("Please specify a command.\n\n") fmt.Printf("Please specify a command.\n\n")
flag.Usage() flag.Usage()
os.Exit(1) os.Exit(1)
} }
switch os.Args[1] { switch args[0] {
case "build": case "build":
buildCmd.Parse(os.Args[2:]) buildCmd.Parse(args[1:])
build(*buildName, *buildPull, buildCmd.Args()) build(*buildName, *buildPull, buildCmd.Args())
case "run": case "run":
runCmd.Parse(os.Args[2:]) runCmd.Parse(args[1:])
run(*runCPUs, *runMem, *runDiskSz, *runDisk, runCmd.Args()) run(*runCPUs, *runMem, *runDiskSz, *runDisk, runCmd.Args())
case "help": case "help":
flag.Usage() flag.Usage()
default: default:
fmt.Printf("%q is not valid command.\n\n", os.Args[1]) fmt.Printf("%q is not valid command.\n\n", args[0])
flag.Usage() flag.Usage()
os.Exit(1) os.Exit(1)
} }