fix(ansible): include diff path in CLI output
Some checks are pending
CI / test (push) Waiting to run
CI / auto-fix (push) Waiting to run
CI / auto-merge (push) Waiting to run

This commit is contained in:
Virgil 2026-04-03 11:30:48 +00:00
parent e5b891e7d7
commit 324411bb95
2 changed files with 33 additions and 6 deletions

View file

@ -253,6 +253,22 @@ func buildPlaybookCommandSettings(opts core.Options, rawArgs []string) (playbook
}, nil
}
func diffOutputLines(diff map[string]any) []string {
lines := []string{"diff:"}
if path, ok := diff["path"].(string); ok && path != "" {
lines = append(lines, sprintf("path: %s", path))
}
if before, ok := diff["before"].(string); ok && before != "" {
lines = append(lines, sprintf("- %s", before))
}
if after, ok := diff["after"].(string); ok && after != "" {
lines = append(lines, sprintf("+ %s", after))
}
return lines
}
func runPlaybookCommand(opts core.Options) core.Result {
settings, err := buildPlaybookCommandSettings(opts, os.Args[1:])
if err != nil {
@ -348,12 +364,8 @@ func runPlaybookCommand(opts core.Options) core.Result {
if executor.Diff {
if diff, ok := result.Data["diff"].(map[string]any); ok {
print("diff:")
if before, ok := diff["before"].(string); ok && before != "" {
print("- %s", before)
}
if after, ok := diff["after"].(string); ok && after != "" {
print("+ %s", after)
for _, line := range diffOutputLines(diff) {
print("%s", line)
}
}
}

View file

@ -194,6 +194,21 @@ func TestBuildPlaybookCommandSettings_Bad_MissingPlaybook(t *testing.T) {
assert.Contains(t, err.Error(), "usage: ansible <playbook>")
}
func TestDiffOutputLines_Good_IncludesPathAndBeforeAfter(t *testing.T) {
lines := diffOutputLines(map[string]any{
"path": "/etc/nginx/conf.d/app.conf",
"before": "server_name=old.example.com;",
"after": "server_name=web01.example.com;",
})
assert.Equal(t, []string{
"diff:",
"path: /etc/nginx/conf.d/app.conf",
"- server_name=old.example.com;",
"+ server_name=web01.example.com;",
}, lines)
}
func TestTestKeyFile_Good_PrefersExplicitKey(t *testing.T) {
opts := core.NewOptions(
core.Option{Key: "key", Value: "/tmp/id_ed25519"},