-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
84 lines (70 loc) · 1.9 KB
/
Copy pathmain.go
File metadata and controls
84 lines (70 loc) · 1.9 KB
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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
)
var version string
func main() {
inFile := flag.String("input", "", "Path to input ini file.")
outFile := flag.String("output", "", "Path to output ini file.")
delimiter := flag.String("delimit", ";", "Split the modify string on this value.")
modify := flag.String("modify", "", "Modifications to make. ex: [section1];prop1=value1;prop2=value2;[section2];prop1=value3.")
newline := flag.String("newline", "\n", "Output uses this line ending.")
showVersion := flag.Bool("version", false, "output the version")
flag.Parse()
if *showVersion {
fmt.Println(version)
return
}
if *inFile == "" && *modify == "" {
fmt.Fprintf(os.Stderr, "arguments -input and/or -modify are required\n\n")
flag.PrintDefaults()
os.Exit(1)
}
file := newFile()
if *inFile != "" {
channel := make(chan string)
go readFile(channel, *inFile)
file.merge(channel, true)
}
if *modify != "" {
channel := make(chan string)
go readModify(channel, *modify, *delimiter)
file.merge(channel, false)
}
output := strings.Join(file.render(), *newline) + *newline
if *outFile == "" {
fmt.Printf(output)
} else {
writeFile(*outFile, output)
}
}
func readFile(channel chan<- string, filename string) {
content, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "error reading file: %v\n", err)
os.Exit(2)
}
for _, line := range strings.Split(string(content), "\n") {
channel <- line
}
close(channel)
}
func readModify(channel chan<- string, modify string, delimiter string) {
for _, line := range strings.Split(modify, "\n") {
for _, line := range strings.Split(line, delimiter) {
channel <- line
}
}
close(channel)
}
func writeFile(filename string, contents string) {
err := ioutil.WriteFile(filename, []byte(contents), 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "error writing file: %v\n", err)
os.Exit(3)
}
}