use CommandContext
This commit is contained in:
parent
7762c3af12
commit
8bb696fb51
@ -11,4 +11,7 @@ var (
|
|||||||
|
|
||||||
// ErrExecTimeout means that the provided context was done before the command finished execution.
|
// ErrExecTimeout means that the provided context was done before the command finished execution.
|
||||||
ErrExecTimeout = errors.New("command timed out")
|
ErrExecTimeout = errors.New("command timed out")
|
||||||
|
|
||||||
|
// ErrInsufficientPermissions means the calling executable was invoked without enough permissions to run the selected command
|
||||||
|
ErrInsufficientPermissions = errors.New("insufficient permissions for action")
|
||||||
)
|
)
|
||||||
|
@ -2,6 +2,7 @@ package systemctl
|
|||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
func IsFailed(unit string) (bool, error) {
|
func IsFailed(unit string) (bool, error) {
|
||||||
|
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
40
util.go
Normal file
40
util.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package systemctl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"os/exec"
|
||||||
|
)
|
||||||
|
|
||||||
|
var systemctl string
|
||||||
|
|
||||||
|
const killed = 130
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
path, err := exec.LookPath("systemctl")
|
||||||
|
if err != nil {
|
||||||
|
panic(ErrNotInstalled)
|
||||||
|
}
|
||||||
|
systemctl = path
|
||||||
|
}
|
||||||
|
|
||||||
|
func execute(ctx context.Context, args []string) (string, string, int, error) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
stderr bytes.Buffer
|
||||||
|
stdout bytes.Buffer
|
||||||
|
code int
|
||||||
|
output string
|
||||||
|
warnings string
|
||||||
|
)
|
||||||
|
|
||||||
|
cmd := exec.CommandContext(ctx, systemctl, args...)
|
||||||
|
cmd.Stdout = &stdout
|
||||||
|
cmd.Stderr = &stderr
|
||||||
|
err = cmd.Run()
|
||||||
|
output = stdout.String()
|
||||||
|
warnings = stderr.String()
|
||||||
|
code = cmd.ProcessState.ExitCode()
|
||||||
|
|
||||||
|
return output, warnings, code, err
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user