@@ -7,11 +7,11 @@ import (
7
7
"io/ioutil"
8
8
"log"
9
9
"os"
10
- "path/filepath"
11
10
"strings"
12
11
"text/template"
13
12
"time"
14
13
14
+ "github.com/gomatic/funcmap"
15
15
"gopkg.in/yaml.v2"
16
16
)
17
17
@@ -49,8 +49,6 @@ type Settings struct {
49
49
Debugging bool
50
50
//
51
51
Verbose bool
52
- //
53
- CommandLine string
54
52
}
55
53
56
54
//
@@ -63,17 +61,10 @@ var settings = Settings{
63
61
//
64
62
func main () {
65
63
66
- if len (os .Args ) == 1 {
67
- usage (os .Stdout )
68
- return
69
- }
70
-
71
64
vars := map [string ]interface {}{}
72
65
load := map [string ]interface {}{}
73
66
args := []string {}
74
67
75
- settings .CommandLine = commandLine ()
76
-
77
68
// Initialize some settings from the environment.
78
69
79
70
if m , exists := os .LookupEnv ("RENDERIZER_MISSINGKEY" ); exists {
@@ -112,6 +103,8 @@ func main() {
112
103
nv := strings .SplitN (arg , "=" , 2 )
113
104
if len (nv ) != 1 {
114
105
settings .Environment = nv [1 ]
106
+ } else {
107
+ settings .Environment = "env"
115
108
}
116
109
case 'v' , 'V' :
117
110
settings .Verbose = true
@@ -281,14 +274,16 @@ func main() {
281
274
}
282
275
}
283
276
277
+ files := args
284
278
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
+ }
292
287
}
293
288
294
289
// Copy the loaded keys in the vars unless provided on the command line.
@@ -299,6 +294,15 @@ func main() {
299
294
}
300
295
}
301
296
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
+
302
306
// Dump the settings
303
307
304
308
if settings .Debugging {
@@ -309,52 +313,44 @@ func main() {
309
313
310
314
// Execute each template
311
315
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
317
332
}
318
333
319
334
tmpl , err := template .New (arg ).
320
335
Option (fmt .Sprintf ("missingkey=%s" , settings .MissingKey )).
321
- Funcs (funcs ).
336
+ Funcs (funcmap . Map ).
322
337
Parse (string (file ))
323
338
324
339
if err != nil {
325
340
log .Print (err )
326
341
}
327
342
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
+ }()
332
354
fmt .Println (string (sqlBuffer .Bytes ()))
333
355
}
334
356
}
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
- }
0 commit comments