-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreplace_shared.go
More file actions
64 lines (58 loc) · 1.72 KB
/
replace_shared.go
File metadata and controls
64 lines (58 loc) · 1.72 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
// -----------------------------------------------------------------------------
// CMDX Utilities Suite cmdx/[replace_shared.go]
// (c) balarabe@protonmail.com License: GPLv3
// -----------------------------------------------------------------------------
package main
import (
"path/filepath"
"strings"
)
// getConfigBool _ _
func getConfigBool(s, keyword string) (value, exists bool) {
// TODO: apply this function in replaceStringsInFiles
s = strings.ToUpper(s)
keyword = strings.ToUpper(keyword)
for i, ar := range [][]string{
{"0", "FALSE", "OFF", "IGNORE"},
{"1", "TRUE", "ON", "MATCH"},
} {
for _, match := range ar {
if strings.HasPrefix(s, keyword+" "+match) {
return i == 1, true
}
}
}
return false, false
}
// hasConfigBool _ _
func hasConfigBool(s, keyword string) (ret bool) {
// TODO: apply this function in replaceStringsInFiles
_, ret = getConfigBool(s, keyword)
return ret
}
// readConfigFileLines _ _
func readConfigFileLines(configFile string) (configLines []string) {
// TODO: use this function in replaceStringsInFiles()
// TODO: do away with this function, use env.ReadFileLines() instead
var err error
configFile, err = filepath.Abs(configFile)
if err != nil {
env.Println("command file path error: ", configFile)
return
}
env.Println("FILE:", configFile)
data, done := env.ReadFile(configFile)
if !done {
return []string{}
}
s := string(data)
s = strings.TrimSpace(s)
s = strings.ReplaceAll(s, "\r\n", "\n")
for strings.Contains(s, "\n\n") {
s = strings.ReplaceAll(s, "\n\n", "\n")
}
configLines = strings.Split(s, "\n")
configLines = append(configLines, "") // initiates replacement
return configLines
}
// end