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

Unified 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: Created 4 years, 8 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 side-by-side diff with in-line comments
Download patch
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)
+ 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.")
+ 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
+
+ if len(os.Args) < 2 {
+ fmt.Println("No subcommand recognized.")
+ printCrHelp()
+ os.Exit(-1)
+ }
+
+ cmd = subcommands[os.Args[1]]
+ 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)
+ }
+}
« go/src/infra/tools/cr/cmd/help/help.go ('K') | « go/src/infra/tools/cr/lib/terminal/terminal.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698