talos/pkg/cmd/cmd.go
Andrey Smirnov c2cb0f9778 chore: enable 'wsl' linter and fix all the issues
I wish there were less of them :)

Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
2019-10-10 01:16:29 +03:00

47 lines
1.0 KiB
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 (
"os/exec"
"github.com/armon/circbuf"
"github.com/pkg/errors"
"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 errors.Errorf("%s: %s", err, stderr.String())
}
if err = reaper.WaitWrapper(usingReaper, notifyCh, cmd); err != nil {
return errors.Errorf("%s: %s", err, stderr.String())
}
return nil
}