fix(ansible): include diff path in CLI output
This commit is contained in:
parent
e5b891e7d7
commit
324411bb95
2 changed files with 33 additions and 6 deletions
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"},
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue