-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
57 lines (47 loc) · 1.18 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"os"
"github.com/jhump/protoreflect/desc/protoparse"
"github.com/sonatard/proto-to-postman/postman"
"golang.org/x/xerrors"
)
func main() {
opt, paths, err := parseOption()
if err != nil || len(paths) == 0 {
flag.PrintDefaults()
os.Exit(1)
}
w := os.Stdout
if err := run(paths, opt.importPaths, opt.configName, opt.baseURL, opt.headers, w); err != nil {
fmt.Fprintf(os.Stderr, "%+v", err)
os.Exit(1)
}
}
func run(files []string, importPaths []string, configName, baseURL string, headers []*postman.HeaderParam, w io.Writer) error {
p := protoparse.Parser{
ImportPaths: importPaths,
}
fds, err := p.ParseFiles(files...)
if err != nil {
return xerrors.Errorf("Unable to parse pb file: %v \n", err)
}
apiParamBuilder := NewAPIParamsBuilder(baseURL, headers)
apiParams, err := apiParamBuilder.Build(fds)
if err != nil {
return xerrors.Errorf(": %w", err)
}
postman := postman.Build(configName, apiParams)
body, err := json.Marshal(postman)
if err != nil {
return xerrors.Errorf(": %w", err)
}
_, err = fmt.Fprintf(w, "%s\n", body)
if err != nil {
return xerrors.Errorf(": %w", err)
}
return nil
}