/
RemoveEscapedCharacters.go
107 lines (91 loc) · 2.82 KB
/
RemoveEscapedCharacters.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
package main
import (
"bufio"
"bytes"
"fmt"
"html"
"os"
"path/filepath"
"strings"
"howett.net/plist"
)
// RemoveEscapedCharacters removes escaped characters from a plist / .mobileconfig file
func main() {
reader := bufio.NewReader(os.Stdin)
// Prompt for and read the source plist file path
fmt.Print("Enter the path to the source plist file: ")
sourceFilePath, err := readInput(reader)
if err != nil {
fmt.Println("Error reading source file path:", err)
return
}
// Prompt for and read the destination folder path
fmt.Print("Enter the destination folder path: ")
destFolderPath, err := readInput(reader)
if err != nil {
fmt.Println("Error reading destination folder path:", err)
return
}
// Read and decode the plist file content
data, err := decodePlistFile(sourceFilePath)
if err != nil {
fmt.Println("Error:", err)
return
}
// Re-encode to XML to ensure correct formatting
newPlist, err := plist.MarshalIndent(data, plist.XMLFormat, "\t")
if err != nil {
fmt.Println("Error marshaling plist:", err)
return
}
// Construct the output file path and write the new plist content
outputFilePath := filepath.Join(destFolderPath, filepath.Base(sourceFilePath))
if err := writeFile(outputFilePath, newPlist); err != nil {
fmt.Println("Error writing file:", err)
return
}
fmt.Printf("Plist file has been reformatted and written successfully to '%s'.\n", outputFilePath)
}
// ReadInput simplifies reading a line of text input
func readInput(reader *bufio.Reader) (string, error) {
input, err := reader.ReadString('\n')
if err != nil {
return "", err
}
return filepath.Clean(strings.TrimSpace(input)), nil
}
// DecodePlistFile handles reading and unmarshaling a plist file
func decodePlistFile(filePath string) (interface{}, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("error opening file '%s': %v", filePath, err)
}
defer file.Close()
// Read the file content
var buf bytes.Buffer
if _, err := buf.ReadFrom(file); err != nil {
return nil, fmt.Errorf("error reading file '%s': %v", filePath, err)
}
content := buf.Bytes()
// Check if content needs unescaping
if bytes.Contains(content, []byte("<")) {
content = []byte(UnescapeXML(string(content)))
}
decoder := plist.NewDecoder(bytes.NewReader(content))
var data interface{}
if err := decoder.Decode(&data); err != nil {
return nil, fmt.Errorf("error decoding plist: %v", err)
}
return data, nil
}
// UnescapeXML takes an XML string with HTML entities and returns the unescaped XML.
func UnescapeXML(encodedXML string) string {
return html.UnescapeString(encodedXML)
}
// WriteFile handles the file writing process
func writeFile(filePath string, data []byte) error {
if err := os.WriteFile(filePath, data, 0644); err != nil {
return fmt.Errorf("error writing file '%s': %v", filePath, err)
}
return nil
}