diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2018-08-03 16:51:48 +0200 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2018-08-03 16:51:48 +0200 |
| commit | 534859fe5ab1f42a26c60ed2a58b34176f8920c8 (patch) | |
| tree | d21d85993d826b22428e3be0b62388ddba202105 /pkg | |
| parent | 0a2c27237bb9766589a502bd8c9cea3ccf009d4b (diff) | |
pkg/osutil: minor tweaks
1. Make it clear when a command times out.
2. Don't add trailing newline for VerboseError if output is empty.
3. Fix WriteExecFile for the case when the file already exists.
Diffstat (limited to 'pkg')
| -rw-r--r-- | pkg/osutil/osutil.go | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/pkg/osutil/osutil.go b/pkg/osutil/osutil.go index 7242c61b7..d9a543971 100644 --- a/pkg/osutil/osutil.go +++ b/pkg/osutil/osutil.go @@ -40,19 +40,27 @@ func Run(timeout time.Duration, cmd *exec.Cmd) ([]byte, error) { return nil, fmt.Errorf("failed to start %v %+v: %v", cmd.Path, cmd.Args, err) } done := make(chan bool) + timedout := make(chan bool, 1) timer := time.NewTimer(timeout) go func() { select { case <-timer.C: + timedout <- true cmd.Process.Kill() case <-done: + timedout <- false timer.Stop() } }() - defer close(done) - if err := cmd.Wait(); err != nil { + err := cmd.Wait() + close(done) + if err != nil { + text := fmt.Sprintf("failed to run %q: %v", cmd.Args, err) + if <-timedout { + text = fmt.Sprintf("timedout %q", cmd.Args) + } return nil, &VerboseError{ - Title: fmt.Sprintf("failed to run %v %+v: %v", cmd.Path, cmd.Args, err), + Title: text, Output: output.Bytes(), } } @@ -72,6 +80,9 @@ type VerboseError struct { } func (err *VerboseError) Error() string { + if len(err.Output) == 0 { + return err.Title + } return fmt.Sprintf("%v\n%s", err.Title, err.Output) } @@ -185,7 +196,10 @@ func WriteFile(filename string, data []byte) error { } func WriteExecFile(filename string, data []byte) error { - return ioutil.WriteFile(filename, data, DefaultExecPerm) + if err := ioutil.WriteFile(filename, data, DefaultExecPerm); err != nil { + return err + } + return os.Chmod(filename, DefaultExecPerm) } // TempFile creates a unique temp filename. |
