-
Notifications
You must be signed in to change notification settings - Fork 50
/
main.go
123 lines (117 loc) · 2.97 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
121
122
123
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/dustin/go-humanize"
"github.com/pacedotdev/oto/parser"
"github.com/pacedotdev/oto/render"
"github.com/pkg/errors"
)
// Version is set during build.
var Version = "dev"
func main() {
if err := run(os.Stdout, os.Args); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
func run(stdout io.Writer, args []string) error {
flags := flag.NewFlagSet(args[0], flag.ContinueOnError)
flags.Usage = func() {
fmt.Println(args[0] + " " + Version + ` usage:
oto [flags] paths [[path2] [path3]...]`)
fmt.Println(`
flags:`)
flags.PrintDefaults()
}
var (
template = flags.String("template", "", "plush template to render")
outfile = flags.String("out", "", "output file (default: stdout)")
pkg = flags.String("pkg", "", "explicit package name (default: inferred)")
v = flags.Bool("v", false, "verbose output")
paramsStr = flags.String("params", "", "list of parameters in the format: \"key:value,key:value\"")
ignoreList = flags.String("ignore", "", "comma separated list of interfaces to ignore")
)
if err := flags.Parse(args[1:]); err != nil {
return err
}
if *template == "" {
flags.PrintDefaults()
return errors.New("missing template")
}
params, err := parseParams(*paramsStr)
if err != nil {
flags.PrintDefaults()
return errors.Wrap(err, "params")
}
p := parser.New(flags.Args()...)
ignoreItems := strings.Split(*ignoreList, ",")
if ignoreItems[0] != "" {
p.ExcludeInterfaces = ignoreItems
}
p.Verbose = *v
if p.Verbose {
fmt.Println("oto - github.com/pacedotdev/oto", Version)
}
def, err := p.Parse()
if err != nil {
return err
}
if *pkg != "" {
def.PackageName = *pkg
}
b, err := ioutil.ReadFile(*template)
if err != nil {
return err
}
out, err := render.Render(string(b), def, params)
if err != nil {
return err
}
var w io.Writer = stdout
if *outfile != "" {
f, err := os.Create(*outfile)
if err != nil {
return err
}
defer f.Close()
w = f
}
if _, err := io.WriteString(w, out); err != nil {
return err
}
if p.Verbose {
var methodsCount int
for i := range def.Services {
methodsCount += len(def.Services[i].Methods)
}
fmt.Println()
fmt.Printf("\tTotal services: %d", len(def.Services))
fmt.Printf("\tTotal Methods: %d", methodsCount)
fmt.Printf("\tTotal Objects: %d\n", len(def.Objects))
fmt.Printf("\tOutput size: %s\n", humanize.Bytes(uint64(len(out))))
}
return nil
}
// parseParams returns a map of data parsed from the params string.
func parseParams(s string) (map[string]interface{}, error) {
params := make(map[string]interface{})
if s == "" {
// empty map for an empty string
return params, nil
}
pairs := strings.Split(s, ",")
for i := range pairs {
pair := strings.TrimSpace(pairs[i])
segs := strings.Split(pair, ":")
if len(segs) != 2 {
return nil, errors.New("malformed params")
}
params[strings.TrimSpace(segs[0])] = strings.TrimSpace(segs[1])
}
return params, nil
}