From 324411bb959591e00719a5ee2b0409ac28c7f5ba Mon Sep 17 00:00:00 2001 From: Virgil Date: Fri, 3 Apr 2026 11:30:48 +0000 Subject: [PATCH] fix(ansible): include diff path in CLI output --- cmd/ansible/ansible.go | 24 ++++++++++++++++++------ cmd/ansible/ansible_test.go | 15 +++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/cmd/ansible/ansible.go b/cmd/ansible/ansible.go index 24be273..64c9a1f 100644 --- a/cmd/ansible/ansible.go +++ b/cmd/ansible/ansible.go @@ -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) } } } diff --git a/cmd/ansible/ansible_test.go b/cmd/ansible/ansible_test.go index eeb8aa1..f9054cf 100644 --- a/cmd/ansible/ansible_test.go +++ b/cmd/ansible/ansible_test.go @@ -194,6 +194,21 @@ func TestBuildPlaybookCommandSettings_Bad_MissingPlaybook(t *testing.T) { assert.Contains(t, err.Error(), "usage: ansible ") } +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"},