-
Notifications
You must be signed in to change notification settings - Fork 3
/
option.go
71 lines (61 loc) · 1.77 KB
/
option.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
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/sonatard/proto-to-postman/postman"
"golang.org/x/xerrors"
)
type Option struct {
importPaths []string
configName string
baseURL string
headers []*postman.HeaderParam
}
func parseOption() (*Option, []string, error) {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, `
Usage of %s:
%s [OPTIONS] [pb files...]
Options\n`, os.Args[0], os.Args[0])
flag.PrintDefaults()
}
dir, err := os.Getwd()
if err != nil {
return nil, nil, xerrors.Errorf("failed to get current directory: %v", err)
}
configName := flag.String("n", "", `config name`)
protoImportOpt := flag.String("i", dir, `pb files import directory`)
baseURLOpt := flag.String("b", "", `request API Base Path e.g) -b https://example.com/`)
headerOpts := flag.String("h", "", `request headerOpts e.g) -h Content-Type:application/json,XXXX:ABC`)
flag.Parse()
protoImportPaths := strings.Split(*protoImportOpt, ",")
for i := range protoImportPaths {
protoImportPath, err := filepath.Abs(protoImportPaths[i])
if err != nil {
return nil, nil, xerrors.Errorf("failed to get absolute path: %v", err)
}
protoImportPaths[i] = protoImportPath
}
headers := strings.Split(*headerOpts, ",")
postHeaderParams := make([]*postman.HeaderParam, 0, len(headers))
for _, header := range headers {
h := strings.Split(header, ":")
if len(h) != 2 {
return nil, nil, xerrors.New("header format is wrong. HeaderName:HeaderValue")
}
postHeaderParam := &postman.HeaderParam{
Key: h[0],
Value: h[1],
}
postHeaderParams = append(postHeaderParams, postHeaderParam)
}
return &Option{
importPaths: protoImportPaths,
configName: *configName,
baseURL: *baseURLOpt,
headers: postHeaderParams,
}, flag.Args(), nil
}