Browse code

Move utility package 'namesgenerator' to pkg/namesgenerator

Solomon Hykes authored on 2013/12/24 08:45:18
Showing 5 changed files
1 1
deleted file mode 100644
... ...
@@ -1,67 +0,0 @@
1
-package namesgenerator
2
-
3
-import (
4
-	"fmt"
5
-	"math/rand"
6
-	"time"
7
-)
8
-
9
-type NameChecker interface {
10
-	Exists(name string) bool
11
-}
12
-
13
-var (
14
-	left = [...]string{"happy", "jolly", "dreamy", "sad", "angry", "pensive", "focused", "sleepy", "grave", "distracted", "determined", "stoic", "stupefied", "sharp", "agitated", "cocky", "tender", "goofy", "furious", "desperate", "hopeful", "compassionate", "silly", "lonely", "condescending", "naughty", "kickass", "drunk", "boring", "nostalgic", "ecstatic", "insane", "cranky", "mad", "jovial", "sick", "hungry", "thirsty", "elegant", "backstabbing", "clever", "trusting", "loving", "suspicious", "berserk", "high", "romantic", "prickly", "evil"}
15
-	// Docker 0.7.x generates names from notable scientists and hackers.
16
-	//
17
-	// Ada Lovelace invented the first algorithm. http://en.wikipedia.org/wiki/Ada_Lovelace (thanks James Turnbull)
18
-	// Alan Turing was a founding father of computer science. http://en.wikipedia.org/wiki/Alan_Turing.
19
-	// Albert Einstein invented the general theory of relativity. http://en.wikipedia.org/wiki/Albert_Einstein
20
-	// Ambroise Pare invented modern surgery. http://en.wikipedia.org/wiki/Ambroise_Par%C3%A9
21
-	// Archimedes was a physicist, engineer and mathematician who invented too many things to list them here. http://en.wikipedia.org/wiki/Archimedes
22
-	// Benjamin Franklin is famous for his experiments in electricity and the invention of the lightning rod.
23
-	// Charles Babbage invented the concept of a programmable computer. http://en.wikipedia.org/wiki/Charles_Babbage.
24
-	// Charles Darwin established the principles of natural evolution. http://en.wikipedia.org/wiki/Charles_Darwin.
25
-	// Dennis Ritchie and Ken Thompson created UNIX and the C programming language. http://en.wikipedia.org/wiki/Dennis_Ritchie http://en.wikipedia.org/wiki/Ken_Thompson
26
-	// Douglas Engelbart gave the mother of all demos: http://en.wikipedia.org/wiki/Douglas_Engelbart
27
-	// Emmett Brown invented time travel. http://en.wikipedia.org/wiki/Emmett_Brown (thanks Brian Goff)
28
-	// Enrico Fermi invented the first nuclear reactor. http://en.wikipedia.org/wiki/Enrico_Fermi.
29
-	// Euclid invented geometry. http://en.wikipedia.org/wiki/Euclid
30
-	// Galileo was a founding father of modern astronomy, and faced politics and obscurantism to establish scientific truth.  http://en.wikipedia.org/wiki/Galileo_Galilei
31
-	// Henry Poincare made fundamental contributions in several fields of mathematics. http://en.wikipedia.org/wiki/Henri_Poincar%C3%A9
32
-	// Isaac Newton invented classic mechanics and modern optics. http://en.wikipedia.org/wiki/Isaac_Newton
33
-	// John McCarthy invented LISP: http://en.wikipedia.org/wiki/John_McCarthy_(computer_scientist)
34
-	// Leonardo Da Vinci invented too many things to list here. http://en.wikipedia.org/wiki/Leonardo_da_Vinci.
35
-	// Linus Torvalds invented Linux and Git. http://en.wikipedia.org/wiki/Linus_Torvalds
36
-	// Louis Pasteur discovered vaccination, fermentation and pasteurization. http://en.wikipedia.org/wiki/Louis_Pasteur.
37
-	// Malcolm McLean invented the modern shipping container: http://en.wikipedia.org/wiki/Malcom_McLean
38
-	// Marie Curie discovered radioactivity. http://en.wikipedia.org/wiki/Marie_Curie.
39
-	// Muhammad ibn Jābir al-Ḥarrānī al-Battānī was a founding father of astronomy. http://en.wikipedia.org/wiki/Mu%E1%B8%A5ammad_ibn_J%C4%81bir_al-%E1%B8%A4arr%C4%81n%C4%AB_al-Batt%C4%81n%C4%AB
40
-	// Niels Bohr is the father of quantum theory. http://en.wikipedia.org/wiki/Niels_Bohr.
41
-	// Nikola Tesla invented the AC electric system and every gaget ever used by a James Bond villain. http://en.wikipedia.org/wiki/Nikola_Tesla
42
-	// Pierre de Fermat pioneered several aspects of modern mathematics. http://en.wikipedia.org/wiki/Pierre_de_Fermat
43
-	// Richard Feynmann was a key contributor to quantum mechanics and particle physics. http://en.wikipedia.org/wiki/Richard_Feynman
44
-	// Rob Pike was a key contributor to Unix, Plan 9, the X graphic system, utf-8, and the Go programming language. http://en.wikipedia.org/wiki/Rob_Pike
45
-	// Stephen Hawking pioneered the field of cosmology by combining general relativity and quantum mechanics. http://en.wikipedia.org/wiki/Stephen_Hawking
46
-	// Steve Wozniak invented the Apple I and Apple II. http://en.wikipedia.org/wiki/Steve_Wozniak
47
-	// Werner Heisenberg was a founding father of quantum mechanics. http://en.wikipedia.org/wiki/Werner_Heisenberg
48
-	// William Shockley, Walter Houser Brattain and John Bardeen co-invented the transistor (thanks Brian Goff).
49
-	//	http://en.wikipedia.org/wiki/John_Bardeen
50
-	//	http://en.wikipedia.org/wiki/Walter_Houser_Brattain
51
-	//	http://en.wikipedia.org/wiki/William_Shockley
52
-	right = [...]string{"lovelace", "franklin", "tesla", "einstein", "bohr", "davinci", "pasteur", "nobel", "curie", "darwin", "turing", "ritchie", "torvalds", "pike", "thompson", "wozniak", "galileo", "euclide", "newton", "fermat", "archimede", "poincare", "heisenberg", "feynmann", "hawkings", "fermi", "pare", "mccarthy", "engelbart", "babbage", "albattani", "ptolemy", "bell", "wright", "lumiere", "morse", "mclean", "brown", "bardeen", "brattain", "shockley"}
53
-)
54
-
55
-func GenerateRandomName(checker NameChecker) (string, error) {
56
-	retry := 5
57
-	rand.Seed(time.Now().UnixNano())
58
-	name := fmt.Sprintf("%s_%s", left[rand.Intn(len(left))], right[rand.Intn(len(right))])
59
-	for checker != nil && checker.Exists(name) && retry > 0 {
60
-		name = fmt.Sprintf("%s%d", name, rand.Intn(10))
61
-		retry = retry - 1
62
-	}
63
-	if retry == 0 {
64
-		return name, fmt.Errorf("Error generating random name")
65
-	}
66
-	return name, nil
67
-}
68 1
deleted file mode 100644
... ...
@@ -1,49 +0,0 @@
1
-package namesgenerator
2
-
3
-import (
4
-	"testing"
5
-)
6
-
7
-type FalseChecker struct{}
8
-
9
-func (n *FalseChecker) Exists(name string) bool {
10
-	return false
11
-}
12
-
13
-type TrueChecker struct{}
14
-
15
-func (n *TrueChecker) Exists(name string) bool {
16
-	return true
17
-}
18
-
19
-func TestGenerateRandomName(t *testing.T) {
20
-	if _, err := GenerateRandomName(&FalseChecker{}); err != nil {
21
-		t.Error(err)
22
-	}
23
-
24
-	if _, err := GenerateRandomName(&TrueChecker{}); err == nil {
25
-		t.Error("An error was expected")
26
-	}
27
-
28
-}
29
-
30
-// Make sure the generated names are awesome
31
-func TestGenerateAwesomeNames(t *testing.T) {
32
-	name, err := GenerateRandomName(&FalseChecker{})
33
-	if err != nil {
34
-		t.Error(err)
35
-	}
36
-	if !isAwesome(name) {
37
-		t.Fatalf("Generated name '%s' is not awesome.", name)
38
-	}
39
-}
40
-
41
-// To be awesome, a container name must involve cool inventors, be easy to remember,
42
-// be at least mildly funny, and always be politically correct for enterprise adoption.
43
-func isAwesome(name string) bool {
44
-	coolInventorNames := true
45
-	easyToRemember := true
46
-	mildlyFunnyOnOccasion := true
47
-	politicallyCorrect := true
48
-	return coolInventorNames && easyToRemember && mildlyFunnyOnOccasion && politicallyCorrect
49
-}
50 1
new file mode 100644
... ...
@@ -0,0 +1,67 @@
0
+package namesgenerator
1
+
2
+import (
3
+	"fmt"
4
+	"math/rand"
5
+	"time"
6
+)
7
+
8
+type NameChecker interface {
9
+	Exists(name string) bool
10
+}
11
+
12
+var (
13
+	left = [...]string{"happy", "jolly", "dreamy", "sad", "angry", "pensive", "focused", "sleepy", "grave", "distracted", "determined", "stoic", "stupefied", "sharp", "agitated", "cocky", "tender", "goofy", "furious", "desperate", "hopeful", "compassionate", "silly", "lonely", "condescending", "naughty", "kickass", "drunk", "boring", "nostalgic", "ecstatic", "insane", "cranky", "mad", "jovial", "sick", "hungry", "thirsty", "elegant", "backstabbing", "clever", "trusting", "loving", "suspicious", "berserk", "high", "romantic", "prickly", "evil"}
14
+	// Docker 0.7.x generates names from notable scientists and hackers.
15
+	//
16
+	// Ada Lovelace invented the first algorithm. http://en.wikipedia.org/wiki/Ada_Lovelace (thanks James Turnbull)
17
+	// Alan Turing was a founding father of computer science. http://en.wikipedia.org/wiki/Alan_Turing.
18
+	// Albert Einstein invented the general theory of relativity. http://en.wikipedia.org/wiki/Albert_Einstein
19
+	// Ambroise Pare invented modern surgery. http://en.wikipedia.org/wiki/Ambroise_Par%C3%A9
20
+	// Archimedes was a physicist, engineer and mathematician who invented too many things to list them here. http://en.wikipedia.org/wiki/Archimedes
21
+	// Benjamin Franklin is famous for his experiments in electricity and the invention of the lightning rod.
22
+	// Charles Babbage invented the concept of a programmable computer. http://en.wikipedia.org/wiki/Charles_Babbage.
23
+	// Charles Darwin established the principles of natural evolution. http://en.wikipedia.org/wiki/Charles_Darwin.
24
+	// Dennis Ritchie and Ken Thompson created UNIX and the C programming language. http://en.wikipedia.org/wiki/Dennis_Ritchie http://en.wikipedia.org/wiki/Ken_Thompson
25
+	// Douglas Engelbart gave the mother of all demos: http://en.wikipedia.org/wiki/Douglas_Engelbart
26
+	// Emmett Brown invented time travel. http://en.wikipedia.org/wiki/Emmett_Brown (thanks Brian Goff)
27
+	// Enrico Fermi invented the first nuclear reactor. http://en.wikipedia.org/wiki/Enrico_Fermi.
28
+	// Euclid invented geometry. http://en.wikipedia.org/wiki/Euclid
29
+	// Galileo was a founding father of modern astronomy, and faced politics and obscurantism to establish scientific truth.  http://en.wikipedia.org/wiki/Galileo_Galilei
30
+	// Henry Poincare made fundamental contributions in several fields of mathematics. http://en.wikipedia.org/wiki/Henri_Poincar%C3%A9
31
+	// Isaac Newton invented classic mechanics and modern optics. http://en.wikipedia.org/wiki/Isaac_Newton
32
+	// John McCarthy invented LISP: http://en.wikipedia.org/wiki/John_McCarthy_(computer_scientist)
33
+	// Leonardo Da Vinci invented too many things to list here. http://en.wikipedia.org/wiki/Leonardo_da_Vinci.
34
+	// Linus Torvalds invented Linux and Git. http://en.wikipedia.org/wiki/Linus_Torvalds
35
+	// Louis Pasteur discovered vaccination, fermentation and pasteurization. http://en.wikipedia.org/wiki/Louis_Pasteur.
36
+	// Malcolm McLean invented the modern shipping container: http://en.wikipedia.org/wiki/Malcom_McLean
37
+	// Marie Curie discovered radioactivity. http://en.wikipedia.org/wiki/Marie_Curie.
38
+	// Muhammad ibn Jābir al-Ḥarrānī al-Battānī was a founding father of astronomy. http://en.wikipedia.org/wiki/Mu%E1%B8%A5ammad_ibn_J%C4%81bir_al-%E1%B8%A4arr%C4%81n%C4%AB_al-Batt%C4%81n%C4%AB
39
+	// Niels Bohr is the father of quantum theory. http://en.wikipedia.org/wiki/Niels_Bohr.
40
+	// Nikola Tesla invented the AC electric system and every gaget ever used by a James Bond villain. http://en.wikipedia.org/wiki/Nikola_Tesla
41
+	// Pierre de Fermat pioneered several aspects of modern mathematics. http://en.wikipedia.org/wiki/Pierre_de_Fermat
42
+	// Richard Feynmann was a key contributor to quantum mechanics and particle physics. http://en.wikipedia.org/wiki/Richard_Feynman
43
+	// Rob Pike was a key contributor to Unix, Plan 9, the X graphic system, utf-8, and the Go programming language. http://en.wikipedia.org/wiki/Rob_Pike
44
+	// Stephen Hawking pioneered the field of cosmology by combining general relativity and quantum mechanics. http://en.wikipedia.org/wiki/Stephen_Hawking
45
+	// Steve Wozniak invented the Apple I and Apple II. http://en.wikipedia.org/wiki/Steve_Wozniak
46
+	// Werner Heisenberg was a founding father of quantum mechanics. http://en.wikipedia.org/wiki/Werner_Heisenberg
47
+	// William Shockley, Walter Houser Brattain and John Bardeen co-invented the transistor (thanks Brian Goff).
48
+	//	http://en.wikipedia.org/wiki/John_Bardeen
49
+	//	http://en.wikipedia.org/wiki/Walter_Houser_Brattain
50
+	//	http://en.wikipedia.org/wiki/William_Shockley
51
+	right = [...]string{"lovelace", "franklin", "tesla", "einstein", "bohr", "davinci", "pasteur", "nobel", "curie", "darwin", "turing", "ritchie", "torvalds", "pike", "thompson", "wozniak", "galileo", "euclide", "newton", "fermat", "archimede", "poincare", "heisenberg", "feynmann", "hawkings", "fermi", "pare", "mccarthy", "engelbart", "babbage", "albattani", "ptolemy", "bell", "wright", "lumiere", "morse", "mclean", "brown", "bardeen", "brattain", "shockley"}
52
+)
53
+
54
+func GenerateRandomName(checker NameChecker) (string, error) {
55
+	retry := 5
56
+	rand.Seed(time.Now().UnixNano())
57
+	name := fmt.Sprintf("%s_%s", left[rand.Intn(len(left))], right[rand.Intn(len(right))])
58
+	for checker != nil && checker.Exists(name) && retry > 0 {
59
+		name = fmt.Sprintf("%s%d", name, rand.Intn(10))
60
+		retry = retry - 1
61
+	}
62
+	if retry == 0 {
63
+		return name, fmt.Errorf("Error generating random name")
64
+	}
65
+	return name, nil
66
+}
0 67
new file mode 100644
... ...
@@ -0,0 +1,49 @@
0
+package namesgenerator
1
+
2
+import (
3
+	"testing"
4
+)
5
+
6
+type FalseChecker struct{}
7
+
8
+func (n *FalseChecker) Exists(name string) bool {
9
+	return false
10
+}
11
+
12
+type TrueChecker struct{}
13
+
14
+func (n *TrueChecker) Exists(name string) bool {
15
+	return true
16
+}
17
+
18
+func TestGenerateRandomName(t *testing.T) {
19
+	if _, err := GenerateRandomName(&FalseChecker{}); err != nil {
20
+		t.Error(err)
21
+	}
22
+
23
+	if _, err := GenerateRandomName(&TrueChecker{}); err == nil {
24
+		t.Error("An error was expected")
25
+	}
26
+
27
+}
28
+
29
+// Make sure the generated names are awesome
30
+func TestGenerateAwesomeNames(t *testing.T) {
31
+	name, err := GenerateRandomName(&FalseChecker{})
32
+	if err != nil {
33
+		t.Error(err)
34
+	}
35
+	if !isAwesome(name) {
36
+		t.Fatalf("Generated name '%s' is not awesome.", name)
37
+	}
38
+}
39
+
40
+// To be awesome, a container name must involve cool inventors, be easy to remember,
41
+// be at least mildly funny, and always be politically correct for enterprise adoption.
42
+func isAwesome(name string) bool {
43
+	coolInventorNames := true
44
+	easyToRemember := true
45
+	mildlyFunnyOnOccasion := true
46
+	politicallyCorrect := true
47
+	return coolInventorNames && easyToRemember && mildlyFunnyOnOccasion && politicallyCorrect
48
+}
... ...
@@ -3,7 +3,7 @@ package docker
3 3
 import (
4 4
 	"fmt"
5 5
 	"github.com/dotcloud/docker/archive"
6
-	"github.com/dotcloud/docker/namesgenerator"
6
+	"github.com/dotcloud/docker/pkg/namesgenerator"
7 7
 	"github.com/dotcloud/docker/utils"
8 8
 	"io/ioutil"
9 9
 	"strconv"