| 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 |
| 17 "infra/tools/cr/lib/subcommand" |
| 18 "infra/tools/cr/lib/terminal" |
| 19 |
| 20 _ "infra/tools/cr/cmd/firstrun" |
| 21 ) |
| 22 |
| 23 var ( |
| 24 // The subcommand which will be executed. |
| 25 cmd *subcommand.Subcommand |
| 26 |
| 27 // Accessible variables for global flags. |
| 28 help bool |
| 29 verbose bool |
| 30 ) |
| 31 |
| 32 func printSubcommands() { |
| 33 fmt.Println("Available subcommands are:") |
| 34 subcommand.Tabulate() |
| 35 fmt.Println("") |
| 36 } |
| 37 |
| 38 func printCrHelp() { |
| 39 fmt.Print(`The cr tool intelligently manages the Chrome Infra command-li
ne SDK. |
| 40 |
| 41 You must provide a subcommand. Run 'cr help' for a list of available commands. |
| 42 `) |
| 43 printSubcommands() |
| 44 } |
| 45 |
| 46 func main() { |
| 47 if len(os.Args) < 2 { |
| 48 fmt.Println("No subcommand provided.") |
| 49 printCrHelp() |
| 50 os.Exit(-1) |
| 51 } |
| 52 |
| 53 cmd := subcommand.Get(os.Args[1]) |
| 54 if cmd == nil { |
| 55 fmt.Printf("Unrecognized subcommand '%v'.\n", os.Args[1]) |
| 56 printCrHelp() |
| 57 os.Exit(-1) |
| 58 } |
| 59 |
| 60 flags := flag.NewFlagSet("flags", flag.ExitOnError) |
| 61 flags.BoolVar(&help, "help", false, "print help for the given command") |
| 62 flags.BoolVar(&verbose, "verbose", false, "print more verbose output") |
| 63 cmd.InitFlags(flags) |
| 64 flags.Parse(os.Args[2:]) |
| 65 |
| 66 if verbose { |
| 67 terminal.ShowDebug = true |
| 68 } |
| 69 |
| 70 // There are two ways to get help: 'cr help foo' and 'cr foo --help'. Th
e |
| 71 // former is handled by setSubcommand, the latter is special-cased here. |
| 72 if help { |
| 73 cmd.Help(flags) |
| 74 return |
| 75 } |
| 76 |
| 77 err := cmd.Run(flags) |
| 78 if err != nil { |
| 79 fmt.Print(err) |
| 80 } |
| 81 } |
| OLD | NEW |