10 Commits

11 changed files with 92 additions and 84 deletions

12
.github/FUNDING.yml vendored Normal file
View File

@@ -0,0 +1,12 @@
# These are supported funding model platforms
github: taigrr # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']

View File

@@ -18,6 +18,7 @@ In fact, if `systemctl` isn't found in the `PATH`, this library will panic.
- [x] `systemctl daemon-reload` - [x] `systemctl daemon-reload`
- [x] `systemctl disable` - [x] `systemctl disable`
- [x] `systemctl enable` - [x] `systemctl enable`
- [x] `systemctl reenable`
- [x] `systemctl is-active` - [x] `systemctl is-active`
- [x] `systemctl is-enabled` - [x] `systemctl is-enabled`
- [x] `systemctl is-failed` - [x] `systemctl is-failed`
@@ -65,10 +66,9 @@ func main() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() defer cancel()
// Equivalent to `systemctl enable nginx` with a 10 second timeout // Equivalent to `systemctl enable nginx` with a 10 second timeout
opts := Options{ opts := systemctl.Options{ UserMode: false }
usermode: false, unit := "nginx"
} err := systemctl.Enable(ctx, unit, opts)
err := Enable(ctx, unit, opts)
if err != nil { if err != nil {
log.Fatalf("unable to enable unit %s: %v", "nginx", err) log.Fatalf("unable to enable unit %s: %v", "nginx", err)
} }

View File

@@ -36,5 +36,5 @@ var (
// Something in the stderr output contains the word `Failed`, but it is not a known case // Something in the stderr output contains the word `Failed`, but it is not a known case
// This is a catch-all, and if it's ever seen in the wild, please submit a PR // This is a catch-all, and if it's ever seen in the wild, please submit a PR
ErrUnspecified = errors.New("Unknown error, please submit an issue at github.com/taigrr/systemctl") ErrUnspecified = errors.New("unknown error, please submit an issue at github.com/taigrr/systemctl")
) )

View File

@@ -53,7 +53,6 @@ func TestErrorFuncs(t *testing.T) {
fName := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name() fName := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
fName = strings.TrimPrefix(fName, "github.com/taigrr/") fName = strings.TrimPrefix(fName, "github.com/taigrr/")
t.Run(fmt.Sprintf("Errorcheck %s", fName), func(t *testing.T) { t.Run(fmt.Sprintf("Errorcheck %s", fName), func(t *testing.T) {
for _, tc := range errCases { for _, tc := range errCases {
t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) { t.Run(fmt.Sprintf("%s as %s", tc.unit, userString), func(t *testing.T) {
if (userString == "root" || userString == "system") && tc.runAsUser { if (userString == "root" || userString == "system") && tc.runAsUser {

4
go.mod
View File

@@ -1,5 +1,3 @@
module github.com/taigrr/systemctl module github.com/taigrr/systemctl
go 1.16 go 1.17
require golang.org/x/tools v0.1.1 // indirect

33
go.sum
View File

@@ -1,33 +0,0 @@
github.com/yuin/goldmark v1.3.5 h1:dPmz1Snjq0kmkz159iL7S6WzdahUTHnHB5M56WFVifs=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.1 h1:wGiQel/hW0NnEkJUk8lbzkX2gFJU6PFxf1v5OlCfuOs=
golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

View File

@@ -13,13 +13,12 @@ const dateFormat = "Mon 2006-01-02 15:04:05 MST"
// Get start time of a service (`systemctl show [unit] --property ExecMainStartTimestamp`) as a `Time` type // Get start time of a service (`systemctl show [unit] --property ExecMainStartTimestamp`) as a `Time` type
func GetStartTime(ctx context.Context, unit string, opts Options) (time.Time, error) { func GetStartTime(ctx context.Context, unit string, opts Options) (time.Time, error) {
value, err := Show(ctx, unit, properties.ExecMainStartTimestamp, opts) value, err := Show(ctx, unit, properties.ExecMainStartTimestamp, opts)
if err != nil { if err != nil {
return time.Unix(0, 0), err return time.Time{}, err
} }
// ExecMainStartTimestamp returns an empty string if the unit is not running // ExecMainStartTimestamp returns an empty string if the unit is not running
if value == "" { if value == "" {
return time.Unix(0, 0), ErrUnitNotActive return time.Time{}, ErrUnitNotActive
} }
return time.Parse(dateFormat, value) return time.Parse(dateFormat, value)
} }

View File

@@ -90,8 +90,8 @@ func TestGetStartTime(t *testing.T) {
t.Errorf("Expected start diff to be positive, but got: %d", int(diff)) t.Errorf("Expected start diff to be positive, but got: %d", int(diff))
} }
}) })
} }
func TestGetNumRestarts(t *testing.T) { func TestGetNumRestarts(t *testing.T) {
testCases := []struct { testCases := []struct {
unit string unit string
@@ -170,7 +170,6 @@ func TestGetNumRestarts(t *testing.T) {
t.Errorf("Expected restart count to differ by one, but difference was: %d", secondRestarts-restarts) t.Errorf("Expected restart count to differ by one, but difference was: %d", secondRestarts-restarts)
} }
}) })
} }
func TestGetMemoryUsage(t *testing.T) { func TestGetMemoryUsage(t *testing.T) {
@@ -230,8 +229,8 @@ func TestGetMemoryUsage(t *testing.T) {
t.Errorf("Expected memory usage between nginx and user.slice to differ, but both were: %d", bytes) t.Errorf("Expected memory usage between nginx and user.slice to differ, but both were: %d", bytes)
} }
}) })
} }
func TestGetPID(t *testing.T) { func TestGetPID(t *testing.T) {
testCases := []struct { testCases := []struct {
unit string unit string
@@ -296,7 +295,5 @@ func TestGetPID(t *testing.T) {
if pid == secondPid { if pid == secondPid {
t.Errorf("Expected pid != secondPid, but both were: %d", pid) t.Errorf("Expected pid != secondPid, but both were: %d", pid)
} }
}) })
} }

View File

@@ -15,7 +15,21 @@ import (
// reloaded, all sockets systemd listens on behalf of user configuration will // reloaded, all sockets systemd listens on behalf of user configuration will
// stay accessible. // stay accessible.
func DaemonReload(ctx context.Context, opts Options) error { func DaemonReload(ctx context.Context, opts Options) error {
var args = []string{"daemon-reload", "--system"} args := []string{"daemon-reload", "--system"}
if opts.UserMode {
args[1] = "--user"
}
_, _, _, err := execute(ctx, args)
return err
}
// Reenables one or more units.
//
// This removes all symlinks to the unit files backing the specified units from
// the unit configuration directory, then recreates the symlink to the unit again,
// atomically. Can be used to change the symlink target.
func Reenable(ctx context.Context, unit string, opts Options) error {
args := []string{"reenable", "--system", unit}
if opts.UserMode { if opts.UserMode {
args[1] = "--user" args[1] = "--user"
} }
@@ -29,7 +43,7 @@ func DaemonReload(ctx context.Context, opts Options) error {
// the unit configuration directory, and hence undoes any changes made by // the unit configuration directory, and hence undoes any changes made by
// enable or link. // enable or link.
func Disable(ctx context.Context, unit string, opts Options) error { func Disable(ctx context.Context, unit string, opts Options) error {
var args = []string{"disable", "--system", unit} args := []string{"disable", "--system", unit}
if opts.UserMode { if opts.UserMode {
args[1] = "--user" args[1] = "--user"
} }
@@ -44,7 +58,7 @@ func Disable(ctx context.Context, unit string, opts Options) error {
// manager configuration is reloaded (in a way equivalent to daemon-reload), // manager configuration is reloaded (in a way equivalent to daemon-reload),
// in order to ensure the changes are taken into account immediately. // in order to ensure the changes are taken into account immediately.
func Enable(ctx context.Context, unit string, opts Options) error { func Enable(ctx context.Context, unit string, opts Options) error {
var args = []string{"enable", "--system", unit} args := []string{"enable", "--system", unit}
if opts.UserMode { if opts.UserMode {
args[1] = "--user" args[1] = "--user"
} }
@@ -57,7 +71,7 @@ func Enable(ctx context.Context, unit string, opts Options) error {
// Returns true if the unit is active, false if inactive or failed. // Returns true if the unit is active, false if inactive or failed.
// Also returns false in an error case. // Also returns false in an error case.
func IsActive(ctx context.Context, unit string, opts Options) (bool, error) { func IsActive(ctx context.Context, unit string, opts Options) (bool, error) {
var args = []string{"is-active", "--system", unit} args := []string{"is-active", "--system", unit}
if opts.UserMode { if opts.UserMode {
args[1] = "--user" args[1] = "--user"
} }
@@ -87,7 +101,7 @@ func IsActive(ctx context.Context, unit string, opts Options) (bool, error) {
// See https://www.freedesktop.org/software/systemd/man/systemctl.html#is-enabled%20UNIT%E2%80%A6 // See https://www.freedesktop.org/software/systemd/man/systemctl.html#is-enabled%20UNIT%E2%80%A6
// for more information // for more information
func IsEnabled(ctx context.Context, unit string, opts Options) (bool, error) { func IsEnabled(ctx context.Context, unit string, opts Options) (bool, error) {
var args = []string{"is-enabled", "--system", unit} args := []string{"is-enabled", "--system", unit}
if opts.UserMode { if opts.UserMode {
args[1] = "--user" args[1] = "--user"
} }
@@ -127,7 +141,7 @@ func IsEnabled(ctx context.Context, unit string, opts Options) (bool, error) {
// Check whether any of the specified units are in a "failed" state. // Check whether any of the specified units are in a "failed" state.
func IsFailed(ctx context.Context, unit string, opts Options) (bool, error) { func IsFailed(ctx context.Context, unit string, opts Options) (bool, error) {
var args = []string{"is-failed", "--system", unit} args := []string{"is-failed", "--system", unit}
if opts.UserMode { if opts.UserMode {
args[1] = "--user" args[1] = "--user"
} }
@@ -149,7 +163,7 @@ func IsFailed(ctx context.Context, unit string, opts Options) (bool, error) {
// continue masking anyway. Calling Mask on a non-existing masked unit does not // continue masking anyway. Calling Mask on a non-existing masked unit does not
// return an error. Similarly, see Unmask. // return an error. Similarly, see Unmask.
func Mask(ctx context.Context, unit string, opts Options) error { func Mask(ctx context.Context, unit string, opts Options) error {
var args = []string{"mask", "--system", unit} args := []string{"mask", "--system", unit}
if opts.UserMode { if opts.UserMode {
args[1] = "--user" args[1] = "--user"
} }
@@ -160,7 +174,7 @@ func Mask(ctx context.Context, unit string, opts Options) error {
// Stop and then start one or more units specified on the command line. // Stop and then start one or more units specified on the command line.
// If the units are not running yet, they will be started. // If the units are not running yet, they will be started.
func Restart(ctx context.Context, unit string, opts Options) error { func Restart(ctx context.Context, unit string, opts Options) error {
var args = []string{"restart", "--system", unit} args := []string{"restart", "--system", unit}
if opts.UserMode { if opts.UserMode {
args[1] = "--user" args[1] = "--user"
} }
@@ -171,7 +185,7 @@ func Restart(ctx context.Context, unit string, opts Options) error {
// Show a selected property of a unit. Accepted properties are predefined in the // Show a selected property of a unit. Accepted properties are predefined in the
// properties subpackage to guarantee properties are valid and assist code-completion. // properties subpackage to guarantee properties are valid and assist code-completion.
func Show(ctx context.Context, unit string, property properties.Property, opts Options) (string, error) { func Show(ctx context.Context, unit string, property properties.Property, opts Options) (string, error) {
var args = []string{"show", "--system", unit, "--property", string(property)} args := []string{"show", "--system", unit, "--property", string(property)}
if opts.UserMode { if opts.UserMode {
args[1] = "--user" args[1] = "--user"
} }
@@ -183,7 +197,7 @@ func Show(ctx context.Context, unit string, property properties.Property, opts O
// Start (activate) a given unit // Start (activate) a given unit
func Start(ctx context.Context, unit string, opts Options) error { func Start(ctx context.Context, unit string, opts Options) error {
var args = []string{"start", "--system", unit} args := []string{"start", "--system", unit}
if opts.UserMode { if opts.UserMode {
args[1] = "--user" args[1] = "--user"
} }
@@ -197,7 +211,7 @@ func Start(ctx context.Context, unit string, opts Options) error {
// Generally, it makes more sense to programatically retrieve the properties // Generally, it makes more sense to programatically retrieve the properties
// using Show, but this command is provided for the sake of completeness // using Show, but this command is provided for the sake of completeness
func Status(ctx context.Context, unit string, opts Options) (string, error) { func Status(ctx context.Context, unit string, opts Options) (string, error) {
var args = []string{"status", "--system", unit} args := []string{"status", "--system", unit}
if opts.UserMode { if opts.UserMode {
args[1] = "--user" args[1] = "--user"
} }
@@ -207,7 +221,7 @@ func Status(ctx context.Context, unit string, opts Options) (string, error) {
// Stop (deactivate) a given unit // Stop (deactivate) a given unit
func Stop(ctx context.Context, unit string, opts Options) error { func Stop(ctx context.Context, unit string, opts Options) error {
var args = []string{"stop", "--system", unit} args := []string{"stop", "--system", unit}
if opts.UserMode { if opts.UserMode {
args[1] = "--user" args[1] = "--user"
} }
@@ -223,7 +237,7 @@ func Stop(ctx context.Context, unit string, opts Options) error {
// If the unit doesn't exist but it's masked anyway, no error will be // If the unit doesn't exist but it's masked anyway, no error will be
// returned. Gross, I know. Take it up with Poettering. // returned. Gross, I know. Take it up with Poettering.
func Unmask(ctx context.Context, unit string, opts Options) error { func Unmask(ctx context.Context, unit string, opts Options) error {
var args = []string{"unmask", "--system", unit} args := []string{"unmask", "--system", unit}
if opts.UserMode { if opts.UserMode {
args[1] = "--user" args[1] = "--user"
} }

View File

@@ -38,6 +38,7 @@ func TestMain(m *testing.M) {
} }
os.Exit(retCode) os.Exit(retCode)
} }
func TestDaemonReload(t *testing.T) { func TestDaemonReload(t *testing.T) {
testCases := []struct { testCases := []struct {
unit string unit string
@@ -78,6 +79,7 @@ func TestDaemonReload(t *testing.T) {
}) })
} }
} }
func TestDisable(t *testing.T) { func TestDisable(t *testing.T) {
t.Run(fmt.Sprintf(""), func(t *testing.T) { t.Run(fmt.Sprintf(""), func(t *testing.T) {
if userString != "root" && userString != "system" { if userString != "root" && userString != "system" {
@@ -101,10 +103,34 @@ func TestDisable(t *testing.T) {
t.Errorf("Unable to unmask %s", unit) t.Errorf("Unable to unmask %s", unit)
} }
}) })
} }
func TestEnable(t *testing.T) {
func TestReenable(t *testing.T) {
t.Run(fmt.Sprintf(""), func(t *testing.T) {
if userString != "root" && userString != "system" {
t.Skip("skipping superuser test while running as user")
}
unit := "nginx"
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err := Mask(ctx, unit, Options{UserMode: false})
if err != nil {
Unmask(ctx, unit, Options{UserMode: false})
t.Errorf("Unable to mask %s", unit)
}
err = Reenable(ctx, unit, Options{UserMode: false})
if err != ErrMasked {
Unmask(ctx, unit, Options{UserMode: false})
t.Errorf("error is %v, but should have been %v", err, ErrMasked)
}
err = Unmask(ctx, unit, Options{UserMode: false})
if err != nil {
t.Errorf("Unable to unmask %s", unit)
}
})
}
func TestEnable(t *testing.T) {
t.Run(fmt.Sprintf(""), func(t *testing.T) { t.Run(fmt.Sprintf(""), func(t *testing.T) {
if userString != "root" && userString != "system" { if userString != "root" && userString != "system" {
t.Skip("skipping superuser test while running as user") t.Skip("skipping superuser test while running as user")
@@ -127,8 +153,8 @@ func TestEnable(t *testing.T) {
t.Errorf("Unable to unmask %s", unit) t.Errorf("Unable to unmask %s", unit)
} }
}) })
} }
func ExampleEnable() { func ExampleEnable() {
unit := "syncthing" unit := "syncthing"
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
@@ -196,7 +222,6 @@ func TestIsActive(t *testing.T) {
t.Errorf("error is %v, but should have been %v", err, ErrDoesNotExist) t.Errorf("error is %v, but should have been %v", err, ErrDoesNotExist)
} }
}) })
} }
func TestIsEnabled(t *testing.T) { func TestIsEnabled(t *testing.T) {
@@ -251,7 +276,6 @@ func TestIsEnabled(t *testing.T) {
Unmask(ctx, unit, Options{UserMode: userMode}) Unmask(ctx, unit, Options{UserMode: userMode})
Enable(ctx, unit, Options{UserMode: userMode}) Enable(ctx, unit, Options{UserMode: userMode})
}) })
} }
func TestMask(t *testing.T) { func TestMask(t *testing.T) {
@@ -321,7 +345,6 @@ func TestMask(t *testing.T) {
t.Errorf("error on second masking is %v, but should have been %v", err, nil) t.Errorf("error on second masking is %v, but should have been %v", err, nil)
} }
Unmask(ctx, unit, opts) Unmask(ctx, unit, opts)
}) })
t.Run(fmt.Sprintf("test double masking nonexisting"), func(t *testing.T) { t.Run(fmt.Sprintf("test double masking nonexisting"), func(t *testing.T) {
unit := "nonexistant" unit := "nonexistant"
@@ -342,7 +365,6 @@ func TestMask(t *testing.T) {
} }
Unmask(ctx, unit, opts) Unmask(ctx, unit, opts)
}) })
} }
func TestRestart(t *testing.T) { func TestRestart(t *testing.T) {
@@ -381,7 +403,6 @@ func TestRestart(t *testing.T) {
if restarts+1 != secondRestarts { if restarts+1 != secondRestarts {
t.Errorf("Expected restart count to differ by one, but difference was: %d", secondRestarts-restarts) t.Errorf("Expected restart count to differ by one, but difference was: %d", secondRestarts-restarts)
} }
} }
// Runs through all defined Properties in parallel and checks for error cases // Runs through all defined Properties in parallel and checks for error cases
@@ -439,7 +460,6 @@ func TestStart(t *testing.T) {
break break
} }
} }
} }
func TestStatus(t *testing.T) { func TestStatus(t *testing.T) {
@@ -452,7 +472,6 @@ func TestStatus(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("error: %v", err) t.Errorf("error: %v", err)
} }
} }
func TestStop(t *testing.T) { func TestStop(t *testing.T) {
@@ -488,7 +507,6 @@ func TestStop(t *testing.T) {
break break
} }
} }
} }
func TestUnmask(t *testing.T) { func TestUnmask(t *testing.T) {
@@ -558,7 +576,6 @@ func TestUnmask(t *testing.T) {
t.Errorf("error on second unmasking is %v, but should have been %v", err, nil) t.Errorf("error on second unmasking is %v, but should have been %v", err, nil)
} }
Unmask(ctx, unit, opts) Unmask(ctx, unit, opts)
}) })
t.Run(fmt.Sprintf("test double unmasking nonexisting"), func(t *testing.T) { t.Run(fmt.Sprintf("test double unmasking nonexisting"), func(t *testing.T) {
unit := "nonexistant" unit := "nonexistant"
@@ -579,5 +596,4 @@ func TestUnmask(t *testing.T) {
t.Errorf("error on second unmasking is %v, but should have been %v", err, ErrDoesNotExist) t.Errorf("error on second unmasking is %v, but should have been %v", err, ErrDoesNotExist)
} }
}) })
} }

View File

@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"log"
"os/exec" "os/exec"
"regexp" "regexp"
) )
@@ -15,7 +16,9 @@ const killed = 130
func init() { func init() {
path, err := exec.LookPath("systemctl") path, err := exec.LookPath("systemctl")
if err != nil { if err != nil {
panic(ErrNotInstalled) log.Printf("%v", ErrNotInstalled)
systemctl = ""
return
} }
systemctl = path systemctl = path
} }
@@ -30,6 +33,9 @@ func execute(ctx context.Context, args []string) (string, string, int, error) {
warnings string warnings string
) )
if systemctl == "" {
panic(ErrNotInstalled)
}
cmd := exec.CommandContext(ctx, systemctl, args...) cmd := exec.CommandContext(ctx, systemctl, args...)
cmd.Stdout = &stdout cmd.Stdout = &stdout
cmd.Stderr = &stderr cmd.Stderr = &stderr