Skip to content

feat: EnsureSystemdKey — idempotent systemd unit directive (v0.14.0)#10

Merged
Booyaka101 merged 1 commit into
mainfrom
feat/ensure-systemd-key
Jun 12, 2026
Merged

feat: EnsureSystemdKey — idempotent systemd unit directive (v0.14.0)#10
Booyaka101 merged 1 commit into
mainfrom
feat/ensure-systemd-key

Conversation

@Booyaka101

Copy link
Copy Markdown
Owner

What

Adds porter.EnsureSystemdKey(path, section, key, value) — idempotently ensures a key=value directive under a systemd unit's [section] (Unit/Service/Timer/Install).

  • Inserts the line directly after the section header — the only correct place for a systemd key. EnsureLine could only append a stray line at EOF.
  • No-ops when a key= line is already present anywhere (a hand-edited value wins).
  • Preserves CRLF; preserves the unit's mode/owner on rewrite (cp over the existing file).
  • .Sudo() for units under /etc/systemd/system; follow with Run("systemctl daemon-reload").
porter.EnsureSystemdKey("/etc/systemd/system/app.service", "Service", "Restart", "always").Sudo()

Why

General-use gap surfaced reviewing bulk's deploy code (idnerdidx/bulk#396), which hand-rolls exactly this section-aware key insertion over a raw SSH client (casino/systemd EnsureServiceKey/EnsureUnitKey). Porter's EnsureLine appends at EOF, which is wrong for a sectioned INI file — so any deploy needing Restart=, ExecStartPre=, After=, etc. had to drop out of porter. This closes that.

Tests

Pure-function coverage (insert placement, no-op-when-present, missing-section error, CRLF) in features_test.go; read→insert→write + no-op dispatch via the fake runner in runtime_test.go. Full suite green; go vet/gofmt clean. Bumps version to 0.14.0.

🤖 Generated with Claude Code

Adds porter.EnsureSystemdKey(path, section, key, value): ensures a key=value
directive under a systemd unit's [section] (Unit/Service/Timer/Install),
inserting it directly after the section header rather than appending a stray
line at EOF the way EnsureLine would. Idempotent — no-ops when the key is
already present anywhere (a hand-edited value wins) — preserves CRLF, and
preserves the unit's mode/owner on rewrite (cp over the existing file). .Sudo()
for units under /etc/systemd/system.

General-use gap surfaced by bulk's deploy code, which hand-rolls the same
section-aware key insertion over a raw SSH client (casino/systemd
EnsureServiceKey/EnsureUnitKey); porter's EnsureLine couldn't target a section.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@Booyaka101 Booyaka101 merged commit d444181 into main Jun 12, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants