From c4c07c97ac57c7d75cb62b2950d56dd03848441c Mon Sep 17 00:00:00 2001 From: Andrea Luzzardi Date: Thu, 7 May 2015 00:02:17 -0700 Subject: [PATCH] godeps: vendor dependency to docker/docker/pkg/stringid. Signed-off-by: Andrea Luzzardi --- Godeps/Godeps.json | 7 +++- .../docker/docker/pkg/stringid/README.md | 1 + .../docker/docker/pkg/stringid/stringid.go | 38 +++++++++++++++++++ .../docker/pkg/stringid/stringid_test.go | 35 +++++++++++++++++ 4 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 Godeps/_workspace/src/github.com/docker/docker/pkg/stringid/README.md create mode 100644 Godeps/_workspace/src/github.com/docker/docker/pkg/stringid/stringid.go create mode 100644 Godeps/_workspace/src/github.com/docker/docker/pkg/stringid/stringid_test.go diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index d4066de1e0..e516876c98 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -1,6 +1,6 @@ { "ImportPath": "github.com/docker/swarm", - "GoVersion": "go1.3.1", + "GoVersion": "go1.3.3", "Packages": [ "./..." ], @@ -40,6 +40,11 @@ "Comment": "v1.4.1-3245-g443437f", "Rev": "443437f5ea04da9d62bf3e05d7951f7d30e77d96" }, + { + "ImportPath": "github.com/docker/docker/pkg/stringid", + "Comment": "v1.4.1-3245-g443437f", + "Rev": "443437f5ea04da9d62bf3e05d7951f7d30e77d96" + }, { "ImportPath": "github.com/docker/docker/pkg/units", "Comment": "v1.4.1-3245-g443437f", diff --git a/Godeps/_workspace/src/github.com/docker/docker/pkg/stringid/README.md b/Godeps/_workspace/src/github.com/docker/docker/pkg/stringid/README.md new file mode 100644 index 0000000000..37a5098fd9 --- /dev/null +++ b/Godeps/_workspace/src/github.com/docker/docker/pkg/stringid/README.md @@ -0,0 +1 @@ +This package provides helper functions for dealing with string identifiers diff --git a/Godeps/_workspace/src/github.com/docker/docker/pkg/stringid/stringid.go b/Godeps/_workspace/src/github.com/docker/docker/pkg/stringid/stringid.go new file mode 100644 index 0000000000..bf39df9b73 --- /dev/null +++ b/Godeps/_workspace/src/github.com/docker/docker/pkg/stringid/stringid.go @@ -0,0 +1,38 @@ +package stringid + +import ( + "crypto/rand" + "encoding/hex" + "io" + "strconv" +) + +// TruncateID returns a shorthand version of a string identifier for convenience. +// A collision with other shorthands is very unlikely, but possible. +// In case of a collision a lookup with TruncIndex.Get() will fail, and the caller +// will need to use a langer prefix, or the full-length Id. +func TruncateID(id string) string { + shortLen := 12 + if len(id) < shortLen { + shortLen = len(id) + } + return id[:shortLen] +} + +// GenerateRandomID returns an unique id +func GenerateRandomID() string { + for { + id := make([]byte, 32) + if _, err := io.ReadFull(rand.Reader, id); err != nil { + panic(err) // This shouldn't happen + } + value := hex.EncodeToString(id) + // if we try to parse the truncated for as an int and we don't have + // an error then the value is all numberic and causes issues when + // used as a hostname. ref #3869 + if _, err := strconv.ParseInt(TruncateID(value), 10, 64); err == nil { + continue + } + return value + } +} diff --git a/Godeps/_workspace/src/github.com/docker/docker/pkg/stringid/stringid_test.go b/Godeps/_workspace/src/github.com/docker/docker/pkg/stringid/stringid_test.go new file mode 100644 index 0000000000..21f8f8a2fb --- /dev/null +++ b/Godeps/_workspace/src/github.com/docker/docker/pkg/stringid/stringid_test.go @@ -0,0 +1,35 @@ +package stringid + +import "testing" + +func TestGenerateRandomID(t *testing.T) { + id := GenerateRandomID() + + if len(id) != 64 { + t.Fatalf("Id returned is incorrect: %s", id) + } +} + +func TestShortenId(t *testing.T) { + id := GenerateRandomID() + truncID := TruncateID(id) + if len(truncID) != 12 { + t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID) + } +} + +func TestShortenIdEmpty(t *testing.T) { + id := "" + truncID := TruncateID(id) + if len(truncID) > len(id) { + t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID) + } +} + +func TestShortenIdInvalid(t *testing.T) { + id := "1234" + truncID := TruncateID(id) + if len(truncID) != len(id) { + t.Fatalf("Id returned is incorrect: truncate on %s returned %s", id, truncID) + } +}