Browse code

Support windows style dockerfile paths for build cmd

Currently TestBuildRenamedDockerfile fails since passing
custom dockerfile paths like:

docker build -f dir/file .

fails on windows because those are unix paths. Instead, on
windows accept windows style paths like:

docker build -f dir\file .

and convert them to unix style paths using the helper we
have in `pkg/archive` so that daemon can correctly locate
the path in the context.

Signed-off-by: Ahmet Alp Balkan <ahmetalpbalkan@gmail.com>

Ahmet Alp Balkan authored on 2015/02/25 12:43:29
Showing 6 changed files
... ...
@@ -155,11 +155,10 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
155 155
 		if *dockerfileName == "" {
156 156
 			// No -f/--file was specified so use the default
157 157
 			*dockerfileName = api.DefaultDockerfileName
158
-			filename = path.Join(absRoot, *dockerfileName)
158
+			filename = filepath.Join(absRoot, *dockerfileName)
159 159
 		}
160 160
 
161 161
 		origDockerfile := *dockerfileName // used for error msg
162
-
163 162
 		if filename, err = filepath.Abs(filename); err != nil {
164 163
 			return err
165 164
 		}
... ...
@@ -175,6 +174,11 @@ func (cli *DockerCli) CmdBuild(args ...string) error {
175 175
 		if err != nil {
176 176
 			return err
177 177
 		}
178
+		// And canonicalize dockerfile name to a platform-independent one
179
+		*dockerfileName, err = archive.CanonicalTarNameForPath(*dockerfileName)
180
+		if err != nil {
181
+			return fmt.Errorf("Cannot canonicalize dockerfile path %s: %v", dockerfileName, err)
182
+		}
178 183
 
179 184
 		if _, err = os.Lstat(filename); os.IsNotExist(err) {
180 185
 			return fmt.Errorf("Cannot locate Dockerfile: %s", origDockerfile)
... ...
@@ -175,7 +175,7 @@ type tarAppender struct {
175 175
 // canonicalTarName provides a platform-independent and consistent posix-style
176 176
 //path for files and directories to be archived regardless of the platform.
177 177
 func canonicalTarName(name string, isDir bool) (string, error) {
178
-	name, err := canonicalTarNameForPath(name)
178
+	name, err := CanonicalTarNameForPath(name)
179 179
 	if err != nil {
180 180
 		return "", err
181 181
 	}
... ...
@@ -12,7 +12,7 @@ import (
12 12
 // canonicalTarNameForPath returns platform-specific filepath
13 13
 // to canonical posix-style path for tar archival. p is relative
14 14
 // path.
15
-func canonicalTarNameForPath(p string) (string, error) {
15
+func CanonicalTarNameForPath(p string) (string, error) {
16 16
 	return p, nil // already unix-style
17 17
 }
18 18
 
... ...
@@ -13,7 +13,7 @@ func TestCanonicalTarNameForPath(t *testing.T) {
13 13
 		{"foo/dir/", "foo/dir/"},
14 14
 	}
15 15
 	for _, v := range cases {
16
-		if out, err := canonicalTarNameForPath(v.in); err != nil {
16
+		if out, err := CanonicalTarNameForPath(v.in); err != nil {
17 17
 			t.Fatalf("cannot get canonical name for path: %s: %v", v.in, err)
18 18
 		} else if out != v.expected {
19 19
 			t.Fatalf("wrong canonical tar name. expected:%s got:%s", v.expected, out)
... ...
@@ -12,7 +12,7 @@ import (
12 12
 // canonicalTarNameForPath returns platform-specific filepath
13 13
 // to canonical posix-style path for tar archival. p is relative
14 14
 // path.
15
-func canonicalTarNameForPath(p string) (string, error) {
15
+func CanonicalTarNameForPath(p string) (string, error) {
16 16
 	// windows: convert windows style relative path with backslashes
17 17
 	// into forward slashes. since windows does not allow '/' or '\'
18 18
 	// in file names, it is mostly safe to replace however we must
... ...
@@ -17,7 +17,7 @@ func TestCanonicalTarNameForPath(t *testing.T) {
17 17
 		{`foo\bar`, "foo/bar/", false},
18 18
 	}
19 19
 	for _, v := range cases {
20
-		if out, err := canonicalTarNameForPath(v.in); err != nil && !v.shouldFail {
20
+		if out, err := CanonicalTarNameForPath(v.in); err != nil && !v.shouldFail {
21 21
 			t.Fatalf("cannot get canonical name for path: %s: %v", v.in, err)
22 22
 		} else if v.shouldFail && err == nil {
23 23
 			t.Fatalf("canonical path call should have pailed with error. in=%s out=%s", v.in, out)