-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
120 lines (94 loc) · 2.96 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"bytes"
"fmt"
"go/format"
"go/token"
"log"
"os"
"path/filepath"
"strings"
"github.com/pivotal-cf/jhanda"
"github.com/ryanmoran/faux/parsing"
"github.com/ryanmoran/faux/rendering"
"golang.org/x/tools/imports"
)
var version = "unknown"
func main() {
var options struct {
Help bool `long:"help" short:"h" description:"prints the usage"`
Version bool `long:"version" short:"v" description:"prints the version"`
Package string `long:"package" short:"p" description:"the name of the package that contains the interface"`
File string `long:"file" short:"f" env:"GOFILE" description:"the name of the file to parse"`
Output string `long:"output" short:"o" description:"the name of the file to write"`
Interface string `long:"interface" short:"i" description:"the name of the interface to fake"`
Name string `long:"name" short:"n" description:"the name to give the generated type"`
}
stdout := log.New(os.Stdout, "", 0)
stderr := log.New(os.Stderr, "", 0)
_, err := jhanda.Parse(&options, os.Args[1:])
if err != nil {
stderr.Fatal(err)
}
if options.Help {
flags, err := jhanda.PrintUsage(options)
if err != nil {
stderr.Fatal(err)
}
flags = strings.Join(strings.Split(flags, "\n"), "\n ")
stdout.Printf(`faux helps you generate fakes
Usage: faux --file <FILE> --output <FILE> --interface <INTERFACE-TO-FAKE> [--help]
Flags:
%s
`, flags)
os.Exit(0)
}
if options.Version {
stdout.Print(version)
os.Exit(0)
}
if options.Package == "" {
path, err := filepath.Abs(options.File)
if err != nil {
stderr.Fatal(err)
}
options.Package = filepath.Dir(path)
}
fmt.Fprintf(os.Stderr, "Generating fake %s at %s...", options.Interface, options.Output)
fake, err := parsing.Parse(options.Package, options.Interface)
if err != nil {
stderr.Fatal(err)
}
if options.Name != "" {
fake.Interface.Name = options.Name
}
err = os.MkdirAll(filepath.Dir(options.Output), 0755)
if err != nil {
stderr.Fatalf("could not create directory: %s", err)
}
output, err := os.OpenFile(options.Output, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
stderr.Fatalf("could not create output file: %s", err)
}
defer output.Close()
buffer := bytes.NewBuffer([]byte{})
context := rendering.NewContext()
tree := context.Build(fake).AST()
err = format.Node(buffer, token.NewFileSet(), tree)
if err != nil {
stderr.Fatalf("could not format fake ast: %s", err)
}
outputPath, err := filepath.Abs(output.Name())
if err != nil {
stderr.Fatalf("could not determine output absolute path: %s", err)
}
result, err := imports.Process(outputPath, buffer.Bytes(), &imports.Options{FormatOnly: false})
if err != nil {
stderr.Fatalf("could not process imports: %s\n\n%s", err, buffer.String())
}
_, err = output.Write(result)
if err != nil {
stderr.Fatalf("could not write output file: %s", err)
}
fmt.Fprintln(os.Stderr, "done!")
}