1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
16 "git.arvados.org/arvados.git/lib/cmd"
19 var Command = command{}
23 func (command) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
24 flags := flag.NewFlagSet(prog, flag.ExitOnError)
25 poll := flags.Duration("poll", 10*time.Second, "reporting interval")
26 debug := flags.Bool("debug", false, "show additional debug info")
27 dump := flags.String("dump", "", "save snapshot of OS files in given `directory` (for creating test cases)")
28 getVersion := flags.Bool("version", false, "print version information and exit")
30 if ok, code := cmd.ParseFlags(flags, prog, args, "program [args ...]", stderr); !ok {
32 } else if *getVersion {
33 fmt.Printf("%s %s\n", prog, cmd.Version.String())
35 } else if flags.NArg() == 0 {
36 fmt.Fprintf(stderr, "missing required argument: program (try -help)\n")
40 reporter := &Reporter{
41 Logger: log.New(stderr, prog+": ", 0),
45 reporter.Logger.Printf("%s %s", prog, cmd.Version.String())
46 reporter.Logger.Printf("running %v", flags.Args())
47 cmd := exec.Command(flags.Arg(0), flags.Args()[1:]...)
49 // Child process will use our stdin and stdout pipes (we close
53 // Child process stderr and our stats will both go to stderr
56 if err := cmd.Start(); err != nil {
57 reporter.Logger.Printf("error in cmd.Start: %v", err)
60 reporter.Pid = func() int {
61 return cmd.Process.Pid
65 if stdin, ok := stdin.(io.Closer); ok {
68 if stdout, ok := stdout.(io.Closer); ok {
74 err := reporter.dumpSourceFiles(*dump)
76 fmt.Fprintf(stderr, "error dumping source files: %s\n", err)
83 if err, ok := err.(*exec.ExitError); ok {
84 // The program has exited with an exit code != 0
86 // This works on both Unix and Windows. Although
87 // package syscall is generally platform dependent,
88 // WaitStatus is defined for both Unix and Windows and
89 // in both cases has an ExitStatus() method with the
91 if status, ok := err.Sys().(syscall.WaitStatus); ok {
92 return status.ExitStatus()
94 reporter.Logger.Printf("ExitError without WaitStatus: %v", err)
97 } else if err != nil {
98 reporter.Logger.Printf("error running command: %v", err)