X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/2d3726dd11046146731f7f7391659a5eaf44702b..f41534fcdfa95c282a04d64158f8a7d850637768:/lib/cmd/cmd.go diff --git a/lib/cmd/cmd.go b/lib/cmd/cmd.go index 2cc71e68a8..63d7576b4c 100644 --- a/lib/cmd/cmd.go +++ b/lib/cmd/cmd.go @@ -2,7 +2,7 @@ // // SPDX-License-Identifier: Apache-2.0 -// package cmd helps define reusable functions that can be exposed as +// Package cmd helps define reusable functions that can be exposed as // [subcommands of] command line programs. package cmd @@ -11,8 +11,13 @@ import ( "fmt" "io" "io/ioutil" + "path/filepath" + "regexp" + "runtime" "sort" "strings" + + "github.com/sirupsen/logrus" ) type Handler interface { @@ -25,8 +30,28 @@ func (f HandlerFunc) RunCommand(prog string, args []string, stdin io.Reader, std return f(prog, args, stdin, stdout, stderr) } -// Multi is a Handler that looks up its first argument in a map, and -// invokes the resulting Handler with the remaining args. +// Version is a Handler that prints the package version (set at build +// time using -ldflags) and Go runtime version to stdout, and returns +// 0. +var Version versionCommand + +var version = "dev" + +type versionCommand struct{} + +func (versionCommand) String() string { + return fmt.Sprintf("%s (%s)", version, runtime.Version()) +} + +func (versionCommand) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int { + prog = regexp.MustCompile(` -*version$`).ReplaceAllLiteralString(prog, "") + fmt.Fprintf(stdout, "%s %s (%s)\n", prog, version, runtime.Version()) + return 0 +} + +// Multi is a Handler that looks up its first argument in a map (after +// stripping any "arvados-" or "crunch-" prefix), and invokes the +// resulting Handler with the remaining args. // // Example: // @@ -41,17 +66,33 @@ func (f HandlerFunc) RunCommand(prog string, args []string, stdin io.Reader, std type Multi map[string]Handler func (m Multi) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int { - if len(args) < 1 { + _, basename := filepath.Split(prog) + if i := strings.Index(basename, "~"); i >= 0 { + // drop "~anything" suffix (arvados-dispatch-cloud's + // DeployRunnerBinary feature relies on this) + basename = basename[:i] + } + cmd, ok := m[basename] + if !ok { + // "controller" command exists, and binary is named "arvados-controller" + cmd, ok = m[strings.TrimPrefix(basename, "arvados-")] + } + if !ok { + // "dispatch-slurm" command exists, and binary is named "crunch-dispatch-slurm" + cmd, ok = m[strings.TrimPrefix(basename, "crunch-")] + } + if ok { + return cmd.RunCommand(prog, args, stdin, stdout, stderr) + } else if len(args) < 1 { fmt.Fprintf(stderr, "usage: %s command [args]\n", prog) m.Usage(stderr) return 2 - } - if cmd, ok := m[args[0]]; !ok { - fmt.Fprintf(stderr, "unrecognized command %q\n", args[0]) + } else if cmd, ok = m[args[0]]; ok { + return cmd.RunCommand(prog+" "+args[0], args[1:], stdin, stdout, stderr) + } else { + fmt.Fprintf(stderr, "%s: unrecognized command %q\n", prog, args[0]) m.Usage(stderr) return 2 - } else { - return cmd.RunCommand(prog+" "+args[0], args[1:], stdin, stdout, stderr) } } @@ -114,3 +155,9 @@ func SubcommandToFront(args []string, flagset FlagSet) []string { copy(newargs[flagargs+1:], args[flagargs+1:]) return newargs } + +type NoPrefixFormatter struct{} + +func (NoPrefixFormatter) Format(entry *logrus.Entry) ([]byte, error) { + return []byte(entry.Message + "\n"), nil +}