X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/080c940d7a8134a6e277a53b7e45eb27e2b2c87f..f41534fcdfa95c282a04d64158f8a7d850637768:/lib/cmd/cmd.go diff --git a/lib/cmd/cmd.go b/lib/cmd/cmd.go index 8c65cf7acf..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 @@ -16,6 +16,8 @@ import ( "runtime" "sort" "strings" + + "github.com/sirupsen/logrus" ) type Handler interface { @@ -28,16 +30,28 @@ func (f HandlerFunc) RunCommand(prog string, args []string, stdin io.Reader, std return f(prog, args, stdin, stdout, stderr) } -type Version string +// 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 (v Version) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int { +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, v, runtime.Version()) + 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, and -// invokes the resulting Handler with the remaining args. +// 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: // @@ -53,9 +67,21 @@ type Multi map[string]Handler func (m Multi) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int { _, basename := filepath.Split(prog) - basename = strings.TrimPrefix(basename, "arvados-") - basename = strings.TrimPrefix(basename, "crunch-") - if cmd, ok := m[basename]; ok { + 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) @@ -129,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 +}