forked from matryer/codeform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
146 lines (142 loc) · 3.83 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
"github.com/matryer/codeform/internal/version"
"github.com/matryer/codeform/source"
"github.com/matryer/codeform/tool"
"github.com/pkg/errors"
)
func main() {
var fatalErr error
defer func() {
if fatalErr != nil {
io.WriteString(os.Stderr, "codeform: ")
io.WriteString(os.Stderr, fatalErr.Error()+"\n")
os.Exit(1)
}
}()
var (
src = flag.String("src", ".", "code source")
srcin = flag.Bool("srcin", false, "take source from standard in")
templatesrc = flag.String("templatesrc", "", "template source")
template = flag.String("template", "", "template verbatim")
targetPackage = flag.String("pkg", "", "target package")
names = flag.String("name", "", "comma separated list of things to include")
packages = flag.String("package", "", "comma separated list of packages to include")
interfaces = flag.String("interface", "", "comma separated list of interfaces to include")
structs = flag.String("struct", "", "comma separated list of structs to include")
funcs = flag.String("func", "", "comma separated list of funcs to include")
out = flag.String("out", "", "output file (defaults to standard out)")
timeout = flag.Duration("timeout", 2*time.Second, "timeout for HTTP requests")
verbose = flag.Bool("v", false, "verbose logging")
nolinefeed = flag.Bool("n", false, "suppress final linefeed")
printVersion = flag.Bool("version", false, "print version and exit")
)
flag.Usage = func() {
fmt.Println("codeform", "v"+version.Current)
flag.PrintDefaults()
}
flag.Parse()
if *printVersion {
fmt.Println(version.Current)
return
}
log := func(args ...interface{}) {}
logf := func(format string, args ...interface{}) {
log(fmt.Sprintf(format, args...))
}
if *verbose {
log = func(args ...interface{}) {
fmt.Println(args...)
}
}
var err error
sourceLookup := source.Lookup{Client: http.Client{Timeout: *timeout}}
if len(*src) == 0 && !*srcin {
fatalErr = errors.New("provide src")
return
}
logf("lookup: %s", *src)
var codeSource *source.Source
if *srcin {
if len(*src) > 0 {
logf("srcin: ignoring src")
}
codeSource = source.Reader("stdin", os.Stdin)
} else {
codeSource, err = sourceLookup.Get(*src)
if err != nil {
fatalErr = errors.Wrap(err, "lookup code source")
return
}
}
defer codeSource.Close()
var templateSource *source.Source
if len(*template)+len(*templatesrc) == 0 {
fatalErr = errors.New("provide templatesrc or template")
return
}
if len(*template) > 0 {
if len(*templatesrc) > 0 {
fatalErr = errors.New("provide templatesrc or template (not both)")
return
}
log("using verbatim template")
templateSource = source.Reader("verbatim-template", strings.NewReader(*template))
} else {
logf("lookup: %s", *templatesrc)
templateSource, err = sourceLookup.Get(*templatesrc)
if err != nil {
fatalErr = errors.Wrap(err, "lookup template source")
return
}
}
defer templateSource.Close()
var buf bytes.Buffer
var w io.Writer
w = &buf
if len(*out) == 0 {
w = os.Stdout
}
job := tool.Job{
Log: log,
Code: codeSource,
Template: templateSource,
Names: splitArgList(*names),
Packages: splitArgList(*packages),
Interfaces: splitArgList(*interfaces),
Structs: splitArgList(*structs),
Funcs: splitArgList(*funcs),
TargetPackage: *targetPackage,
}
err = job.Execute(w)
if err != nil {
fatalErr = err
return
}
if !*nolinefeed {
w.Write([]byte("\n"))
}
if len(*out) > 0 {
// save file
err = ioutil.WriteFile(*out, buf.Bytes(), 0644)
if err != nil {
fatalErr = err
return
}
}
}
func splitArgList(s string) []string {
if len(s) == 0 {
return []string{}
}
return strings.Split(s, ",")
}