Chromium Code Reviews| Index: go/src/infra/tools/cr/main.go |
| diff --git a/go/src/infra/tools/cr/main.go b/go/src/infra/tools/cr/main.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..30c1dc6ca8a4022b5f5a7c8dd5ae160c79aba726 |
| --- /dev/null |
| +++ b/go/src/infra/tools/cr/main.go |
| @@ -0,0 +1,94 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +/* |
| +Core of the Chrome Infrastructure CLI SDK. Provides autoupdate, module |
| +discovery, module installation, and help. |
| +*/ |
| + |
| +package main |
| + |
| +import ( |
| + "flag" |
| + "fmt" |
| + "os" |
| + "text/tabwriter" |
| + |
| + "infra/tools/cr/lib/subcommand" |
| + "infra/tools/cr/lib/terminal" |
| + |
| + "infra/tools/cr/cmd/firstrun" |
| +) |
| + |
| +var ( |
| + // The subcommand which will be executed. |
| + cmd *subcommand.Subcommand |
| + |
| + // Accessible variables for global flags. |
| + help bool |
| + verbose bool |
| +) |
| + |
| +var subcommands = map[string]*subcommand.Subcommand{ |
| + "firstrun": firstrun.FirstrunCmd, |
| +} |
| + |
| +func printSubcommands() { |
| + fmt.Println("Available subcommands are:") |
| + colwriter := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) |
|
seanmccullough1
2016/04/29 00:54:37
nice, I hadn't run across tabriter before.
|
| + for key, value := range subcommands { |
| + fmt.Fprintf(colwriter, "\t%v\t%v", key, value.ShortHelp) |
| + } |
| + colwriter.Flush() |
| + fmt.Println("") |
| +} |
| + |
| +func printCrHelp() { |
| + fmt.Println("The cr tool intelligently manages the Chrome Infra command-line SDK.") |
|
seanmccullough1
2016/04/29 00:54:37
fmt.Print(`
// all this stuff
`)
agable
2016/05/05 23:59:43
Done.
|
| + fmt.Println("") |
| + fmt.Println("You must provide a subcommand. Run 'cr help' for a list of available commands.") |
| + fmt.Println("") |
| + printSubcommands() |
| +} |
| + |
| +func main() { |
| + // This has to be done after init-time to prevent an initialization loop. |
| + subcommands["help"] = helpCmd |
|
seanmccullough1
2016/04/29 00:54:37
https://golang.org/ref/spec#Package_initialization
agable
2016/05/05 23:59:43
Oh good idea on the second suggestion, I like keep
|
| + |
| + if len(os.Args) < 2 { |
| + fmt.Println("No subcommand recognized.") |
| + printCrHelp() |
| + os.Exit(-1) |
| + } |
| + |
| + cmd = subcommands[os.Args[1]] |
|
seanmccullough1
2016/04/29 00:54:37
same comment as before with cmd, ok := ...
agable
2016/05/05 23:59:43
This puts the entire rest of main() inside the if-
seanmccullough1
2016/05/06 00:43:54
sgtm
|
| + if cmd == nil { |
| + fmt.Printf("Unrecognized subcommand '%v'.\n", os.Args[1]) |
| + printCrHelp() |
| + os.Exit(-1) |
| + } |
| + |
| + flags := flag.NewFlagSet("flags", flag.ExitOnError) |
| + flags.BoolVar(&help, "help", false, "print help for the given command") |
| + flags.BoolVar(&verbose, "verbose", false, "print more verbose output") |
| + cmd.InitFlags(flags) |
| + flags.Parse(os.Args[2:]) |
| + |
| + if verbose { |
| + terminal.SetDebug(verbose) |
| + } |
| + |
| + // There are two ways to get help: 'cr help foo' and 'cr foo --help'. The |
| + // former is handled by setSubcommand, the latter is special-cased here. |
| + if help { |
| + cmd.Help(flags) |
| + return |
| + } |
| + |
| + err := cmd.Run(flags) |
| + if err != nil { |
| + fmt.Print(err) |
| + cmd.Help(flags) |
| + } |
| +} |