Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(51)

Side by Side Diff: go/src/infra/tools/cr/main.go

Issue 1929153002: Add beginnings of new cr command (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Style and documentation Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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)
seanmccullough1 2016/04/29 00:54:37 nice, I hadn't run across tabriter before.
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.")
seanmccullough1 2016/04/29 00:54:37 fmt.Print(` // all this stuff `)
agable 2016/05/05 23:59:43 Done.
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
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
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]]
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
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698