-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (57 loc) · 1.77 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"os"
)
type ttyError struct{}
func (e ttyError) Error() string {
return ""
}
func main() {
usage := `envoke fills templates from environment variables.
Usage:
envoke template.go.src Print template output, using environment variables as template context.
envoke '<<' '>>' template.go.src Print template output, using custom delimiters.
envoke -f template.go.src Print template output, without failing for missing environment variables. Default with "".
envoke Print template output, using a template from stdin.
envoke '<<' '>>' - Print template output, using a template from stdin, with custom delimiters.
Options:
-f Do not fail on missing environment variables.
-h, --help Print this help message.
`
f := flag.Bool("f", false, "Do not fail on missing environment variables.")
h := flag.Bool("h", false, "Print help.")
flag.Parse()
if *h {
fmt.Println(usage)
return
}
for _, a := range flag.Args() {
if a == "" {
log.Fatal("Do not put empty arguments.")
}
}
var conf config
if flag.NArg() == 3 {
// Envoke template (or stdin) with custom delimiters
conf = newConfig(flag.Arg(0), flag.Arg(1), flag.Arg(2), *f)
} else if flag.NArg() == 2 {
log.Fatal("Only 1 or 3 args, nothing in between.")
} else if flag.NArg() == 1 {
// Envoke template file (or stdin) with default delimiters
conf = newConfig("", "", flag.Arg(0), *f)
} else if flag.NArg() == 0 {
// Envoke stdin with default delimiters
conf = newConfig("", "", "-", *f)
}
err := conf.envoke(getEnvironment(os.Environ()))
switch err := err.(type) {
case ttyError:
fmt.Println(usage)
return
default:
log.Fatal(err)
}
}