Skip to content

Commit 33828cf

Browse files
committed
gofumpt against systemctl + add reenable
1 parent e44344e commit 33828cf

File tree

3 files changed

+58
-27
lines changed

3 files changed

+58
-27
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ In fact, if `systemctl` isn't found in the `PATH`, this library will panic.
1818
- [x] `systemctl daemon-reload`
1919
- [x] `systemctl disable`
2020
- [x] `systemctl enable`
21+
- [x] `systemctl reenable`
2122
- [x] `systemctl is-active`
2223
- [x] `systemctl is-enabled`
2324
- [x] `systemctl is-failed`

systemctl.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,21 @@ import (
1515
// reloaded, all sockets systemd listens on behalf of user configuration will
1616
// stay accessible.
1717
func DaemonReload(ctx context.Context, opts Options) error {
18-
var args = []string{"daemon-reload", "--system"}
18+
args := []string{"daemon-reload", "--system"}
19+
if opts.UserMode {
20+
args[1] = "--user"
21+
}
22+
_, _, _, err := execute(ctx, args)
23+
return err
24+
}
25+
26+
// Reenables one or more units.
27+
//
28+
// This removes all symlinks to the unit files backing the specified units from
29+
// the unit configuration directory, then recreates the symlink to the unit again,
30+
// atomically. Can be used to change the symlink target.
31+
func Reenable(ctx context.Context, unit string, opts Options) error {
32+
args := []string{"reenable", "--system", unit}
1933
if opts.UserMode {
2034
args[1] = "--user"
2135
}
@@ -29,7 +43,7 @@ func DaemonReload(ctx context.Context, opts Options) error {
2943
// the unit configuration directory, and hence undoes any changes made by
3044
// enable or link.
3145
func Disable(ctx context.Context, unit string, opts Options) error {
32-
var args = []string{"disable", "--system", unit}
46+
args := []string{"disable", "--system", unit}
3347
if opts.UserMode {
3448
args[1] = "--user"
3549
}
@@ -44,7 +58,7 @@ func Disable(ctx context.Context, unit string, opts Options) error {
4458
// manager configuration is reloaded (in a way equivalent to daemon-reload),
4559
// in order to ensure the changes are taken into account immediately.
4660
func Enable(ctx context.Context, unit string, opts Options) error {
47-
var args = []string{"enable", "--system", unit}
61+
args := []string{"enable", "--system", unit}
4862
if opts.UserMode {
4963
args[1] = "--user"
5064
}
@@ -57,7 +71,7 @@ func Enable(ctx context.Context, unit string, opts Options) error {
5771
// Returns true if the unit is active, false if inactive or failed.
5872
// Also returns false in an error case.
5973
func IsActive(ctx context.Context, unit string, opts Options) (bool, error) {
60-
var args = []string{"is-active", "--system", unit}
74+
args := []string{"is-active", "--system", unit}
6175
if opts.UserMode {
6276
args[1] = "--user"
6377
}
@@ -87,7 +101,7 @@ func IsActive(ctx context.Context, unit string, opts Options) (bool, error) {
87101
// See https://www.freedesktop.org/software/systemd/man/systemctl.html#is-enabled%20UNIT%E2%80%A6
88102
// for more information
89103
func IsEnabled(ctx context.Context, unit string, opts Options) (bool, error) {
90-
var args = []string{"is-enabled", "--system", unit}
104+
args := []string{"is-enabled", "--system", unit}
91105
if opts.UserMode {
92106
args[1] = "--user"
93107
}
@@ -127,7 +141,7 @@ func IsEnabled(ctx context.Context, unit string, opts Options) (bool, error) {
127141

128142
// Check whether any of the specified units are in a "failed" state.
129143
func IsFailed(ctx context.Context, unit string, opts Options) (bool, error) {
130-
var args = []string{"is-failed", "--system", unit}
144+
args := []string{"is-failed", "--system", unit}
131145
if opts.UserMode {
132146
args[1] = "--user"
133147
}
@@ -149,7 +163,7 @@ func IsFailed(ctx context.Context, unit string, opts Options) (bool, error) {
149163
// continue masking anyway. Calling Mask on a non-existing masked unit does not
150164
// return an error. Similarly, see Unmask.
151165
func Mask(ctx context.Context, unit string, opts Options) error {
152-
var args = []string{"mask", "--system", unit}
166+
args := []string{"mask", "--system", unit}
153167
if opts.UserMode {
154168
args[1] = "--user"
155169
}
@@ -160,7 +174,7 @@ func Mask(ctx context.Context, unit string, opts Options) error {
160174
// Stop and then start one or more units specified on the command line.
161175
// If the units are not running yet, they will be started.
162176
func Restart(ctx context.Context, unit string, opts Options) error {
163-
var args = []string{"restart", "--system", unit}
177+
args := []string{"restart", "--system", unit}
164178
if opts.UserMode {
165179
args[1] = "--user"
166180
}
@@ -171,7 +185,7 @@ func Restart(ctx context.Context, unit string, opts Options) error {
171185
// Show a selected property of a unit. Accepted properties are predefined in the
172186
// properties subpackage to guarantee properties are valid and assist code-completion.
173187
func Show(ctx context.Context, unit string, property properties.Property, opts Options) (string, error) {
174-
var args = []string{"show", "--system", unit, "--property", string(property)}
188+
args := []string{"show", "--system", unit, "--property", string(property)}
175189
if opts.UserMode {
176190
args[1] = "--user"
177191
}
@@ -183,7 +197,7 @@ func Show(ctx context.Context, unit string, property properties.Property, opts O
183197

184198
// Start (activate) a given unit
185199
func Start(ctx context.Context, unit string, opts Options) error {
186-
var args = []string{"start", "--system", unit}
200+
args := []string{"start", "--system", unit}
187201
if opts.UserMode {
188202
args[1] = "--user"
189203
}
@@ -197,7 +211,7 @@ func Start(ctx context.Context, unit string, opts Options) error {
197211
// Generally, it makes more sense to programatically retrieve the properties
198212
// using Show, but this command is provided for the sake of completeness
199213
func Status(ctx context.Context, unit string, opts Options) (string, error) {
200-
var args = []string{"status", "--system", unit}
214+
args := []string{"status", "--system", unit}
201215
if opts.UserMode {
202216
args[1] = "--user"
203217
}
@@ -207,7 +221,7 @@ func Status(ctx context.Context, unit string, opts Options) (string, error) {
207221

208222
// Stop (deactivate) a given unit
209223
func Stop(ctx context.Context, unit string, opts Options) error {
210-
var args = []string{"stop", "--system", unit}
224+
args := []string{"stop", "--system", unit}
211225
if opts.UserMode {
212226
args[1] = "--user"
213227
}
@@ -223,7 +237,7 @@ func Stop(ctx context.Context, unit string, opts Options) error {
223237
// If the unit doesn't exist but it's masked anyway, no error will be
224238
// returned. Gross, I know. Take it up with Poettering.
225239
func Unmask(ctx context.Context, unit string, opts Options) error {
226-
var args = []string{"unmask", "--system", unit}
240+
args := []string{"unmask", "--system", unit}
227241
if opts.UserMode {
228242
args[1] = "--user"
229243
}

systemctl_test.go

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func TestMain(m *testing.M) {
3838
}
3939
os.Exit(retCode)
4040
}
41+
4142
func TestDaemonReload(t *testing.T) {
4243
testCases := []struct {
4344
unit string
@@ -78,6 +79,7 @@ func TestDaemonReload(t *testing.T) {
7879
})
7980
}
8081
}
82+
8183
func TestDisable(t *testing.T) {
8284
t.Run(fmt.Sprintf(""), func(t *testing.T) {
8385
if userString != "root" && userString != "system" {
@@ -101,10 +103,34 @@ func TestDisable(t *testing.T) {
101103
t.Errorf("Unable to unmask %s", unit)
102104
}
103105
})
106+
}
104107

108+
func TestReenable(t *testing.T) {
109+
t.Run(fmt.Sprintf(""), func(t *testing.T) {
110+
if userString != "root" && userString != "system" {
111+
t.Skip("skipping superuser test while running as user")
112+
}
113+
unit := "nginx"
114+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
115+
defer cancel()
116+
err := Mask(ctx, unit, Options{UserMode: false})
117+
if err != nil {
118+
Unmask(ctx, unit, Options{UserMode: false})
119+
t.Errorf("Unable to mask %s", unit)
120+
}
121+
err = Reenable(ctx, unit, Options{UserMode: false})
122+
if err != ErrMasked {
123+
Unmask(ctx, unit, Options{UserMode: false})
124+
t.Errorf("error is %v, but should have been %v", err, ErrMasked)
125+
}
126+
err = Unmask(ctx, unit, Options{UserMode: false})
127+
if err != nil {
128+
t.Errorf("Unable to unmask %s", unit)
129+
}
130+
})
105131
}
106-
func TestEnable(t *testing.T) {
107132

133+
func TestEnable(t *testing.T) {
108134
t.Run(fmt.Sprintf(""), func(t *testing.T) {
109135
if userString != "root" && userString != "system" {
110136
t.Skip("skipping superuser test while running as user")
@@ -127,8 +153,8 @@ func TestEnable(t *testing.T) {
127153
t.Errorf("Unable to unmask %s", unit)
128154
}
129155
})
130-
131156
}
157+
132158
func ExampleEnable() {
133159
unit := "syncthing"
134160
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
@@ -196,7 +222,6 @@ func TestIsActive(t *testing.T) {
196222
t.Errorf("error is %v, but should have been %v", err, ErrDoesNotExist)
197223
}
198224
})
199-
200225
}
201226

202227
func TestIsEnabled(t *testing.T) {
@@ -251,7 +276,6 @@ func TestIsEnabled(t *testing.T) {
251276
Unmask(ctx, unit, Options{UserMode: userMode})
252277
Enable(ctx, unit, Options{UserMode: userMode})
253278
})
254-
255279
}
256280

257281
func TestMask(t *testing.T) {
@@ -263,7 +287,7 @@ func TestMask(t *testing.T) {
263287
}{
264288
/* Run these tests only as an unpriviledged user */
265289

266-
//try nonexistant unit in user mode as user
290+
// try nonexistant unit in user mode as user
267291
{"nonexistant", ErrDoesNotExist, Options{UserMode: true}, true},
268292
// try existing unit in user mode as user
269293
{"syncthing", nil, Options{UserMode: true}, true},
@@ -321,7 +345,6 @@ func TestMask(t *testing.T) {
321345
t.Errorf("error on second masking is %v, but should have been %v", err, nil)
322346
}
323347
Unmask(ctx, unit, opts)
324-
325348
})
326349
t.Run(fmt.Sprintf("test double masking nonexisting"), func(t *testing.T) {
327350
unit := "nonexistant"
@@ -342,7 +365,6 @@ func TestMask(t *testing.T) {
342365
}
343366
Unmask(ctx, unit, opts)
344367
})
345-
346368
}
347369

348370
func TestRestart(t *testing.T) {
@@ -381,7 +403,6 @@ func TestRestart(t *testing.T) {
381403
if restarts+1 != secondRestarts {
382404
t.Errorf("Expected restart count to differ by one, but difference was: %d", secondRestarts-restarts)
383405
}
384-
385406
}
386407

387408
// Runs through all defined Properties in parallel and checks for error cases
@@ -439,7 +460,6 @@ func TestStart(t *testing.T) {
439460
break
440461
}
441462
}
442-
443463
}
444464

445465
func TestStatus(t *testing.T) {
@@ -452,7 +472,6 @@ func TestStatus(t *testing.T) {
452472
if err != nil {
453473
t.Errorf("error: %v", err)
454474
}
455-
456475
}
457476

458477
func TestStop(t *testing.T) {
@@ -488,7 +507,6 @@ func TestStop(t *testing.T) {
488507
break
489508
}
490509
}
491-
492510
}
493511

494512
func TestUnmask(t *testing.T) {
@@ -500,7 +518,7 @@ func TestUnmask(t *testing.T) {
500518
}{
501519
/* Run these tests only as an unpriviledged user */
502520

503-
//try nonexistant unit in user mode as user
521+
// try nonexistant unit in user mode as user
504522
{"nonexistant", ErrDoesNotExist, Options{UserMode: true}, true},
505523
// try existing unit in user mode as user
506524
{"syncthing", nil, Options{UserMode: true}, true},
@@ -558,7 +576,6 @@ func TestUnmask(t *testing.T) {
558576
t.Errorf("error on second unmasking is %v, but should have been %v", err, nil)
559577
}
560578
Unmask(ctx, unit, opts)
561-
562579
})
563580
t.Run(fmt.Sprintf("test double unmasking nonexisting"), func(t *testing.T) {
564581
unit := "nonexistant"
@@ -579,5 +596,4 @@ func TestUnmask(t *testing.T) {
579596
t.Errorf("error on second unmasking is %v, but should have been %v", err, ErrDoesNotExist)
580597
}
581598
})
582-
583599
}

0 commit comments

Comments
 (0)