Skip to content

Commit 8ec6b8d

Browse files
committed
+gomatic/funcmap. +env. +stdin.
1 parent ca3344e commit 8ec6b8d

File tree

4 files changed

+52
-119
lines changed

4 files changed

+52
-119
lines changed

TODO.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
- [ ] settings
33
- [ ] templates
44
- input
5-
- [ ] templates on stdin
5+
- [x] templates on stdin
66
- modes
77
- [ ] HTML
88
- [x] text
99
- functions
1010
- [x] environment
11-
- [x] commandLine
11+
- [x] command_line
1212
- [ ] osquery

func.go

-63
This file was deleted.

main.go

+49-53
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import (
77
"io/ioutil"
88
"log"
99
"os"
10-
"path/filepath"
1110
"strings"
1211
"text/template"
1312
"time"
1413

14+
"github.com/gomatic/funcmap"
1515
"gopkg.in/yaml.v2"
1616
)
1717

@@ -49,8 +49,6 @@ type Settings struct {
4949
Debugging bool
5050
//
5151
Verbose bool
52-
//
53-
CommandLine string
5452
}
5553

5654
//
@@ -63,17 +61,10 @@ var settings = Settings{
6361
//
6462
func main() {
6563

66-
if len(os.Args) == 1 {
67-
usage(os.Stdout)
68-
return
69-
}
70-
7164
vars := map[string]interface{}{}
7265
load := map[string]interface{}{}
7366
args := []string{}
7467

75-
settings.CommandLine = commandLine()
76-
7768
// Initialize some settings from the environment.
7869

7970
if m, exists := os.LookupEnv("RENDERIZER_MISSINGKEY"); exists {
@@ -112,6 +103,8 @@ func main() {
112103
nv := strings.SplitN(arg, "=", 2)
113104
if len(nv) != 1 {
114105
settings.Environment = nv[1]
106+
} else {
107+
settings.Environment = "env"
115108
}
116109
case 'v', 'V':
117110
settings.Verbose = true
@@ -281,14 +274,16 @@ func main() {
281274
}
282275
}
283276

277+
files := args
284278
if len(args) == 0 {
285-
usage(os.Stdout)
286-
os.Exit(1)
287-
}
288-
289-
if len(load) == 0 && len(vars) == 0 {
290-
usage(os.Stderr)
291-
fmt.Fprintln(os.Stderr, "WARNING: No variables provided.")
279+
if len(args) < 2 {
280+
stat, _ := os.Stdin.Stat()
281+
isTTY := (stat.Mode() & os.ModeCharDevice) != 0
282+
if isTTY {
283+
log.Println("source: stdin")
284+
}
285+
files = []string{""}
286+
}
292287
}
293288

294289
// Copy the loaded keys in the vars unless provided on the command line.
@@ -299,6 +294,15 @@ func main() {
299294
}
300295
}
301296

297+
if settings.Environment != "" {
298+
v := make(map[string]string)
299+
for _, item := range os.Environ() {
300+
splits := strings.Split(item, "=")
301+
v[splits[0]] = strings.Join(splits[1:], "=")
302+
}
303+
vars[settings.Environment] = v
304+
}
305+
302306
// Dump the settings
303307

304308
if settings.Debugging {
@@ -309,52 +313,44 @@ func main() {
309313

310314
// Execute each template
311315

312-
for _, arg := range args {
313-
file, err := ioutil.ReadFile(arg)
314-
if err != nil {
315-
log.Println(err)
316-
continue
316+
for _, arg := range files {
317+
var file []byte
318+
if arg == "" {
319+
f, err := ioutil.ReadAll(os.Stdin)
320+
if err != nil {
321+
log.Println(err)
322+
continue
323+
}
324+
file = f
325+
} else {
326+
f, err := ioutil.ReadFile(arg)
327+
if err != nil {
328+
log.Println(err)
329+
continue
330+
}
331+
file = f
317332
}
318333

319334
tmpl, err := template.New(arg).
320335
Option(fmt.Sprintf("missingkey=%s", settings.MissingKey)).
321-
Funcs(funcs).
336+
Funcs(funcmap.Map).
322337
Parse(string(file))
323338

324339
if err != nil {
325340
log.Print(err)
326341
}
327342
var sqlBuffer bytes.Buffer
328-
err = tmpl.Execute(&sqlBuffer, vars)
329-
if err != nil {
330-
log.Print(err)
331-
}
343+
func() {
344+
defer func() {
345+
if r := recover(); r != nil {
346+
log.Printf("PANIC: %+v", r)
347+
}
348+
}()
349+
err = tmpl.Execute(&sqlBuffer, vars)
350+
if err != nil {
351+
log.Print(err)
352+
}
353+
}()
332354
fmt.Println(string(sqlBuffer.Bytes()))
333355
}
334356
}
335-
336-
// Reproduce a command line string that reflects a usable command line.
337-
func commandLine() string {
338-
339-
quoter := func(e string) string {
340-
if !strings.Contains(e, " ") {
341-
return e
342-
}
343-
p := strings.SplitN(e, "=", 2)
344-
if strings.Contains(p[0], " ") {
345-
p[0] = `"` + strings.Replace(p[0], `"`, `\"`, -1) + `"`
346-
}
347-
if len(p) == 1 {
348-
return p[0]
349-
}
350-
return p[0] + `="` + strings.Replace(p[1], `"`, `\"`, -1) + `"`
351-
}
352-
each := func(s []string) (o []string) {
353-
o = make([]string, len(s))
354-
for i, t := range s {
355-
o[i] = quoter(t)
356-
}
357-
return
358-
}
359-
return filepath.Base(os.Args[0]) + " " + strings.Join(each(os.Args[1:]), " ")
360-
}

test/pod.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Generated: {{now.UTC.Format "2006-01-02T15:04:05MST"}}
2-
# With: {{commandLine}}
2+
# With: {{command_line}}
33
# From: {{environment "USER"}}@{{environment "HOST"}}:{{environment "PWD"}}
44
apiVersion: v1
55
kind: Pod

0 commit comments

Comments
 (0)