From 6814e21c0a4ffa032ac79d0a252464ea09a04cac Mon Sep 17 00:00:00 2001 From: Rolf Neugebauer Date: Tue, 28 Mar 2017 21:40:17 +0100 Subject: [PATCH] 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 --- src/cmd/moby/main.go | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/src/cmd/moby/main.go b/src/cmd/moby/main.go index a054edc2f..17ad9f10b 100644 --- a/src/cmd/moby/main.go +++ b/src/cmd/moby/main.go @@ -4,18 +4,25 @@ import ( "flag" "fmt" "os" + + log "github.com/Sirupsen/logrus" ) func main() { 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(" build Build a Moby image from a YAML file\n") fmt.Printf(" run Run a Moby image on a local hypervisor\n") fmt.Printf(" help Print this message\n") fmt.Printf("\n") 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.Usage = func() { @@ -44,23 +51,39 @@ func main() { runDiskSz := runCmd.Int("disk-size", 0, "Size of Disk in MB") 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") flag.Usage() os.Exit(1) } - switch os.Args[1] { + switch args[0] { case "build": - buildCmd.Parse(os.Args[2:]) + buildCmd.Parse(args[1:]) build(*buildName, *buildPull, buildCmd.Args()) case "run": - runCmd.Parse(os.Args[2:]) + runCmd.Parse(args[1:]) run(*runCPUs, *runMem, *runDiskSz, *runDisk, runCmd.Args()) case "help": flag.Usage() 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() os.Exit(1) }