diff --git a/Makefile b/Makefile index 03d03c3178..b21e9deb8c 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ LIBSECCOMP_COMMIT := release-2.3 EXTRA_LDFLAGS ?= BUILDAH_LDFLAGS := -ldflags '-X main.GitCommit=$(GIT_COMMIT) -X main.buildInfo=$(SOURCE_DATE_EPOCH) -X main.cniVersion=$(CNI_COMMIT) $(EXTRA_LDFLAGS)' -SOURCES=*.go imagebuildah/*.go bind/*.go chroot/*.go copier/*.go docker/*.go manifests/*.go pkg/blobcache/*.go pkg/chrootuser/*.go pkg/cli/*.go pkg/completion/*.go pkg/formats/*.go pkg/overlay/*.go pkg/parse/*.go pkg/rusage/*.go pkg/sshagent/*.go pkg/umask/*.go util/*.go +SOURCES=*.go imagebuildah/*.go bind/*.go chroot/*.go copier/*.go docker/*.go manifests/*.go pkg/blobcache/*.go pkg/chrootuser/*.go pkg/cli/*.go pkg/completion/*.go pkg/formats/*.go pkg/overlay/*.go pkg/parse/*.go pkg/rusage/*.go pkg/sshagent/*.go pkg/umask/*.go pkg/util/*.go util/*.go LINTFLAGS ?= diff --git a/cmd/buildah/bud.go b/cmd/buildah/bud.go index 3cb1b9f812..f821406d12 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 := buildahutil.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 0000000000..453e3aca60 --- /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 0000000000..453e3aca60 --- /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 0000000000..453e3aca60 --- /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 0000000000..8410d3984d --- /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 0000000000..a39108e571 --- /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") + +}