talos/pkg/cmd/cmd.go
Andrey Smirnov d3d011c8d2 chore: replace /* */ comments with // comments in license header
This fixes issues with `// +build` directives not being recognized in
source files.

Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
2019-10-25 14:15:17 -07:00

47 lines
1017 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
}