-
Notifications
You must be signed in to change notification settings - Fork 2
/
xml.go
161 lines (153 loc) · 3.27 KB
/
xml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// The xml.go file contains functions to parse WinRM service's xml response.
package gopsremote
import (
"encoding/base64"
"encoding/xml"
"io"
"strings"
)
func CaptureAttribute(body io.Reader, tag, attr string) (string, error) {
decoder := xml.NewDecoder(body)
for {
token, err := decoder.Token()
if err != nil {
if err == io.EOF {
break
}
return "", err
}
switch d := token.(type) {
case xml.StartElement:
if d.Name.Local == tag {
for _, a := range d.Attr {
if a.Name.Local == attr {
return a.Value, nil
}
}
}
}
}
return "", nil
}
func CaptureStreams(body io.Reader, stdout, stderr io.Writer) (string, bool, error) {
decoder := xml.NewDecoder(body)
var comp, exitCode string
var isComplete bool
for {
if exitCode != "" {
return exitCode, isComplete, nil
}
token, err := decoder.Token()
if err != nil {
if err == io.EOF {
break
}
return "", false, err
}
switch d := token.(type) {
case xml.StartElement:
if d.Name.Local == "Stream" {
for _, a := range d.Attr {
if a.Name.Local == "Name" {
if a.Value == "stdout" {
comp = "stdout"
} else if a.Value == "stderr" {
comp = "stderr"
}
}
}
} else if d.Name.Local == "CommandState" {
for _, a := range d.Attr {
if a.Name.Local == "State" && a.Value == "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done" {
isComplete = true
}
}
} else if d.Name.Local == "ExitCode" {
comp = "code"
}
case xml.CharData:
switch comp {
case "stdout":
writeBase64Decode(d, stdout)
case "stderr":
writeBase64Decode(d, stderr)
case "code":
exitCode = strings.TrimSpace(string(d))
}
comp = ""
}
}
return "", false, nil
}
// Decode content into base64 and writes to writer
func writeBase64Decode(b []byte, writer io.Writer) error {
content, err := base64.StdEncoding.DecodeString(string(b))
if err != nil {
return err
}
_, err = writer.Write(content)
if err != nil {
return err
}
return nil
}
func CaptureText(body io.Reader, tag string) (string, error) {
decoder := xml.NewDecoder(body)
found := false
for {
token, err := decoder.Token()
if err != nil {
if err == io.EOF {
break
}
return "", err
}
switch d := token.(type) {
case xml.StartElement:
if d.Name.Local == tag {
found = true
}
case xml.CharData:
if found {
return strings.TrimSpace(string(d)), nil
}
}
}
return "", nil
}
func CaptureErrorMessages(errorMessage string) (string, error) {
index := strings.Index(errorMessage, "<Objs")
if index == -1 {
return errorMessage, nil
}
errorMessage = errorMessage[index:]
decoder := xml.NewDecoder(strings.NewReader(errorMessage))
builder := strings.Builder{}
isError := false
for {
token, err := decoder.Token()
if err != nil {
if err == io.EOF {
return builder.String(), nil
}
return "", err
}
switch v := token.(type) {
case xml.StartElement:
if v.Name.Local == "S" {
for _, a := range v.Attr {
if a.Name.Local == "S" && a.Value == "Error" {
isError = true
break
}
}
}
case xml.CharData:
if isError {
builder.WriteString(strings.TrimSuffix(string(v), "_x000D__x000A_"))
builder.WriteString("\n")
isError = false
}
}
}
}