summaryrefslogtreecommitdiff
path: root/build
diff options
context:
space:
mode:
Diffstat (limited to 'build')
-rw-r--r--build/instructions.go39
-rw-r--r--build/instructions_test.go14
2 files changed, 41 insertions, 12 deletions
diff --git a/build/instructions.go b/build/instructions.go
index 38ea322..41c959d 100644
--- a/build/instructions.go
+++ b/build/instructions.go
@@ -64,20 +64,18 @@ type Env struct {
}
func (env Env) Compile() []string {
- defs := make([]string, 0, len(env.Definitions))
- names := make([]string, 0, len(env.Definitions))
-
- for name := range env.Definitions {
- names = append(names, name)
- }
-
- sort.Strings(names)
+ return compileSortedKeyValues(env.Definitions)
+}
- for _, name := range names {
- defs = append(defs, name+"="+quote(env.Definitions[name]))
- }
+// Label represents a number of meta-data key/value pairs
+type Label struct {
+ Definitions map[string]string
+}
- return defs
+// Compile returns the label key/value pairs as a number of `key="value"`
+// strings where the values are properly quoted
+func (label Label) Compile() []string {
+ return compileSortedKeyValues(label.Definitions)
}
type Volume struct {
@@ -88,6 +86,23 @@ func (vol Volume) Compile() []string {
return []string{quote(vol.Path)}
}
+func compileSortedKeyValues(keyValues map[string]string) []string {
+ defs := make([]string, 0, len(keyValues))
+ names := make([]string, 0, len(keyValues))
+
+ for name := range keyValues {
+ names = append(names, name)
+ }
+
+ sort.Strings(names)
+
+ for _, name := range names {
+ defs = append(defs, name+"="+quote(keyValues[name]))
+ }
+
+ return defs
+}
+
func quote(arg string) string {
return strconv.Quote(arg)
}
diff --git a/build/instructions_test.go b/build/instructions_test.go
index acbdb7c..4098711 100644
--- a/build/instructions_test.go
+++ b/build/instructions_test.go
@@ -56,6 +56,20 @@ func TestEnv(t *testing.T) {
}, i.Compile())
}
+func TestLabel(t *testing.T) {
+ i := build.Label{map[string]string{
+ "fooname": "foovalue",
+ "barname": "barvalue",
+ "quxname": "quxvalue",
+ }}
+
+ assert.Equal(t, []string{
+ `barname="barvalue"`,
+ `fooname="foovalue"`,
+ `quxname="quxvalue"`,
+ }, i.Compile())
+}
+
func TestVolume(t *testing.T) {
i := build.Volume{"/foo/dir"}