Initial commit

This commit is contained in:
Ria Bhatia
2017-12-04 13:32:57 -06:00
committed by Erik St. Martin
commit 0075e5b0f3
9056 changed files with 2523100 additions and 0 deletions

94
vendor/github.com/hyperhq/libcompose/labels/labels.go generated vendored Normal file
View File

@@ -0,0 +1,94 @@
package labels
import (
"encoding/json"
"fmt"
"github.com/hyperhq/libcompose/utils"
)
// Label represents a docker label.
type Label string
// Libcompose default labels.
const (
NUMBER = Label("sh.hyper.compose.container-number")
ONEOFF = Label("sh.hyper.compose.oneoff")
PROJECT = Label("sh.hyper.compose.project")
SERVICE = Label("sh.hyper.compose.service")
HASH = Label("sh.hyper.compose.config-hash")
VERSION = Label("sh.hyper.compose.version")
)
// EqString returns a label json string representation with the specified value.
func (f Label) EqString(value string) string {
return LabelFilterString(string(f), value)
}
// Eq returns a label map representation with the specified value.
func (f Label) Eq(value string) map[string][]string {
return LabelFilter(string(f), value)
}
// AndString returns a json list of labels by merging the two specified values (left and right) serialized as string.
func AndString(left, right string) string {
leftMap := map[string][]string{}
rightMap := map[string][]string{}
// Ignore errors
json.Unmarshal([]byte(left), &leftMap)
json.Unmarshal([]byte(right), &rightMap)
for k, v := range rightMap {
existing, ok := leftMap[k]
if ok {
leftMap[k] = append(existing, v...)
} else {
leftMap[k] = v
}
}
result, _ := json.Marshal(leftMap)
return string(result)
}
// And returns a map of labels by merging the two specified values (left and right).
func And(left, right map[string][]string) map[string][]string {
result := map[string][]string{}
for k, v := range left {
result[k] = v
}
for k, v := range right {
existing, ok := result[k]
if ok {
result[k] = append(existing, v...)
} else {
result[k] = v
}
}
return result
}
// Str returns the label name.
func (f Label) Str() string {
return string(f)
}
// LabelFilterString returns a label json string representation of the specifed couple (key,value)
// that is used as filter for docker.
func LabelFilterString(key, value string) string {
return utils.FilterString(map[string][]string{
"label": {fmt.Sprintf("%s=%s", key, value)},
})
}
// LabelFilter returns a label map representation of the specifed couple (key,value)
// that is used as filter for docker.
func LabelFilter(key, value string) map[string][]string {
return map[string][]string{
"label": {fmt.Sprintf("%s=%s", key, value)},
}
}

View File

@@ -0,0 +1,50 @@
package labels
import (
"testing"
)
func TestLabelEq(t *testing.T) {
label := Label("labelName")
m := label.Eq("value")
values, ok := m["label"]
if !ok {
t.Fatalf("expected a label key, got %v", m)
}
if len(values) != 1 {
t.Fatalf("expected only one value, got %v", values)
}
if values[0] != "labelName=value" {
t.Fatalf("expected 'labelName=value', got %s", values)
}
}
func TestLabelEqString(t *testing.T) {
label := Label("labelName")
value := label.EqString("value")
if value != `{"label":["labelName=value"]}` {
t.Fatalf("expected '{labelName=value}', got %s", value)
}
}
func TestLabelFilter(t *testing.T) {
filters := []struct {
key string
value string
expected string
}{
{
"key", "value", `{"label":["key=value"]}`,
}, {
"key", "", `{"label":["key="]}`,
}, {
"", "", `{"label":["="]}`,
},
}
for _, filter := range filters {
actual := LabelFilterString(filter.key, filter.value)
if actual != filter.expected {
t.Fatalf("Expected '%s for key=%s and value=%s, got %s", filter.expected, filter.key, filter.value, actual)
}
}
}