Browse code

Make docker client an interface

Guillaume J. Charmes authored on 2013/05/21 08:00:16
Showing 1 changed files
... ...
@@ -12,7 +12,13 @@ import (
12 12
 	"strings"
13 13
 )
14 14
 
15
-type BuilderClient struct {
15
+type BuilderClient interface {
16
+	Build(io.Reader) (string, error)
17
+	CmdFrom(string) error
18
+	CmdRun(string) error
19
+}
20
+
21
+type builderClient struct {
16 22
 	cli *DockerCli
17 23
 
18 24
 	image      string
... ...
@@ -25,7 +31,7 @@ type BuilderClient struct {
25 25
 	needCommit bool
26 26
 }
27 27
 
28
-func (b *BuilderClient) clearTmp(containers, images map[string]struct{}) {
28
+func (b builderClient) clearTmp(containers, images map[string]struct{}) {
29 29
 	for c := range containers {
30 30
 		if _, _, err := b.cli.call("DELETE", "/containers/"+c, nil); err != nil {
31 31
 			utils.Debugf("%s", err)
... ...
@@ -40,7 +46,7 @@ func (b *BuilderClient) clearTmp(containers, images map[string]struct{}) {
40 40
 	}
41 41
 }
42 42
 
43
-func (b *BuilderClient) CmdFrom(name string) error {
43
+func (b builderClient) CmdFrom(name string) error {
44 44
 	obj, statusCode, err := b.cli.call("GET", "/images/"+name+"/json", nil)
45 45
 	if statusCode == 404 {
46 46
 		if err := b.cli.hijack("POST", "/images/create?fromImage="+name, false); err != nil {
... ...
@@ -63,13 +69,13 @@ func (b *BuilderClient) CmdFrom(name string) error {
63 63
 	return nil
64 64
 }
65 65
 
66
-func (b *BuilderClient) CmdMaintainer(name string) error {
66
+func (b builderClient) CmdMaintainer(name string) error {
67 67
 	b.needCommit = true
68 68
 	b.maintainer = name
69 69
 	return nil
70 70
 }
71 71
 
72
-func (b *BuilderClient) CmdRun(args string) error {
72
+func (b builderClient) CmdRun(args string) error {
73 73
 	if b.image == "" {
74 74
 		return fmt.Errorf("Please provide a source image with `from` prior to run")
75 75
 	}
... ...
@@ -105,7 +111,7 @@ func (b *BuilderClient) CmdRun(args string) error {
105 105
 	return b.commit(cid)
106 106
 }
107 107
 
108
-func (b *BuilderClient) CmdEnv(args string) error {
108
+func (b builderClient) CmdEnv(args string) error {
109 109
 	b.needCommit = true
110 110
 	tmp := strings.SplitN(args, " ", 2)
111 111
 	if len(tmp) != 2 {
... ...
@@ -124,25 +130,25 @@ func (b *BuilderClient) CmdEnv(args string) error {
124 124
 	return nil
125 125
 }
126 126
 
127
-func (b *BuilderClient) CmdCmd(args string) error {
127
+func (b builderClient) CmdCmd(args string) error {
128 128
 	b.needCommit = true
129 129
 	b.config.Cmd = []string{"/bin/sh", "-c", args}
130 130
 	return nil
131 131
 }
132 132
 
133
-func (b *BuilderClient) CmdExpose(args string) error {
133
+func (b builderClient) CmdExpose(args string) error {
134 134
 	ports := strings.Split(args, " ")
135 135
 	b.config.PortSpecs = append(ports, b.config.PortSpecs...)
136 136
 	return nil
137 137
 }
138 138
 
139
-func (b *BuilderClient) CmdInsert(args string) error {
139
+func (b builderClient) CmdInsert(args string) error {
140 140
 	// FIXME: Reimplement this once the remove_hijack branch gets merged.
141 141
 	// We need to retrieve the resulting Id
142 142
 	return fmt.Errorf("INSERT not implemented")
143 143
 }
144 144
 
145
-func (b *BuilderClient) run() (string, error) {
145
+func (b builderClient) run() (string, error) {
146 146
 	if b.image == "" {
147 147
 		return "", fmt.Errorf("Please provide a source image with `from` prior to run")
148 148
 	}
... ...
@@ -183,7 +189,7 @@ func (b *BuilderClient) run() (string, error) {
183 183
 	return apiRun.Id, nil
184 184
 }
185 185
 
186
-func (b *BuilderClient) commit(id string) error {
186
+func (b builderClient) commit(id string) error {
187 187
 	if b.image == "" {
188 188
 		return fmt.Errorf("Please provide a source image with `from` prior to run")
189 189
 	}
... ...
@@ -219,7 +225,7 @@ func (b *BuilderClient) commit(id string) error {
219 219
 	return nil
220 220
 }
221 221
 
222
-func (b *BuilderClient) Build(dockerfile io.Reader) (string, error) {
222
+func (b builderClient) Build(dockerfile io.Reader) (string, error) {
223 223
 	//	defer b.clearTmp(tmpContainers, tmpImages)
224 224
 	file := bufio.NewReader(dockerfile)
225 225
 	for {
... ...
@@ -274,8 +280,8 @@ func (b *BuilderClient) Build(dockerfile io.Reader) (string, error) {
274 274
 	return "", fmt.Errorf("An error occured during the build\n")
275 275
 }
276 276
 
277
-func NewBuilderClient(addr string, port int) *BuilderClient {
278
-	return &BuilderClient{
277
+func NewBuilderClient(addr string, port int) BuilderClient {
278
+	return &builderClient{
279 279
 		cli:           NewDockerCli(addr, port),
280 280
 		config:        &Config{},
281 281
 		tmpContainers: make(map[string]struct{}),