Browse code

Factor cache-probing logic out of buildfile.commit() and CmdRun().

Graydon Hoare authored on 2013/12/12 14:33:15
Showing 1 changed files
... ...
@@ -87,6 +87,26 @@ func (b *buildFile) CmdMaintainer(name string) error {
87 87
 	return b.commit("", b.config.Cmd, fmt.Sprintf("MAINTAINER %s", name))
88 88
 }
89 89
 
90
+// probeCache checks to see if image-caching is enabled (`b.utilizeCache`)
91
+// and if so attempts to look up the current `b.image` and `b.config` pair
92
+// in the current server `b.srv`. If an image is found, probeCache returns
93
+// `(true, nil)`. If no image is found, it returns `(false, nil)`. If there
94
+// is any error, it returns `(false, err)`.
95
+func (b *buildFile) probeCache() (bool, error) {
96
+	if b.utilizeCache {
97
+		if cache, err := b.srv.ImageGetCached(b.image, b.config); err != nil {
98
+			return false, err
99
+		} else if cache != nil {
100
+			fmt.Fprintf(b.outStream, " ---> Using cache\n")
101
+			utils.Debugf("[BUILDER] Use cached version")
102
+			b.image = cache.ID
103
+			return true, nil
104
+		} else {
105
+			utils.Debugf("[BUILDER] Cache miss")
106
+		}
107
+	}
108
+	return false, nil
109
+}
90 110
 func (b *buildFile) CmdRun(args string) error {
91 111
 	if b.image == "" {
92 112
 		return fmt.Errorf("Please provide a source image with `from` prior to run")
... ...
@@ -104,17 +124,12 @@ func (b *buildFile) CmdRun(args string) error {
104 104
 
105 105
 	utils.Debugf("Command to be executed: %v", b.config.Cmd)
106 106
 
107
-	if b.utilizeCache {
108
-		if cache, err := b.srv.ImageGetCached(b.image, b.config); err != nil {
109
-			return err
110
-		} else if cache != nil {
111
-			fmt.Fprintf(b.outStream, " ---> Using cache\n")
112
-			utils.Debugf("[BUILDER] Use cached version")
113
-			b.image = cache.ID
114
-			return nil
115
-		} else {
116
-			utils.Debugf("[BUILDER] Cache miss")
117
-		}
107
+	hit, err := b.probeCache()
108
+	if err != nil {
109
+		return err
110
+	}
111
+	if hit {
112
+		return nil
118 113
 	}
119 114
 
120 115
 	cid, err := b.run()
... ...
@@ -460,17 +475,12 @@ func (b *buildFile) commit(id string, autoCmd []string, comment string) error {
460 460
 		b.config.Cmd = []string{"/bin/sh", "-c", "#(nop) " + comment}
461 461
 		defer func(cmd []string) { b.config.Cmd = cmd }(cmd)
462 462
 
463
-		if b.utilizeCache {
464
-			if cache, err := b.srv.ImageGetCached(b.image, b.config); err != nil {
465
-				return err
466
-			} else if cache != nil {
467
-				fmt.Fprintf(b.outStream, " ---> Using cache\n")
468
-				utils.Debugf("[BUILDER] Use cached version")
469
-				b.image = cache.ID
470
-				return nil
471
-			} else {
472
-				utils.Debugf("[BUILDER] Cache miss")
473
-			}
463
+		hit, err := b.probeCache()
464
+		if err != nil {
465
+			return err
466
+		}
467
+		if hit {
468
+			return nil
474 469
 		}
475 470
 
476 471
 		container, warnings, err := b.runtime.Create(b.config, "")