-
Notifications
You must be signed in to change notification settings - Fork 1
/
clixml.go
71 lines (56 loc) · 1.55 KB
/
clixml.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
package parsing
import (
"encoding/xml"
"errors"
"fmt"
"strings"
)
// clixml represents the structure for unmarshaling CLIXML.
type clixml struct {
Xml []string `xml:"S"`
}
// unmarshal unmarshals a CLIXML string to an XML object.
func (x *clixml) unmarshal(clixml string) error {
// Remove CLIXML identifier
clixml = strings.ReplaceAll(clixml, "#< CLIXML", "")
// Unmarshal to XML
if err := xml.Unmarshal([]byte(clixml), &x); err != nil {
return err
}
return nil
}
// stringSlice removes all unnecessary characters and whitespaces from the XML document
// and returns a new string slice.
func (x *clixml) stringSlice() []string {
result := make([]string, len(x.Xml))
for i, v := range x.Xml {
// Trim whitespaces
s := strings.TrimSpace(v)
// Remove specific characters
s = strings.ReplaceAll(s, "_x000D__x000A_", "")
// Handle line continuation
if len(s) > 2 && s[0] == '+' {
result[i] = fmt.Sprintf("\n%s", s[2:])
} else {
result[i] = s
}
}
return result
}
// DecodeCliXmlErr converts a CLIXML error string to a
// human-readable PowerShell error message.
func DecodeCliXmlErr(text string) (string, error) {
// Check if input string is a valid CLIXML document
if !strings.Contains(text, "#< CLIXML") {
return "", errors.New("parsing.DecodeCliXmlErr: the input string is not a CLIXML error string")
}
clixml := &clixml{}
// Unmarshal to XML
if err := clixml.unmarshal(text); err != nil {
return "", err
}
// Convert to string slice
s := clixml.stringSlice()
// Join new string slice
return strings.Join(s, ""), nil
}