talos/pkg/cmd/cmd.go
Andrew Rynhard d430a37e46 refactor: use go 1.13 error wrapping
This removes the github.com/pkg/errors package in favor of the official
error wrapping in go 1.13.

Signed-off-by: Andrew Rynhard <andrew@andrewrynhard.com>
2019-10-15 22:20:50 -07:00

47 lines
1020 B
Go

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package cmd
import (
"fmt"
"os/exec"
"github.com/armon/circbuf"
"github.com/talos-systems/talos/pkg/proc/reaper"
)
// MaxStderrLen is maximum length of stderr output captured for error message
const MaxStderrLen = 4096
// Run executes a command.
func Run(name string, args ...string) error {
cmd := exec.Command(name, args...)
stderr, err := circbuf.NewBuffer(MaxStderrLen)
if err != nil {
return err
}
cmd.Stderr = stderr
notifyCh := make(chan reaper.ProcessInfo, 8)
usingReaper := reaper.Notify(notifyCh)
if usingReaper {
defer reaper.Stop(notifyCh)
}
if err = cmd.Start(); err != nil {
return fmt.Errorf("%s: %s", err, stderr.String())
}
if err = reaper.WaitWrapper(usingReaper, notifyCh, cmd); err != nil {
return fmt.Errorf("%s: %s", err, stderr.String())
}
return nil
}