-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
126 lines (108 loc) · 2.87 KB
/
main.go
File metadata and controls
126 lines (108 loc) · 2.87 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// =================================================================
//
// Copyright (C) 2019 Spatial Current, Inc. - All Rights Reserved
// Released as open source under the MIT License. See LICENSE file.
//
// =================================================================
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"text/template"
)
import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
import (
"github.com/spatialcurrent/go-adaptive-functions/af"
"github.com/spatialcurrent/go-simple-serializer/gss"
)
func initFunctions() map[string]interface{} {
funcs := map[string]interface{}{
"parse": func(args ...interface{}) (interface{}, error) {
if len(args) != 2 {
return nil, errors.New("invalid arguments for parse " + fmt.Sprint(args))
}
if text, ok := args[1].(string); ok {
if f, ok := args[0].(string); ok {
t, err := gss.GetType([]byte(text), f)
if err != nil {
return "", errors.Wrap(err, "error creating new object for format "+f)
}
return gss.DeserializeString(
text,
f,
gss.NoHeader,
gss.NoComment,
true,
0,
gss.NoLimit,
t,
false,
false)
}
}
return nil, errors.New("invalid arguments for parse " + fmt.Sprint(args))
},
}
for _, f := range af.Functions {
f := f
for _, alias := range f.Aliases {
alias := alias
funcs[alias] = func(args ...interface{}) (interface{}, error) {
if len(args) <= 1 {
return f.ValidateRun(args)
}
return f.ValidateRun(append([]interface{}{args[len(args)-1]}, args[0:len(args)-1]...))
}
}
}
return funcs
}
func main() {
cmd := &cobra.Command{
Use: "gotmpl [k=v]... < template_file",
DisableFlagsInUseLine: true,
Short: "gotmpl",
Long: `gotmpl is a super simple command line program for rendering templates that uses environment variables and command line arguments as its context variables. The template is read from stdin.`,
RunE: func(cmd *cobra.Command, args []string) error {
fi, err := os.Stdin.Stat()
if err != nil {
return err
}
if fi.Mode()&os.ModeNamedPipe == 0 && !fi.Mode().IsRegular() {
return cmd.Usage()
}
text, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return err
}
ctx := map[string]string{}
// load context from environment variables
for _, str := range os.Environ() {
parts := strings.SplitN(str, "=", 2)
ctx[parts[0]] = parts[1]
}
// load context from command line arguments
for _, str := range args {
parts := strings.SplitN(str, "=", 2)
ctx[parts[0]] = parts[1]
}
tmpl, err := template.New("main").Funcs(initFunctions()).Parse(string(text))
if err != nil {
return err
}
err = tmpl.Execute(os.Stdout, ctx)
if err != nil {
return err
}
return nil
},
}
if err := cmd.Execute(); err != nil {
panic(err)
}
}