-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.go
99 lines (77 loc) · 2.09 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
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
// Go template command
package main
/*
Are there any examples of wanting more than one type exported from the
same package? Possibly for functional type utilities.
Could import multiple types from the same package and the builder
would do the right thing.
Path generation for generated files could do with work - args may have
spaces in, may have upper and lower case characters which will fold
together on Windows.
Detect dupliace template definitions so we don't write them multiple times
write some test
manage all the generated files - find them - delete stale ones, etg
Put comment in generated file, generated by gotemplate from xyz on date?
do replacements in comments too?
*/
import (
"flag"
"fmt"
"strings"
"log"
"os"
"path"
)
// Globals
var (
// Flags
verbose = flag.Bool("v", false, "Verbose - print lots of stuff")
outfile = flag.String("outfmt", "gotemplate_%v", "the format of the output file; must contain a single instance of the %v verb\n"+
"\twhich will be replaced with the template instance name")
)
// Logging function
var logf = log.Printf
// Log then fatal error
var fatalf = func(format string, args ...interface{}) {
logf(format, args...)
os.Exit(1)
}
// Log if -v set
func debugf(format string, args ...interface{}) {
if *verbose {
logf(format, args...)
}
}
// usage prints the syntax and exists
func usage() {
BaseName := path.Base(os.Args[0])
fmt.Fprintf(os.Stderr,
"Syntax: %s [flags] package_name parameter\n\n"+
"Flags:\n\n",
BaseName)
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\n")
os.Exit(1)
}
func main() {
log.SetFlags(0)
log.SetPrefix("")
flag.Usage = usage
flag.Parse()
// verify that *outfile contains exactly one occurrence of the %v verb
// and no other occurences of %
if c := strings.Replace(*outfile, "%v", "", 1); c == *outfile ||
strings.Index(c, "%") != -1 {
fatalf("Invalid outfile format")
}
args := flag.Args()
if len(args) != 2 {
fatalf("Need 2 arguments, package and parameters")
}
cwd, err := os.Getwd()
if err != nil {
fatalf("Couldn't get wd: %v", err)
}
t := newTemplate(cwd, args[0], args[1])
t.instantiate()
}