Debian Codegraph @

internals.go

raw
1 package imagebuilder
2
3 import (
4 "flag"
5 "fmt"
6 "os"
7 "path/filepath"
8 "runtime"
9 "strings"
10 "time"
11 )
12
13 // hasEnvName returns true if the provided environment contains the named ENV var.
14 func hasEnvName(env []string, name string) bool {
15 for _, e := range env {
16 if strings.HasPrefix(e, name+"=") {
17 return true
18 }
19 }
20 return false
21 }
22
23 // platformSupports is a short-term function to give users a quality error
24 // message if a Dockerfile uses a command not supported on the platform.
25 func platformSupports(command string) error {
26 if runtime.GOOS != "windows" {
27 return nil
28 }
29 switch command {
30 case "expose", "user", "stopsignal", "arg":
31 return fmt.Errorf("The daemon on this platform does not support the command '%s'", command)
32 }
33 return nil
34 }
35
36 func handleJSONArgs(args []string, attributes map[string]bool) []string {
37 if len(args) == 0 {
38 return []string{}
39 }
40
41 if attributes != nil && attributes["json"] {
42 return args
43 }
44
45 // literal string command, not an exec array
46 return []string{strings.Join(args, " ")}
47 }
48
49 func hasSlash(input string) bool {
50 return strings.HasSuffix(input, string(os.PathSeparator)) || strings.HasSuffix(input, string(os.PathSeparator)+".")
51 }
52
53 // makeAbsolute ensures that the provided path is absolute.
54 func makeAbsolute(dest, workingDir string) string {
55 // Twiddle the destination when it's a relative path - meaning, make it
56 // relative to the WORKINGDIR
57 if dest == "." {
58 if !hasSlash(workingDir) {
59 workingDir += string(os.PathSeparator)
60 }
61 dest = workingDir
62 }
63
64 if !filepath.IsAbs(dest) {
65 hasSlash := hasSlash(dest)
66 dest = filepath.Join(string(os.PathSeparator), filepath.FromSlash(workingDir), dest)
67
68 // Make sure we preserve any trailing slash
69 if hasSlash {
70 dest += string(os.PathSeparator)
71 }
72 }
73 return dest
74 }
75
76 // parseOptInterval(flag) is the duration of flag.Value, or 0 if
77 // empty. An error is reported if the value is given and is not positive.
78 func parseOptInterval(f *flag.Flag) (time.Duration, error) {
79 if f == nil {
80 return 0, fmt.Errorf("No flag defined")
81 }
82 s := f.Value.String()
83 if s == "" {
84 return 0, nil
85 }
86 d, err := time.ParseDuration(s)
87 if err != nil {
88 return 0, err
89 }
90 if d <= 0 {
91 return 0, fmt.Errorf("Interval %#v must be positive", f.Name)
92 }
93 return d, nil
94 }
95
96 // mergeEnv merges two lists of environment variables, avoiding duplicates.
97 func mergeEnv(defaults, overrides []string) []string {
98 s := make([]string, 0, len(defaults)+len(overrides))
99 index := make(map[string]int)
100 for _, envSpec := range append(defaults, overrides...) {
101 envVar := strings.SplitN(envSpec, "=", 2)
102 if i, ok := index[envVar[0]]; ok {
103 s[i] = envSpec
104 continue
105 }
106 s = append(s, envSpec)
107 index[envVar[0]] = len(s) - 1
108 }
109 return s
110 }
111
112 // envMapAsSlice returns the contents of a map[string]string as a slice of keys
113 // and values joined with "=".
114 func envMapAsSlice(m map[string]string) []string {
115 s := make([]string, 0, len(m))
116 for k, v := range m {
117 s = append(s, k+"="+v)
118 }
119 return s
120 }