diff --git a/cmd/buildah/bud.go b/cmd/buildah/bud.go index 3cb1b9f8125..cf2a63bceff 100644 --- a/cmd/buildah/bud.go +++ b/cmd/buildah/bud.go @@ -12,6 +12,7 @@ import ( "github.com/containers/buildah/imagebuildah" buildahcli "github.com/containers/buildah/pkg/cli" "github.com/containers/buildah/pkg/parse" + buildahutil "github.com/containers/buildah/pkg/util" "github.com/containers/buildah/util" "github.com/containers/common/pkg/auth" "github.com/pkg/errors" @@ -191,7 +192,7 @@ func budCmd(c *cobra.Command, inputArgs []string, iopts budOptions) error { if len(containerfiles) == 0 { // Try to find the Containerfile/Dockerfile within the contextDir - containerfile, err := discoverContainerfile(contextDir) + containerfile, err := buildahcli.DiscoverContainerfile(contextDir) if err != nil { return err } @@ -384,44 +385,3 @@ func budCmd(c *cobra.Command, inputArgs []string, iopts budOptions) error { } return err } - -// 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 -} diff --git a/pkg/util/test/test1/Containerfile b/pkg/util/test/test1/Containerfile new file mode 100644 index 00000000000..453e3aca602 --- /dev/null +++ b/pkg/util/test/test1/Containerfile @@ -0,0 +1 @@ +from scratch diff --git a/pkg/util/test/test1/Dockerfile b/pkg/util/test/test1/Dockerfile new file mode 100644 index 00000000000..453e3aca602 --- /dev/null +++ b/pkg/util/test/test1/Dockerfile @@ -0,0 +1 @@ +from scratch diff --git a/pkg/util/test/test2/Dockerfile b/pkg/util/test/test2/Dockerfile new file mode 100644 index 00000000000..453e3aca602 --- /dev/null +++ b/pkg/util/test/test2/Dockerfile @@ -0,0 +1 @@ +from scratch diff --git a/pkg/util/util.go b/pkg/util/util.go new file mode 100644 index 00000000000..8410d3984d9 --- /dev/null +++ b/pkg/util/util.go @@ -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 +} diff --git a/pkg/util/util_test.go b/pkg/util/util_test.go new file mode 100644 index 00000000000..a39108e5711 --- /dev/null +++ b/pkg/util/util_test.go @@ -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") + +}