forked from docker-exec/dexec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dexec.go
163 lines (143 loc) · 4.37 KB
/
dexec.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"regexp"
)
// ExtractFileExtension extracts the extension from a filename. This is defined
// as the remainder of the string after the last '.'.
func ExtractFileExtension(filename string) string {
patternPermission := regexp.MustCompile(`.*\.(.*):.*`)
permissionMatch := patternPermission.FindStringSubmatch(filename)
if len(permissionMatch) > 0 {
return permissionMatch[1]
}
patternFilename := regexp.MustCompile(`.*\.(.*)`)
return patternFilename.FindStringSubmatch(filename)[1]
}
// DexecImage consists of the file extension, Docker image name and Docker
// image version to use for a given Docker Exec image.
type DexecImage struct {
extension string
image string
version string
}
// LookupImageByExtension is a closure storing a dictionary mapping source
// extensions to the names and versions of Docker Exec images.
var LookupImageByExtension = func() func(string) DexecImage {
innerMap := map[string]DexecImage{
"c": {"c", "c", "1.0.0"},
"clj": {"clj", "clojure", "1.0.0"},
"coffee": {"coffee", "coffee", "1.0.0"},
"cpp": {"cpp", "cpp", "1.0.0"},
"cs": {"cs", "csharp", "1.0.0"},
"d": {"d", "d", "1.0.0"},
"erl": {"erl", "erlang", "1.0.0"},
"fs": {"fs", "fsharp", "1.0.0"},
"go": {"go", "go", "1.0.0"},
"groovy": {"groovy", "groovy", "1.0.0"},
"hs": {"hs", "haskell", "1.0.0"},
"java": {"java", "java", "1.0.0"},
"lisp": {"lisp", "lisp", "1.0.0"},
"js": {"js", "node", "1.0.0"},
"m": {"m", "objc", "1.0.0"},
"ml": {"ml", "ocaml", "1.0.0"},
"pl": {"pl", "perl", "1.0.0"},
"php": {"php", "php", "1.0.0"},
"py": {"py", "python", "1.0.0"},
"rkt": {"rkt", "racket", "1.0.0"},
"rb": {"rb", "ruby", "1.0.0"},
"rs": {"rs", "rust", "1.0.0"},
"scala": {"scala", "scala", "1.0.0"},
"sh": {"sh", "bash", "1.0.0"},
}
return func(key string) DexecImage {
return innerMap[key]
}
}()
const dexecPath = "/tmp/dexec/build"
const dexecImageTemplate = "dexec/%s:%s"
const dexecVolumeTemplate = "%s/%s:%s/%s"
// ExtractBasenameAndPermission takes an include string and splits it into
// its file or folder name and the permission string if present or the empty
// string if not.
func ExtractBasenameAndPermission(path string) (string, string) {
pathPattern := regexp.MustCompile("([\\w.-]+)(:(rw|ro))")
match := pathPattern.FindStringSubmatch(path)
basename := path
var permission string
if len(match) == 4 {
basename = match[1]
permission = match[2]
}
return basename, permission
}
// RunDexecContainer runs an anonymouse Docker container with a Docker Exec
// image, mounting the specified sources and includes and passing the
// list of sources and arguments to the entrypoint.
func RunDexecContainer(dexecImage DexecImage, options map[OptionType][]string) {
dockerImage := fmt.Sprintf(dexecImageTemplate, dexecImage.image, dexecImage.version)
path := "."
if len(options[TargetDir]) > 0 {
path = options[TargetDir][0]
}
absPath, _ := filepath.Abs(path)
var dockerArgs []string
for _, source := range append(options[Source], options[Include]...) {
basename, _ := ExtractBasenameAndPermission(source)
dockerArgs = append(
dockerArgs,
[]string{
"-v",
fmt.Sprintf(dexecVolumeTemplate, absPath, basename, dexecPath, source),
}...,
)
}
var sourceBasenames []string
for _, source := range options[Source] {
basename, _ := ExtractBasenameAndPermission(source)
sourceBasenames = append(sourceBasenames, []string{basename}...)
}
entrypointArgs := JoinStringSlices(
sourceBasenames,
AddPrefix(options[BuildArg], "-b"),
AddPrefix(options[Arg], "-a"),
)
if len(options[UpdateFlag]) > 0 {
DockerPull(dockerImage)
}
RunAnonymousContainer(
dockerImage,
dockerArgs,
entrypointArgs,
)
}
func validate(cli CLI) bool {
if !IsDockerPresent() {
log.Fatal("Docker not found")
} else if !IsDockerRunning() {
log.Fatal("Docker not running")
}
valid := false
if len(cli.options[VersionFlag]) != 0 {
DisplayVersion(cli.filename)
} else if len(cli.options[Source]) == 0 ||
len(cli.options[HelpFlag]) != 0 ||
len(cli.options[TargetDir]) > 1 {
DisplayHelp(cli.filename)
} else {
valid = true
}
return valid
}
func main() {
cli := ParseOsArgs(os.Args)
if validate(cli) {
RunDexecContainer(
LookupImageByExtension(ExtractFileExtension(cli.options[Source][0])),
cli.options,
)
}
}