diff --git a/README.md b/README.md index 948407f..aa14588 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ If your system isn't running (or targeting another system running) `systemctl`, - [x] `systemctl is-enabled` - [x] `systemctl is-failed` - [x] `systemctl mask` +- [x] `systemctl reload` - [x] `systemctl restart` - [x] `systemctl show` - [x] `systemctl start` diff --git a/reload_linux_test.go b/reload_linux_test.go new file mode 100644 index 0000000..9efcc2e --- /dev/null +++ b/reload_linux_test.go @@ -0,0 +1,67 @@ +//go:build linux + +package systemctl + +import ( + "context" + "os" + "path/filepath" + "reflect" + "strings" + "testing" + "time" +) + +func TestReloadBuildsExpectedCommand(t *testing.T) { + tests := []struct { + name string + opts Options + want []string + }{ + { + name: "system mode", + opts: Options{}, + want: []string{"reload", "--system", "nginx.service"}, + }, + { + name: "user mode", + opts: Options{UserMode: true}, + want: []string{"reload", "--user", "nginx.service"}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tempDir := t.TempDir() + logFile := filepath.Join(tempDir, "args.log") + fakeSystemctl := filepath.Join(tempDir, "systemctl") + script := "#!/bin/sh\nprintf '%s\\n' \"$@\" > '" + logFile + "'\n" + + if err := os.WriteFile(fakeSystemctl, []byte(script), 0o755); err != nil { + t.Fatalf("write fake systemctl: %v", err) + } + + original := systemctl + systemctl = fakeSystemctl + t.Cleanup(func() { + systemctl = original + }) + + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + + if err := Reload(ctx, "nginx.service", tt.opts); err != nil { + t.Fatalf("Reload returned error: %v", err) + } + + gotBytes, err := os.ReadFile(logFile) + if err != nil { + t.Fatalf("read captured args: %v", err) + } + got := strings.Fields(strings.TrimSpace(string(gotBytes))) + if !reflect.DeepEqual(got, tt.want) { + t.Fatalf("Reload args = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/systemctl.go b/systemctl.go index d53a9f1..fcea03a 100644 --- a/systemctl.go +++ b/systemctl.go @@ -104,6 +104,13 @@ func Restart(ctx context.Context, unit string, opts Options, args ...string) err return restart(ctx, unit, opts, args...) } +// Reload one or more units if they support reload. +// +// Any additional arguments are passed directly to the systemctl command. +func Reload(ctx context.Context, unit string, opts Options, args ...string) error { + return reload(ctx, unit, opts, args...) +} + // Show a selected property of a unit. Accepted properties are predefined in the // properties subpackage to guarantee properties are valid and assist code-completion. // diff --git a/systemctl_darwin.go b/systemctl_darwin.go index a224851..1d938da 100644 --- a/systemctl_darwin.go +++ b/systemctl_darwin.go @@ -44,6 +44,10 @@ func restart(_ context.Context, _ string, _ Options, _ ...string) error { return nil } +func reload(_ context.Context, _ string, _ Options, _ ...string) error { + return nil +} + func show(_ context.Context, _ string, _ properties.Property, _ Options, _ ...string) (string, error) { return "", nil } diff --git a/systemctl_linux.go b/systemctl_linux.go index d40dcc3..d3bce9b 100644 --- a/systemctl_linux.go +++ b/systemctl_linux.go @@ -115,6 +115,12 @@ func restart(ctx context.Context, unit string, opts Options, args ...string) err return err } +func reload(ctx context.Context, unit string, opts Options, args ...string) error { + a := prepareArgs("reload", opts, append([]string{unit}, args...)...) + _, _, _, err := execute(ctx, a) + return err +} + func show(ctx context.Context, unit string, property properties.Property, opts Options, args ...string) (string, error) { extra := append([]string{unit, "--property", string(property)}, args...) a := prepareArgs("show", opts, extra...)