Skip to content

Add yamledit package and preserve wordpress_sites.yml formatting - #748

Open
swalkinshaw wants to merge 2 commits into
masterfrom
yamledit
Open

swalkinshaw wants to merge 2 commits into
masterfrom
yamledit

Conversation

@swalkinshaw

Copy link
Copy Markdown
Member

Adds trellis/yamledit, a small wrapper around go.yaml.in/yaml/v3's Node API for making targeted edits to YAML files without losing formatting. This is groundwork for trellis vault generate (#426), which is stacked on top of this PR.

yamledit

  • Load/Parse, Lookup, Decode, Set, Delete, RenameKey, SetHeadComment, Save
  • Paths are key segments, and sequence items are addressed by index (e.g. vault_users.0.salt). Keys containing dots like example.com work fine.
  • Preserves comments, key order, and the quoting style of replaced scalars ("generateme" stays double-quoted)
  • Blank lines: yaml.v3 (and v4) drop blank lines between entries. They're recorded at parse time and restored on output. Every Trellis fixture now round-trips byte for byte.
  • go.yaml.in/yaml/v3 is the maintained fork of the archived gopkg.in/yaml.v3 and was already in go.sum through testify. goccy/go-yaml was also considered: it round-trips well, but its path lookups don't handle dotted keys and renaming or inserting keys produced broken output.

trellis new: fixes wordpress_sites.yml being rewritten

Previously new round-tripped wordpress_sites.yml through the Site struct with yaml.v2, which:

  • removed all of Trellis's inline comments (# replace with your Git repo URL, etc.)
  • silently dropped site options the struct doesn't model. With Trellis v1.31.1 that includes xmlrpc.

It now edits the file in place. Compared with upstream, the only differences are the site name, the hosts, and a # Created by trellis-cli header line.

yaml.v2 is still used for read-only parsing and the update-check state file; moving those over is optional and left out of this PR.

Testing

  • Unit tests for yamledit, including round-tripping every fixture
  • Existing UpdateDefaultConfig expectations kept as UpdateDefaultSiteFile tests, plus a full-file formatting test
  • Ran trellis new locally and diffed the generated wordpress_sites.yml against upstream v1.31.1

🤖 Generated with Claude Code

swalkinshaw and others added 2 commits October 18, 2026 12:13
Adds trellis/yamledit, a small wrapper around go.yaml.in/yaml/v3's Node
API for making targeted edits to YAML files while preserving comments,
key order, quoting style, and blank lines (which yaml.v3 normally drops).

`trellis new` now uses it to update wordpress_sites.yml in place instead
of round-tripping it through the Site struct with yaml.v2. Previously
this stripped Trellis' inline comments and silently dropped any site
options the struct doesn't model (such as `xmlrpc`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant