-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.go
76 lines (67 loc) · 2.09 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package systemctl
import (
"bytes"
"context"
"errors"
"fmt"
"os/exec"
"strings"
)
var systemctl string
const killed = 130
func init() {
path, _ := exec.LookPath("systemctl")
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
)
if systemctl == "" {
return "", "", 1, ErrNotInstalled
}
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()
customErr := filterErr(warnings)
if customErr != nil {
err = customErr
}
if code != 0 && err == nil {
err = fmt.Errorf("received error code %d for stderr `%s`: %w", code, warnings, ErrUnspecified)
}
return output, warnings, code, err
}
func filterErr(stderr string) error {
switch {
case strings.Contains(stderr, `does not exist`):
return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(stderr, `not found.`):
return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(stderr, `not loaded.`):
return errors.Join(ErrUnitNotLoaded, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(stderr, `No such file or directory`):
return errors.Join(ErrDoesNotExist, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(stderr, `Interactive authentication required`):
return errors.Join(ErrInsufficientPermissions, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(stderr, `Access denied`):
return errors.Join(ErrInsufficientPermissions, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(stderr, `DBUS_SESSION_BUS_ADDRESS`):
return errors.Join(ErrBusFailure, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(stderr, `is masked`):
return errors.Join(ErrMasked, fmt.Errorf("stderr: %s", stderr))
case strings.Contains(stderr, `Failed`):
return errors.Join(ErrUnspecified, fmt.Errorf("stderr: %s", stderr))
default:
return nil
}
}