-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
101 lines (86 loc) · 2.69 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
100
101
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/stanley-robotics/go-fields-updater/models"
"github.com/stanley-robotics/go-fields-updater/utils"
)
var (
typesArg = flag.String("type", "", "comma-separated list of type/structures names; must be set")
outputArg = flag.String("output", "", "output file name; default srcdir/<type>_updater.go")
)
// Usage is a replacement usage function for the flags package.
func Usage() {
_, _ = fmt.Fprintf(os.Stderr, "Go-Fields-Updater is a tool to generate Go code that adds useful method to update specific fields of specific type.\n")
_, _ = fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
_, _ = fmt.Fprintf(os.Stderr, "\tgo-fields-updater [flags] -type T [directory]\n")
_, _ = fmt.Fprintf(os.Stderr, "\tgo-fields-updater [flags] -type T files... # Must be a single package\n")
_, _ = fmt.Fprintf(os.Stderr, "For more information, see:\n")
_, _ = fmt.Fprintf(os.Stderr, "\thttps://github.com/stanley-robotics/go-fields-updater\n")
_, _ = fmt.Fprintf(os.Stderr, "Flags:\n")
flag.PrintDefaults()
}
func main() {
log.SetFlags(0)
log.SetPrefix("go-fields-updater: ")
flag.Usage = Usage
flag.Parse()
if len(*typesArg) == 0 {
flag.Usage()
os.Exit(2)
}
types := strings.Split(*typesArg, ",")
// We accept either one directory or a list of files. Which do we have?
args := flag.Args()
if len(args) == 0 {
// Default: process whole package in current directory.
args = []string{"."}
}
// Parse the package once.
var dir string
var g models.Generator
if len(args) == 1 && utils.IsDir(args[0]) {
dir = args[0]
} else {
dir = filepath.Dir(args[0])
}
pkg := g.ParsePackage(args, []string{})
// Print the header and package clause.
g.Printf("// Code generated by \"go-fields-updater %s\"; DO NOT EDIT.\n", strings.Join(os.Args[1:], " "))
g.Printf("\n")
g.Printf("package %s", pkg.Name())
g.Printf("\n")
// Run generate for each type.
for _, t := range types {
g.Generate(t)
}
// Format the output.
src := g.Format()
// Figure out filename to write to
outputName := *outputArg
if outputName == "" {
baseName := fmt.Sprintf("%s_updater.go", types[0])
outputName = filepath.Join(dir, strings.ToLower(baseName))
}
// Write to tmpfile first
tmpFile, err := os.CreateTemp(dir, fmt.Sprintf("%s_updater_", types[0]))
if err != nil {
log.Fatalf("creating temporary file for output: %s", err)
}
_, err = tmpFile.Write(src)
if err != nil {
tmpFile.Close()
os.Remove(tmpFile.Name())
log.Fatalf("writing output: %s", err)
}
tmpFile.Close()
// Rename tmpfile to output file
err = os.Rename(tmpFile.Name(), outputName)
if err != nil {
log.Fatalf("moving tempfile to output file: %s", err)
}
}