mirror of
https://github.com/taigrr/systemctl.git
synced 2025-10-06 00:37:03 +08:00
check stderr before reporting back error 1
This commit is contained in:
27
systemctl.go
27
systemctl.go
@@ -2,8 +2,7 @@ package systemctl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// TODO
|
||||
@@ -44,22 +43,36 @@ func Enable(ctx context.Context, unit string, usermode bool) error {
|
||||
args[1] = "--user"
|
||||
}
|
||||
_, stderr, code, err := execute(ctx, args)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
customErr := filterErr(stderr)
|
||||
if customErr != nil {
|
||||
return customErr
|
||||
}
|
||||
err = filterErr(stderr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if code != 0 {
|
||||
return errors.New("received error code " + strconv.Itoa(code))
|
||||
return fmt.Errorf("received error code %d for stderr `%s`: %w", code, stderr, ErrUnspecified)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO
|
||||
func Disable(ctx context.Context, unit string, usermode bool) error {
|
||||
var args = []string{"disable", "--system", unit}
|
||||
if usermode {
|
||||
args[1] = "--user"
|
||||
}
|
||||
_, stderr, code, err := execute(ctx, args)
|
||||
customErr := filterErr(stderr)
|
||||
if customErr != nil {
|
||||
return customErr
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if code != 0 {
|
||||
return fmt.Errorf("received error code %d for stderr `%s`: %w", code, stderr, ErrUnspecified)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user