forked from noqcks/gucci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
78 lines (70 loc) · 1.51 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
77
78
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
)
func env() map[string]string {
env := make(map[string]string)
for _, i := range os.Environ() {
key, val := getkeyval(i)
env[key] = val
}
return env
}
func getkeyval(item string) (key, val string) {
splits := strings.Split(item, "=")
key = splits[0]
val = strings.Join(splits[1:], "=")
return key, val
}
func noArgs() bool {
if len(os.Args) < 2 {
return true
}
return false
}
func loadFile(tplFile string) (*template.Template, error) {
tplName := filepath.Base(tplFile)
tpl, err := template.New(tplName).Funcs(funcMap).ParseFiles(tplFile)
if err != nil {
return nil, fmt.Errorf("Error parsing template(s): %v", err)
}
return tpl, nil
}
func loadStream(name string, in io.Reader) (*template.Template, error) {
tplBytes, err := ioutil.ReadAll(in)
if err != nil {
return nil, fmt.Errorf("Error reading template(s): %v", err)
}
tplStr := string(tplBytes)
return loadString(name, tplStr)
}
func loadString(name, s string) (*template.Template, error) {
tpl, err := template.New(name).Funcs(funcMap).Parse(s)
if err != nil {
return nil, fmt.Errorf("Error parsing template(s): %v", err)
}
return tpl, nil
}
func loadFileOrStdin(f string) (*template.Template, error) {
var tpl *template.Template
if f == "" {
t, err := loadStream("-", os.Stdin)
if err != nil {
return nil, err
}
tpl = t
} else {
t, err := loadFile(f)
if err != nil {
return nil, err
}
tpl = t
}
return tpl, nil
}