-
Notifications
You must be signed in to change notification settings - Fork 2
/
executor.go
124 lines (106 loc) · 2.98 KB
/
executor.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
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"text/template"
)
type Executor struct {
conf *Configuration
runner CommandRunner
varReader VariableReader
}
type CommandRunner interface {
Run(command []string) error
}
type DefaultRunner struct {
}
type VariableReader interface {
Read(variableName string) string
}
type DefaultVariableReader struct {
}
func NewDefaultExecutor(conf *Configuration) *Executor {
runner := &DefaultRunner{}
varReader := &DefaultVariableReader{}
return NewExecutor(conf, runner, varReader)
}
func NewExecutor(conf *Configuration, runner CommandRunner, varReader VariableReader) *Executor {
executor := &Executor{
conf: conf,
runner: runner,
varReader: varReader,
}
return executor
}
func (executor *Executor) RunRequest(requestName string, args []string) error {
request := executor.conf.Requests[requestName]
if request != nil {
return executor.runExecutable(request, args)
}
endpoint := executor.conf.Endpoints[requestName]
if endpoint != nil {
if r, err := executor.createTemporaryRequest(requestName); err == nil {
return executor.runExecutable(r, args)
} else {
return err
}
}
return errors.New(fmt.Sprint("Could not find request/endpoint ", requestName))
}
func (executor *Executor) createTemporaryRequest(requestName string) (*Request, error) {
m := make(map[interface{}]interface{}, 1)
m["endpoint"] = requestName
return executor.conf.createRequest(requestName, m)
}
func (executor *Executor) runExecutable(executable Executable, args []string) error {
t := template.Must(template.New("curlTemplate").Parse(runCurlTemplate))
buf := new(bytes.Buffer)
t.Execute(buf, executable)
requestAsString := buf.String()
if !executor.hasResolvedAllVariables(requestAsString) {
re := regexp.MustCompile("{(.+?)}")
for i, v := range re.FindAllString(requestAsString, -1) {
value := executor.getValue(v, i, args)
requestAsString = strings.Replace(requestAsString, v, value, -1)
}
}
asArray := strings.Split(requestAsString, "\n")
return executor.runner.Run(asArray)
}
func (executor *Executor) getValue(variableName string, position int, args []string) string {
if len(args) > position {
return args[position]
}
return executor.varReader.Read(variableName)
}
func (parameterReader *DefaultVariableReader) Read(variableName string) string {
fmt.Printf("Enter %v: ", variableName)
reader := bufio.NewReader(os.Stdin)
value, _ := reader.ReadString('\n')
return strings.TrimSpace(value)
}
func (runner *DefaultRunner) Run(command []string) error {
cmd := exec.Command("curl", command...)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return err
}
fmt.Println(out.String())
if stderr.Len() > 0 {
fmt.Println("#### Stderr ####")
fmt.Println(stderr.String())
}
return nil
}
func (executor *Executor) hasResolvedAllVariables(request string) bool {
return strings.Index(request, "{") == -1
}