forked from flowonyx/runtemplate
/
util.go
76 lines (66 loc) · 1.43 KB
/
util.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
package support
import (
"fmt"
"os"
"strings"
)
var ShowContextInfo = false
var Verbose = false
var Dbg = false
func Fail(args ...interface{}) {
fmt.Fprint(os.Stderr, "Error: ")
fmt.Fprintln(os.Stderr, args...)
os.Exit(1)
}
func Progress(msg string, args ...interface{}) {
if Verbose {
fmt.Printf(msg, args...)
}
}
func Debug(msg string, args ...interface{}) {
if Dbg {
fmt.Printf("-- "+msg, args...)
}
}
//-------------------------------------------------------------------------------------------------
func FindTemplateArg(tpl string, args []string) (string, []string) {
if tpl != "" {
return tpl, args
}
var left []string
for _, a := range args {
if strings.HasSuffix(a, ".tpl") {
tpl = a
} else {
left = append(left, a)
}
}
return tpl, left
}
func expandSpecialChars(s string) string {
s2 := strings.Replace(s, `\n`, "\n", -1)
return strings.Replace(s2, `\t`, "\t", -1)
}
func SplitKeyValArgs(args []string) (Tuples, Pairs, []string) {
var types Tuples
var others Pairs
var leftover []string
for _, a := range args {
found := false
co := strings.IndexByte(a, ':')
tr := NewTuple(a)
if tr.Valid() {
types = append(types, tr)
found = true
} else if co >= 0 {
k, v := a[:co], a[co+1:]
p := Pair{Key: k, Val: RichString(expandSpecialChars(string(v)))}
others = append(others, p)
found = p.Valid()
}
if !found {
leftover = append(leftover, a)
}
}
return types, others, leftover
}