Skip to content

Commit a4737a3

Browse files
authored
feat(zc1265): insert --now after systemctl enable (#1211)
1 parent e494f27 commit a4737a3

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

pkg/fix/integration_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,14 @@ func TestFixIntegration_ZC1267_DfAddPortable(t *testing.T) {
729729
}
730730
}
731731

732+
func TestFixIntegration_ZC1265_SystemctlEnableNow(t *testing.T) {
733+
src := "systemctl enable nginx\n"
734+
want := "systemctl enable --now nginx\n"
735+
if got := runFix(t, src); got != want {
736+
t.Errorf("got %q, want %q", got, want)
737+
}
738+
}
739+
732740
func TestFixIntegration_ZC1283_SetOToSetopt(t *testing.T) {
733741
src := "set -o pipefail\n"
734742
want := "setopt pipefail\n"

pkg/katas/zc1265.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,70 @@ func init() {
1212
Description: "`systemctl enable` without `--now` only enables on next boot. " +
1313
"Use `--now` to enable and immediately start the service.",
1414
Check: checkZC1265,
15+
Fix: fixZC1265,
1516
})
1617
}
1718

19+
// fixZC1265 inserts ` --now` after the `enable` subcommand in a
20+
// `systemctl enable …` invocation. Same subcommand-level insertion
21+
// pattern as ZC1234's docker-run --rm.
22+
func fixZC1265(node ast.Node, _ Violation, source []byte) []FixEdit {
23+
cmd, ok := node.(*ast.SimpleCommand)
24+
if !ok {
25+
return nil
26+
}
27+
ident, ok := cmd.Name.(*ast.Identifier)
28+
if !ok || ident.Value != "systemctl" {
29+
return nil
30+
}
31+
var enableArg ast.Expression
32+
for _, arg := range cmd.Arguments {
33+
if arg.String() == "enable" {
34+
enableArg = arg
35+
break
36+
}
37+
}
38+
if enableArg == nil {
39+
return nil
40+
}
41+
tok := enableArg.TokenLiteralNode()
42+
off := LineColToByteOffset(source, tok.Line, tok.Column)
43+
if off < 0 || off+len("enable") > len(source) {
44+
return nil
45+
}
46+
if string(source[off:off+len("enable")]) != "enable" {
47+
return nil
48+
}
49+
insertAt := off + len("enable")
50+
insLine, insCol := offsetLineColZC1265(source, insertAt)
51+
if insLine < 0 {
52+
return nil
53+
}
54+
return []FixEdit{{
55+
Line: insLine,
56+
Column: insCol,
57+
Length: 0,
58+
Replace: " --now",
59+
}}
60+
}
61+
62+
func offsetLineColZC1265(source []byte, offset int) (int, int) {
63+
if offset < 0 || offset > len(source) {
64+
return -1, -1
65+
}
66+
line := 1
67+
col := 1
68+
for i := 0; i < offset; i++ {
69+
if source[i] == '\n' {
70+
line++
71+
col = 1
72+
continue
73+
}
74+
col++
75+
}
76+
return line, col
77+
}
78+
1879
func checkZC1265(node ast.Node) []Violation {
1980
cmd, ok := node.(*ast.SimpleCommand)
2081
if !ok {

0 commit comments

Comments
 (0)