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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
package main
import (
"bytes"
"encoding/json"
"errors"
"flag"
"fmt"
"go/format"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
)
// Extension is the required file extension for processed files.
const Extension = ".tmpl"
func main() {
m := NewMain()
if err := m.ParseFlags(os.Args[1:]); err != nil {
fmt.Fprintln(m.Stderr, err)
os.Exit(2)
}
if err := m.Run(); err != nil {
fmt.Fprintln(m.Stderr, err)
os.Exit(1)
}
}
type Main struct {
// Files to be processed.
Paths []string
// Data to be applied to the files during generation.
Data interface{}
OS interface {
Stat(filename string) (os.FileInfo, error)
}
FileReadWriter interface {
ReadFile(filename string) ([]byte, error)
WriteFile(filename string, data []byte, perm os.FileMode) error
}
// Standard input/output
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
}
// NewMain returns a new instance of Main.
func NewMain() *Main {
return &Main{
OS: &mainOS{},
FileReadWriter: &fileReadWriter{},
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
}
// ParseFlags parses the command line flags from args.
func (m *Main) ParseFlags(args []string) error {
fs := flag.NewFlagSet("tmp", flag.ContinueOnError)
fs.SetOutput(m.Stderr)
data := fs.String("data", "", "json data")
if err := fs.Parse(args); err != nil {
return err
}
// Parse JSON data.
if *data != "" {
// If the data has a @-prefix then read from a file.
buf := []byte(*data)
if strings.HasPrefix(*data, "@") {
b, err := m.FileReadWriter.ReadFile(strings.TrimPrefix(*data, "@"))
if err != nil {
return err
}
buf = b
}
if err := json.Unmarshal(buf, &m.Data); err != nil {
return err
}
}
// All arguments are considered paths to process.
m.Paths = fs.Args()
return nil
}
// Run executes the program.
func (m *Main) Run() error {
// Verify we have at least one path.
if len(m.Paths) == 0 {
return errors.New("path required")
}
// Process each path.
for _, path := range m.Paths {
if err := m.process(path); err != nil {
return err
}
}
return nil
}
// process reads a template file from path, processes it, and writes it to its generated path.
func (m *Main) process(path string) error {
// Validate that we have a prefix we can strip off for the generated path.
if !strings.HasSuffix(path, Extension) {
return fmt.Errorf("path must have %s extension: %s", Extension, path)
}
outputPath := strings.TrimSuffix(path, Extension)
// Stat the file to retrieve the mode.
fi, err := m.OS.Stat(path)
if os.IsNotExist(err) {
return fmt.Errorf("file not found")
} else if err != nil {
return err
}
// Read in template file.
source, err := m.FileReadWriter.ReadFile(path)
if os.IsNotExist(err) {
return fmt.Errorf("file not found")
} else if err != nil {
return err
}
// Parse file into template.
tmpl, err := template.New("main").Funcs(FuncMap).Parse(string(source))
if err != nil {
return err
}
// Create a comment at the top if generating to a .go file.
var buf bytes.Buffer
switch filepath.Ext(outputPath) {
case ".go":
fmt.Fprintln(&buf, "// Generated by tmpl")
fmt.Fprintln(&buf, "// https://github.com/benbjohnson/tmpl")
fmt.Fprintln(&buf, "//")
fmt.Fprintln(&buf, "// DO NOT EDIT!")
fmt.Fprintln(&buf, "// Source:", path)
fmt.Fprintln(&buf, "")
}
// Execute template.
if err := tmpl.Execute(&buf, m.Data); err != nil {
return err
}
// Format output if it's a Go file.
output := buf.Bytes()
switch filepath.Ext(outputPath) {
case ".go":
formatted, err := format.Source(output)
if err != nil {
return err
}
output = formatted
}
// Write buffer to file.
if err := m.FileReadWriter.WriteFile(outputPath, output, fi.Mode()); err != nil {
return err
}
return nil
}
var FuncMap = template.FuncMap{
"upcase": strings.ToUpper,
"downcase": strings.ToLower,
"camel": camelCase,
}
func camelCase(s string) string {
if s == "" {
return s
}
return strings.ToLower(string(s[0])) + s[1:]
}
// fileReadWriter implements Main.FileReadWriter.
type fileReadWriter struct{}
func (*fileReadWriter) ReadFile(filename string) ([]byte, error) {
return ioutil.ReadFile(filename)
}
func (*fileReadWriter) WriteFile(filename string, data []byte, perm os.FileMode) error {
return ioutil.WriteFile(filename, data, perm)
}
// mainOS implements Main.OS.
type mainOS struct{}
func (*mainOS) Stat(name string) (os.FileInfo, error) { return os.Stat(name) }
|