forked from dangerous1990/yapi-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
94 lines (88 loc) · 2.13 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
var (
path = flag.String("path", "", "yapi-import.json所在文件夹")
token = flag.String("token", "", "global token,yapi-import.json 没有配置token默认使用全局token")
)
type config struct {
Type string `json:"type"`
Token string `json:"token"`
File string `json:"file"`
Merge string `json:"merge"`
Server string `json:"server"`
}
// main 上传swagger.json到yapi
func main() {
flag.Parse()
if *path == "" {
fmt.Println("path cannot be empty!")
return
}
configPath := *path + "/yapi-import.json"
bytes, err := ioutil.ReadFile(configPath)
if err != nil {
fmt.Printf("read file[%s] failed err=%v", configPath, err)
return
}
var config config
err = json.Unmarshal(bytes, &config)
if err != nil {
fmt.Printf("read file[%s] Unmarshal json failed err=%v content=%s", *path, err, string(bytes))
return
}
fmt.Printf("load config:%s \ncontent: %+v \n", configPath, config)
upload(config)
}
func upload(config config) {
files := strings.Split(config.File, ",")
for _, f := range files {
post(config, f)
}
}
func post(config config, fileName string) {
filePath := *path + "/" + fileName
bytes, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Printf("read file[%s] failed err=%v", filePath, err)
return
}
actualToken := config.Token
if config.Token == "" {
actualToken = *token
}
values := url.Values{
"type": {config.Type},
"json": {string(bytes)},
"merge": {config.Merge},
"token": {actualToken},
}
client := &http.Client{}
uri := config.Server + "/api/open/import_data"
req, err := http.NewRequest("POST", uri, strings.NewReader(values.Encode()))
if err != nil {
fmt.Printf("http.NewRequest failed err=%v", err)
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Close = true
resp, err := client.Do(req)
if err != nil {
fmt.Printf("client.Do failed err=%v", err)
return
}
defer resp.Body.Close()
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("read responde failed err=%v", err)
return
}
fmt.Println(string(result))
}