Skip to content

Commit 9017054

Browse files
authored
Merge pull request #82 from tongson/ansible_changed
Support logging Ansible changed status
2 parents b6f9f06 + 1e45ec4 commit 9017054

File tree

7 files changed

+61
-21
lines changed

7 files changed

+61
-21
lines changed

const.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const cHOSTS1 = "ssh_config"
1010
const cHOSTS2 = "HOSTS"
1111
const cLOG = "LOG"
1212
const cREPAIRED = "__REPAIRED__"
13+
const cCHANGED = "changed=true"
1314
const cRUN = "script"
1415
const cPRE = "script.pre"
1516
const cPOST = "script.post"

main.go

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,23 @@ func generateHashID() string {
677677
return string([]rune(uid)[:8])
678678
}
679679

680+
func customResult(stdout *bufio.Scanner, stderr *bufio.Scanner) string {
681+
var result string
682+
for stderr.Scan() {
683+
if strings.Contains(stderr.Text(), cREPAIRED) {
684+
result = "repaired"
685+
}
686+
}
687+
// It's unlikely we are gonna look for cCHANGED and cREPAIRED at the same time.
688+
// cCHANGED (Ansible output) has priority.
689+
for stdout.Scan() {
690+
if strings.Contains(stdout.Text(), cCHANGED) {
691+
result = "changed"
692+
}
693+
}
694+
return result
695+
}
696+
680697
func main() {
681698
runtime.MemProfileRate = 0
682699

@@ -1052,13 +1069,11 @@ func main() {
10521069
failed = true
10531070
failedLogPrint(scr, opt, out)
10541071
} else {
1055-
scanner := bufio.NewScanner(strings.NewReader(out.Stderr))
1056-
scanner.Split(bufio.ScanWords)
1057-
for scanner.Scan() {
1058-
if strings.Contains(scanner.Text(), cREPAIRED) {
1059-
result = "repaired"
1060-
}
1061-
}
1072+
scanner_err := bufio.NewScanner(strings.NewReader(out.Stderr))
1073+
scanner_out := bufio.NewScanner(strings.NewReader(out.Stdout))
1074+
scanner_err.Split(bufio.ScanWords)
1075+
scanner_out.Split(bufio.ScanWords)
1076+
result = customResult(scanner_out, scanner_err)
10621077
okLogPrint(scr, opt, out)
10631078
}
10641079
} else if _, err := strconv.ParseInt(hostname, 10, 64); err == nil {
@@ -1116,13 +1131,11 @@ func main() {
11161131
failed = true
11171132
failedLogPrint(scr, opt, out)
11181133
} else {
1119-
scanner := bufio.NewScanner(strings.NewReader(out.Stderr))
1120-
scanner.Split(bufio.ScanWords)
1121-
for scanner.Scan() {
1122-
if strings.Contains(scanner.Text(), cREPAIRED) {
1123-
result = "repaired"
1124-
}
1125-
}
1134+
scanner_err := bufio.NewScanner(strings.NewReader(out.Stderr))
1135+
scanner_out := bufio.NewScanner(strings.NewReader(out.Stdout))
1136+
scanner_err.Split(bufio.ScanWords)
1137+
scanner_out.Split(bufio.ScanWords)
1138+
result = customResult(scanner_out, scanner_err)
11261139
okLogPrint(scr, opt, out)
11271140
}
11281141
} else {
@@ -1219,13 +1232,11 @@ func main() {
12191232
failed = true
12201233
failedLogPrint(scr, opt, out)
12211234
} else {
1222-
scanner := bufio.NewScanner(strings.NewReader(out.Stderr))
1223-
scanner.Split(bufio.ScanWords)
1224-
for scanner.Scan() {
1225-
if strings.Contains(scanner.Text(), cREPAIRED) {
1226-
result = "repaired"
1227-
}
1228-
}
1235+
scanner_err := bufio.NewScanner(strings.NewReader(out.Stderr))
1236+
scanner_out := bufio.NewScanner(strings.NewReader(out.Stdout))
1237+
scanner_err.Split(bufio.ScanWords)
1238+
scanner_out.Split(bufio.ScanWords)
1239+
result = customResult(scanner_out, scanner_err)
12291240
okLogPrint(scr, opt, out)
12301241
}
12311242
}

test/changed/linebreak/script

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
printf "ok\\n"
2+
printf "\\n\\nchanged=true\\n"

test/changed/linebreak/shell

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bash

test/changed/nolinebreak/script

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
printf "ok\\n"
2+
printf "changed=true"

test/changed/nolinebreak/shell

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bash

test/rr_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,28 @@ func TestRepaired(T *testing.T) {
132132
})
133133
}
134134

135+
func TestChanged(T *testing.T) {
136+
T.Parallel()
137+
T.Run("changed1", func(t *testing.T) {
138+
rr := RunArg{Exe: cEXE, Args: []string{"changed:nolinebreak"}}
139+
if ret, _ := rr.Run(); !ret {
140+
t.Error("wants `true`")
141+
}
142+
if got := strings.Contains(FileRead("LOG"), "\"msg\":\"changed\""); !got {
143+
t.Error("wants `true`")
144+
}
145+
})
146+
T.Run("changed2", func(t *testing.T) {
147+
rr := RunArg{Exe: cEXE, Args: []string{"changed:linebreak"}}
148+
if ret, _ := rr.Run(); !ret {
149+
t.Error("wants `true`")
150+
}
151+
if got := strings.Contains(FileRead("LOG"), "\"msg\":\"changed\""); !got {
152+
t.Error("wants `true`")
153+
}
154+
})
155+
}
156+
135157
func TestArgs(T *testing.T) {
136158
T.Parallel()
137159
T.Run("args1", func(t *testing.T) {

0 commit comments

Comments
 (0)