-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathini.go
More file actions
64 lines (59 loc) · 1.36 KB
/
Copy pathini.go
File metadata and controls
64 lines (59 loc) · 1.36 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
package ipcheck
import (
"bufio"
"bytes"
"fmt"
"os"
"strings"
)
func parseINIFile(path string) (map[string]map[string]string, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return parseINI(data), nil
}
func parseINI(data []byte) map[string]map[string]string {
res := map[string]map[string]string{}
section := "default"
res[section] = map[string]string{}
scanner := bufio.NewScanner(bytes.NewReader(data))
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "#") || strings.HasPrefix(line, ";") {
continue
}
if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
section = strings.TrimSpace(line[1 : len(line)-1])
if _, ok := res[section]; !ok {
res[section] = map[string]string{}
}
continue
}
idx := strings.Index(line, "=")
if idx < 0 {
continue
}
key := strings.TrimSpace(line[:idx])
val := strings.TrimSpace(line[idx+1:])
res[section][key] = val
}
return res
}
func writeIfMissing(path, content string) (bool, error) {
if _, err := os.Stat(path); err == nil {
return false, nil
}
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
return false, err
}
return true, nil
}
func printFile(path string) error {
data, err := os.ReadFile(path)
if err != nil {
return err
}
fmt.Print(string(data))
return nil
}