github.com/timestee/gotemplate@v0.0.0-20170505123500-7c22f8f4ddd0/main.go (about) 1 // Go template command 2 package main 3 4 /* 5 Are there any examples of wanting more than one type exported from the 6 same package? Possibly for functional type utilities. 7 8 Could import multiple types from the same package and the builder 9 would do the right thing. 10 11 Path generation for generated files could do with work - args may have 12 spaces in, may have upper and lower case characters which will fold 13 together on Windows. 14 15 Detect dupliace template definitions so we don't write them multiple times 16 17 write some test 18 19 manage all the generated files - find them - delete stale ones, etg 20 21 Put comment in generated file, generated by gotemplate from xyz on date? 22 23 do replacements in comments too? 24 */ 25 26 import ( 27 "flag" 28 "fmt" 29 "strings" 30 31 "log" 32 "os" 33 "path" 34 ) 35 36 // Globals 37 var ( 38 // Flags 39 verbose = flag.Bool("v", false, "Verbose - print lots of stuff") 40 outfile = flag.String("outfmt", "gotemplate_%v", "the format of the output file; must contain a single instance of the %v verb\n"+ 41 "\twhich will be replaced with the template instance name") 42 ) 43 44 // Logging function 45 var logf = log.Printf 46 47 // Log then fatal error 48 var fatalf = func(format string, args ...interface{}) { 49 logf(format, args...) 50 os.Exit(1) 51 } 52 53 // Log if -v set 54 func debugf(format string, args ...interface{}) { 55 if *verbose { 56 logf(format, args...) 57 } 58 } 59 60 // usage prints the syntax and exists 61 func usage() { 62 BaseName := path.Base(os.Args[0]) 63 fmt.Fprintf(os.Stderr, 64 "Syntax: %s [flags] package_name parameter\n\n"+ 65 "Flags:\n\n", 66 BaseName) 67 flag.PrintDefaults() 68 fmt.Fprintf(os.Stderr, "\n") 69 os.Exit(1) 70 } 71 72 func main() { 73 log.SetFlags(0) 74 log.SetPrefix("") 75 76 flag.Usage = usage 77 flag.Parse() 78 79 // verify that *outfile contains exactly one occurrence of the %v verb 80 // and no other occurences of % 81 if c := strings.Replace(*outfile, "%v", "", 1); c == *outfile || 82 strings.Index(c, "%") != -1 { 83 84 fatalf("Invalid outfile format") 85 } 86 87 args := flag.Args() 88 if len(args) != 2 { 89 fatalf("Need 2 arguments, package and parameters") 90 } 91 92 cwd, err := os.Getwd() 93 if err != nil { 94 fatalf("Couldn't get wd: %v", err) 95 } 96 97 t := newTemplate(cwd, args[0], args[1]) 98 t.instantiate() 99 }