-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
72 lines (62 loc) · 1.63 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
package main
import (
"bytes"
"flag"
"fmt"
"log"
"os"
"text/template"
"github.com/nathanjcochran/mock/iface"
"golang.org/x/tools/imports"
)
func main() {
var (
dir = flag.String("d", ".", "Directory to search for interface in")
outFile = flag.String("o", "", "Output file (default stdout)")
)
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [options] interface\n", os.Args[0])
fmt.Fprintf(flag.CommandLine.Output(), "Options:\n")
flag.PrintDefaults()
}
flag.Parse()
// First argument is interface name
args := flag.Args()
if len(args) < 1 {
log.Fatal("Not enough args")
}
ifaceName := args[0]
// Parse the package and get info about the interface
iface, err := iface.GetInterface(*dir, ifaceName)
if err != nil {
log.Fatalf("Error getting interface information: %s", err)
}
// Parse the template
tmpl, err := template.New("default").Parse(tmpl)
if err != nil {
log.Fatalf("Error parsing template: %s", err)
}
// Execute/output the template
buf := &bytes.Buffer{}
if err := tmpl.Execute(buf, &iface); err != nil {
log.Fatalf("Error executing template: %s", err)
}
// Format it with go imports
formatted, err := imports.Process(*outFile, buf.Bytes(), nil)
if err != nil {
log.Fatalf("Error formating output: %s", err)
}
// Open the file, if provided, or use stdout
out := os.Stdout
if *outFile != "" {
out, err = os.Create(*outFile)
if err != nil {
log.Fatalf("Error creating output file: %s", err)
}
defer out.Close()
}
// Write the formatted output to the file
if _, err := out.Write(formatted); err != nil {
log.Fatalf("Error writing to file: %s", err)
}
}