Skip to content

Commit da8db0d

Browse files
committed
Add unit reload support
1 parent 038fbe1 commit da8db0d

5 files changed

Lines changed: 85 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ If your system isn't running (or targeting another system running) `systemctl`,
2222
- [x] `systemctl is-enabled`
2323
- [x] `systemctl is-failed`
2424
- [x] `systemctl mask`
25+
- [x] `systemctl reload`
2526
- [x] `systemctl restart`
2627
- [x] `systemctl show`
2728
- [x] `systemctl start`

reload_linux_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//go:build linux
2+
3+
package systemctl
4+
5+
import (
6+
"context"
7+
"os"
8+
"path/filepath"
9+
"reflect"
10+
"strings"
11+
"testing"
12+
"time"
13+
)
14+
15+
func TestReloadBuildsExpectedCommand(t *testing.T) {
16+
tests := []struct {
17+
name string
18+
opts Options
19+
want []string
20+
}{
21+
{
22+
name: "system mode",
23+
opts: Options{},
24+
want: []string{"reload", "--system", "nginx.service"},
25+
},
26+
{
27+
name: "user mode",
28+
opts: Options{UserMode: true},
29+
want: []string{"reload", "--user", "nginx.service"},
30+
},
31+
}
32+
33+
for _, tt := range tests {
34+
t.Run(tt.name, func(t *testing.T) {
35+
tempDir := t.TempDir()
36+
logFile := filepath.Join(tempDir, "args.log")
37+
fakeSystemctl := filepath.Join(tempDir, "systemctl")
38+
script := "#!/bin/sh\nprintf '%s\\n' \"$@\" > '" + logFile + "'\n"
39+
40+
if err := os.WriteFile(fakeSystemctl, []byte(script), 0o755); err != nil {
41+
t.Fatalf("write fake systemctl: %v", err)
42+
}
43+
44+
original := systemctl
45+
systemctl = fakeSystemctl
46+
t.Cleanup(func() {
47+
systemctl = original
48+
})
49+
50+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
51+
defer cancel()
52+
53+
if err := Reload(ctx, "nginx.service", tt.opts); err != nil {
54+
t.Fatalf("Reload returned error: %v", err)
55+
}
56+
57+
gotBytes, err := os.ReadFile(logFile)
58+
if err != nil {
59+
t.Fatalf("read captured args: %v", err)
60+
}
61+
got := strings.Fields(strings.TrimSpace(string(gotBytes)))
62+
if !reflect.DeepEqual(got, tt.want) {
63+
t.Fatalf("Reload args = %v, want %v", got, tt.want)
64+
}
65+
})
66+
}
67+
}

systemctl.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ func Restart(ctx context.Context, unit string, opts Options, args ...string) err
104104
return restart(ctx, unit, opts, args...)
105105
}
106106

107+
// Reload one or more units if they support reload.
108+
//
109+
// Any additional arguments are passed directly to the systemctl command.
110+
func Reload(ctx context.Context, unit string, opts Options, args ...string) error {
111+
return reload(ctx, unit, opts, args...)
112+
}
113+
107114
// Show a selected property of a unit. Accepted properties are predefined in the
108115
// properties subpackage to guarantee properties are valid and assist code-completion.
109116
//

systemctl_darwin.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ func restart(_ context.Context, _ string, _ Options, _ ...string) error {
4444
return nil
4545
}
4646

47+
func reload(_ context.Context, _ string, _ Options, _ ...string) error {
48+
return nil
49+
}
50+
4751
func show(_ context.Context, _ string, _ properties.Property, _ Options, _ ...string) (string, error) {
4852
return "", nil
4953
}

systemctl_linux.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ func restart(ctx context.Context, unit string, opts Options, args ...string) err
115115
return err
116116
}
117117

118+
func reload(ctx context.Context, unit string, opts Options, args ...string) error {
119+
a := prepareArgs("reload", opts, append([]string{unit}, args...)...)
120+
_, _, _, err := execute(ctx, a)
121+
return err
122+
}
123+
118124
func show(ctx context.Context, unit string, property properties.Property, opts Options, args ...string) (string, error) {
119125
extra := append([]string{unit, "--property", string(property)}, args...)
120126
a := prepareArgs("show", opts, extra...)

0 commit comments

Comments
 (0)