-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
109 lines (87 loc) · 1.99 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
package main // import "github.com/percolate/charlatan"
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
type stringSliceValue []string
func (s *stringSliceValue) Set(value string) error {
*s = append(*s, value)
return nil
}
func (s *stringSliceValue) String() string {
return strings.Join(*s, ", ")
}
func (s *stringSliceValue) Get() interface{} {
return []string(*s)
}
const (
usageFormat = `charlatan
https://github.com/percolate/charlatan
Usage:
charlatan [options] <interface> ...
charlatan -h | --help
Options:
`
)
var (
outputPath = flag.String("output", "", "output file path [default: ./charlatan.go]")
outputPackage = flag.String("package", "", "output package name [default: \"<current package>\"]")
dirName = flag.String("dir", "", "input package directory [default: current package directory]")
)
func init() {
log.SetFlags(0)
log.SetPrefix("charlatan: ")
flag.Usage = usage
}
func usage() {
fmt.Fprintf(os.Stderr, usageFormat)
flag.PrintDefaults()
}
func main() {
flag.Parse()
if flag.NArg() == 0 {
log.Print("interface parameters are required")
flag.Usage()
os.Exit(1)
}
if *outputPath != "" && !strings.HasSuffix(*outputPath, ".go") {
log.Print("output path must be a Go source file name")
flag.Usage()
os.Exit(1)
}
packageDirectory := "."
if *dirName != "" {
packageDirectory = *dirName
}
g, err := LoadPackageDir(packageDirectory)
if err != nil {
log.Fatal(err)
}
g.PackageOverride = *outputPackage
src, err := g.Generate(flag.Args())
if err != nil {
log.Print(err)
}
if src == nil {
os.Exit(1)
}
if *outputPath == "" {
*outputPath = "charlatan.go"
}
if err := os.MkdirAll(filepath.Dir(*outputPath), 0755); err != nil {
log.Fatalf("error writing output: %s", err)
}
if err := ioutil.WriteFile(*outputPath, src, 0644); err != nil {
log.Fatalf("error writing output: %s", err)
}
out, err := filepath.Abs(*outputPath)
if err != nil {
out = *outputPath
}
log.Printf("wrote %s\n", out)
}