-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
161 lines (127 loc) · 3.96 KB
/
handler.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
package main
import (
"errors"
"fmt"
"strings"
"github.com/mitchellh/mapstructure"
)
// Handler is the interface around the individual handler implementations
type Handler interface {
BuildCommand([]string) []string
validate() error
}
// FallbackHandler does not wrap the command and will just execute it as is
type FallbackHandler struct{}
// BuildCommand simply returns the passed in command
func (h *FallbackHandler) BuildCommand(command []string) []string {
return command
}
func (h *FallbackHandler) validate() error {
return nil
}
// DockerRunHandler wraps a command with `docker run`
type DockerRunHandler struct {
Remove bool
Image string
Port string
Volumes []string
}
// BuildCommand performs the actual wrapping of the command
func (h *DockerRunHandler) BuildCommand(command []string) []string {
wrappedCommand := []string{"docker", "run", "-it"}
if h.Remove {
wrappedCommand = append(wrappedCommand, "--rm")
}
if h.Image != "" {
wrappedCommand = append(wrappedCommand, h.Image)
}
if h.Port != "" {
wrappedCommand = append(wrappedCommand, "-p ", h.Port)
}
for _, v := range h.Volumes {
wrappedCommand = append(wrappedCommand, "-v ", v)
}
return append(wrappedCommand, command...)
}
func (h *DockerRunHandler) validate() error {
if h.Image == "" {
return errors.New("field image required but not set")
}
return nil
}
// InitDockerRunHandler generates a DockerRunHandler
func InitDockerRunHandler(settings map[string]interface{}) (Handler, error) {
handler := &DockerRunHandler{}
parsingMetadata := &mapstructure.Metadata{}
if err := mapstructure.DecodeMetadata(settings, handler, parsingMetadata); err != nil {
return handler, err
}
return handler, validateHandler(handler, parsingMetadata)
}
// ComposeRunHandler wraps a command with `docker-compose exec`
type ComposeRunHandler struct {
Remove bool
Service string
}
// BuildCommand performs the actual wrapping of the command
func (h *ComposeRunHandler) BuildCommand(command []string) []string {
wrappedCommand := []string{"docker-compose", "run"}
if h.Remove {
wrappedCommand = append(wrappedCommand, "--rm")
}
if h.Service != "" {
wrappedCommand = append(wrappedCommand, h.Service)
}
return append(wrappedCommand, command...)
}
func (h *ComposeRunHandler) validate() error {
if h.Service == "" {
return errors.New("field service required but not set")
}
return nil
}
// InitComposeRunHandler generates a ComposeRunHandler
func InitComposeRunHandler(settings map[string]interface{}) (Handler, error) {
handler := &ComposeRunHandler{}
parsingMetadata := &mapstructure.Metadata{}
if err := mapstructure.DecodeMetadata(settings, handler, parsingMetadata); err != nil {
return handler, err
}
return handler, validateHandler(handler, parsingMetadata)
}
// ComposeExecHandler wraps a command with `docker-compose run`
type ComposeExecHandler struct {
Service string
}
// BuildCommand performs the actual wrapping of the command
func (h *ComposeExecHandler) BuildCommand(command []string) []string {
wrappedCommand := []string{"docker-compose", "exec"}
if h.Service != "" {
wrappedCommand = append(wrappedCommand, h.Service)
}
return append(wrappedCommand, command...)
}
func (h *ComposeExecHandler) validate() error {
if h.Service == "" {
return errors.New("field service required but not set")
}
return nil
}
// InitComposeExecHandler generates a ComposeExecHandler
func InitComposeExecHandler(settings map[string]interface{}) (Handler, error) {
handler := &ComposeExecHandler{}
parsingMetadata := &mapstructure.Metadata{}
if err := mapstructure.DecodeMetadata(settings, handler, parsingMetadata); err != nil {
return handler, err
}
return handler, validateHandler(handler, parsingMetadata)
}
func validateHandler(handler Handler, metadata *mapstructure.Metadata) error {
if err := handler.validate(); err != nil {
return err
}
if len(metadata.Unused) > 0 {
return fmt.Errorf("additional field(s) detected: %v", strings.Join(metadata.Unused, ", "))
}
return nil
}