/
internals.go
95 lines (84 loc) · 2.33 KB
/
internals.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
package builder
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
)
// isURL returns true if the string appears to be a URL.
func isURL(s string) bool {
return strings.HasPrefix(s, "http://") || strings.HasPrefix(s, "https://")
}
// exportEnv creates an export statement for a shell that contains all of the
// provided environment.
func exportEnv(env []string) string {
if len(env) == 0 {
return ""
}
out := "export"
for _, e := range env {
out += " " + bashQuote(e)
}
return out + "; "
}
// bashQuote escapes the provided string and surrounds it with double quotes.
// TODO: verify that these are all we have to escape.
func bashQuote(env string) string {
out := []rune{'"'}
for _, r := range env {
switch r {
case '$', '\\', '"':
out = append(out, '\\', r)
default:
out = append(out, r)
}
}
out = append(out, '"')
return string(out)
}
// hasEnvName returns true if the provided enviroment contains the named ENV var.
func hasEnvName(env []string, name string) bool {
for _, e := range env {
if strings.HasPrefix(e, name+"=") {
return true
}
}
return false
}
// platformSupports is a short-term function to give users a quality error
// message if a Dockerfile uses a command not supported on the platform.
func platformSupports(command string) error {
if runtime.GOOS != "windows" {
return nil
}
switch command {
case "expose", "user", "stopsignal", "arg":
return fmt.Errorf("The daemon on this platform does not support the command '%s'", command)
}
return nil
}
func handleJSONArgs(args []string, attributes map[string]bool) []string {
if len(args) == 0 {
return []string{}
}
if attributes != nil && attributes["json"] {
return args
}
// literal string command, not an exec array
return []string{strings.Join(args, " ")}
}
// makeAbsolute ensures that the provided path is absolute.
func makeAbsolute(dest, workingDir string) string {
// Twiddle the destination when its a relative path - meaning, make it
// relative to the WORKINGDIR
if !filepath.IsAbs(dest) {
hasSlash := strings.HasSuffix(dest, string(os.PathSeparator)) || strings.HasSuffix(dest, string(os.PathSeparator)+".")
dest = filepath.Join(string(os.PathSeparator), filepath.FromSlash(workingDir), dest)
// Make sure we preserve any trailing slash
if hasSlash {
dest += string(os.PathSeparator)
}
}
return dest
}