| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 /* |
| 6 Core of the Chrome Infrastructure CLI SDK. Provides autoupdate, module |
| 7 discovery, module installation, and help. |
| 8 */ |
| 9 |
| 10 package main |
| 11 |
| 12 import ( |
| 13 "flag" |
| 14 "fmt" |
| 15 "os" |
| 16 "text/tabwriter" |
| 17 |
| 18 "infra/tools/cr/lib/subcommand" |
| 19 "infra/tools/cr/lib/terminal" |
| 20 |
| 21 "infra/tools/cr/cmd/firstrun" |
| 22 ) |
| 23 |
| 24 var ( |
| 25 // The subcommand which will be executed. |
| 26 cmd *subcommand.Subcommand |
| 27 |
| 28 // Accessible variables for global flags. |
| 29 help bool |
| 30 verbose bool |
| 31 ) |
| 32 |
| 33 var subcommands = map[string]*subcommand.Subcommand{ |
| 34 "firstrun": firstrun.FirstrunCmd, |
| 35 } |
| 36 |
| 37 func printSubcommands() { |
| 38 fmt.Println("Available subcommands are:") |
| 39 colwriter := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) |
| 40 for key, value := range subcommands { |
| 41 fmt.Fprintf(colwriter, "\t%v\t%v", key, value.ShortHelp) |
| 42 } |
| 43 colwriter.Flush() |
| 44 fmt.Println("") |
| 45 } |
| 46 |
| 47 func printCrHelp() { |
| 48 fmt.Println("The cr tool intelligently manages the Chrome Infra command-
line SDK.") |
| 49 fmt.Println("") |
| 50 fmt.Println("You must provide a subcommand. Run 'cr help' for a list of
available commands.") |
| 51 fmt.Println("") |
| 52 printSubcommands() |
| 53 } |
| 54 |
| 55 func main() { |
| 56 // This has to be done after init-time to prevent an initialization loop
. |
| 57 subcommands["help"] = helpCmd |
| 58 |
| 59 if len(os.Args) < 2 { |
| 60 fmt.Println("No subcommand recognized.") |
| 61 printCrHelp() |
| 62 os.Exit(-1) |
| 63 } |
| 64 |
| 65 cmd = subcommands[os.Args[1]] |
| 66 if cmd == nil { |
| 67 fmt.Printf("Unrecognized subcommand '%v'.\n", os.Args[1]) |
| 68 printCrHelp() |
| 69 os.Exit(-1) |
| 70 } |
| 71 |
| 72 flags := flag.NewFlagSet("flags", flag.ExitOnError) |
| 73 flags.BoolVar(&help, "help", false, "print help for the given command") |
| 74 flags.BoolVar(&verbose, "verbose", false, "print more verbose output") |
| 75 cmd.InitFlags(flags) |
| 76 flags.Parse(os.Args[2:]) |
| 77 |
| 78 if verbose { |
| 79 terminal.SetDebug(verbose) |
| 80 } |
| 81 |
| 82 // There are two ways to get help: 'cr help foo' and 'cr foo --help'. Th
e |
| 83 // former is handled by setSubcommand, the latter is special-cased here. |
| 84 if help { |
| 85 cmd.Help(flags) |
| 86 return |
| 87 } |
| 88 |
| 89 err := cmd.Run(flags) |
| 90 if err != nil { |
| 91 fmt.Print(err) |
| 92 cmd.Help(flags) |
| 93 } |
| 94 } |
| OLD | NEW |