mirror of
https://github.com/taigrr/systemctl.git
synced 2026-03-21 13:02:19 -07:00
Add unit reload support
This commit is contained in:
@@ -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`
|
||||
|
||||
67
reload_linux_test.go
Normal file
67
reload_linux_test.go
Normal file
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
//
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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...)
|
||||
|
||||
Reference in New Issue
Block a user