-
Notifications
You must be signed in to change notification settings - Fork 779
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move DiscoverContainerfile to pkg/util directory
Then podman can use it Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
- Loading branch information
Showing
6 changed files
with
86 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from scratch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from scratch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from scratch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package util | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// DiscoverContainerfile tries to find a Containerfile or a Dockerfile within the provided `path`. | ||
func DiscoverContainerfile(path string) (foundCtrFile string, err error) { | ||
// Test for existence of the file | ||
target, err := os.Stat(path) | ||
if err != nil { | ||
return "", errors.Wrap(err, "discovering Containerfile") | ||
} | ||
|
||
switch mode := target.Mode(); { | ||
case mode.IsDir(): | ||
// If the path is a real directory, we assume a Containerfile or a Dockerfile within it | ||
ctrfile := filepath.Join(path, "Containerfile") | ||
|
||
// Test for existence of the Containerfile file | ||
file, err := os.Stat(ctrfile) | ||
if err != nil { | ||
// See if we have a Dockerfile within it | ||
ctrfile = filepath.Join(path, "Dockerfile") | ||
|
||
// Test for existence of the Dockerfile file | ||
file, err = os.Stat(ctrfile) | ||
if err != nil { | ||
return "", errors.Wrap(err, "cannot find Containerfile or Dockerfile in context directory") | ||
} | ||
} | ||
|
||
// The file exists, now verify the correct mode | ||
if mode := file.Mode(); mode.IsRegular() { | ||
foundCtrFile = ctrfile | ||
} else { | ||
return "", errors.Errorf("assumed Containerfile %q is not a file", ctrfile) | ||
} | ||
|
||
case mode.IsRegular(): | ||
// If the context dir is a file, we assume this as Containerfile | ||
foundCtrFile = path | ||
} | ||
|
||
return foundCtrFile, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDiscoverContainerfile(t *testing.T) { | ||
_, err := DiscoverContainerfile("./bogus") | ||
assert.NotNil(t, err) | ||
|
||
_, err = DiscoverContainerfile("./") | ||
assert.NotNil(t, err) | ||
|
||
name, err := DiscoverContainerfile("test/test1/Dockerfile") | ||
assert.Nil(t, err) | ||
assert.Equal(t, name, "test/test1/Dockerfile") | ||
|
||
name, err = DiscoverContainerfile("test/test1/Containerfile") | ||
assert.Nil(t, err) | ||
assert.Equal(t, name, "test/test1/Containerfile") | ||
|
||
name, err = DiscoverContainerfile("test/test1") | ||
assert.Nil(t, err) | ||
assert.Equal(t, name, "test/test1/Containerfile") | ||
|
||
name, err = DiscoverContainerfile("test/test2") | ||
assert.Nil(t, err) | ||
assert.Equal(t, name, "test/test2/Dockerfile") | ||
|
||
} |