-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
177 lines (161 loc) · 3.84 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"fmt"
"github.com/urfave/cli"
"os"
"runtime"
"strconv"
"time"
)
type Endpoint struct {
Name string
Url string
Path string
QueryRaw string
QueryList map[string]string
QueryListKeys []string
Method string
Headers map[string]bool
Options map[string]bool
Parameters map[string]interface{}
}
type Request struct {
Name string
Url string
Path string
QueryRaw string
QueryList map[string]string
QueryListKeys []string
Method string
Headers map[string]bool
Options map[string]bool
Parameters map[interface{}]interface{}
}
type Executable interface {
GetName() string
GetOptions() map[string]bool
}
var version string
var commit string
var buildDate string
func main() {
app := cli.NewApp()
app.Version = version
cli.VersionPrinter = func(c *cli.Context) {
fmt.Println("Version: " + c.App.Version)
fmt.Println("Git commit: " + commit)
if i, err := strconv.ParseInt(buildDate, 10, 64); err == nil {
fmt.Println("Build date: " + time.Unix(i, 0).UTC().String())
}
fmt.Println("Go version: " + runtime.Version())
}
var file string
var directory string
var oneLine bool
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "f",
Usage: "Load one yaml file",
Destination: &file,
},
cli.StringFlag{
Name: "d",
Usage: "Specify directory to load the yaml files",
Destination: &directory,
Value: ".",
},
cli.BoolFlag{
Name: "oneline, ol",
Usage: "Print commands in one line",
Destination: &oneLine,
},
}
app.Commands = []cli.Command{
{
Name: "requests",
ShortName: "r",
Action: func(c *cli.Context) error {
conf, err := NewConfiguration(NewDefaultConfigurationReader(directory, file))
if err != nil {
return err
}
printer := &Printer{conf: conf, writer: os.Stdout, oneLine: oneLine}
printer.ShowRequests()
return nil
},
},
{
Name: "endpoints",
ShortName: "e",
Action: func(c *cli.Context) error {
conf, err := NewConfiguration(NewDefaultConfigurationReader(directory, file))
if err != nil {
return err
}
printer := &Printer{conf: conf, writer: os.Stdout, oneLine: oneLine}
printer.ShowEndpoints()
return nil
},
},
{
Name: "show",
Action: func(c *cli.Context) error {
conf, err := NewConfiguration(NewDefaultConfigurationReader(directory, file))
if err != nil {
return err
}
printer := &Printer{conf: conf, writer: os.Stdout, oneLine: oneLine}
printer.ShowRequestOrEndpoint(c.Args().First())
return nil
},
},
{
Name: "run",
Action: func(c *cli.Context) error {
conf, err := NewConfiguration(NewDefaultConfigurationReader(directory, file))
if err != nil {
return err
}
executor := NewDefaultExecutor(conf)
requestName := c.Args().First()
return executor.RunRequest(requestName, c.Args().Tail())
},
},
}
app.Run(os.Args)
}
func (endpoint *Endpoint) GetName() string {
return endpoint.Name
}
func (request *Request) GetName() string {
return request.Name
}
func (endpoint *Endpoint) GetOptions() map[string]bool {
return endpoint.Options
}
func (request *Request) GetOptions() map[string]bool {
return request.Options
}
func (request *Request) String() string {
return fmt.Sprintf("%v %v %v %v QueryRaw=%v QueryList=%v Headers=%v Options=%v Param=%v", request.Name,
request.Method,
request.Url,
request.Path,
request.QueryRaw,
request.QueryList,
len(request.Headers),
len(request.Options),
len(request.Parameters),
)
}
func (endpoint *Endpoint) String() string {
return fmt.Sprintf("%v %v %v %v QueryRaw=%v QueryList=%v Headers=%v Options=%v", endpoint.Name,
endpoint.Method,
endpoint.Url,
endpoint.Path,
endpoint.QueryRaw,
endpoint.QueryList,
len(endpoint.Headers),
len(endpoint.Options),
)
}