Skip to content

Commit

Permalink
Merge pull request #3442 from rhatdan/define
Browse files Browse the repository at this point in the history
Move DiscoverContainerfile to define directory, so podman can use it
  • Loading branch information
rhatdan authored Aug 21, 2021
2 parents f5f2dca + 9faf946 commit 1b3f9cd
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 43 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?=

Expand Down
44 changes: 2 additions & 42 deletions cmd/buildah/bud.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions pkg/util/test/test1/Containerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from scratch
1 change: 1 addition & 0 deletions pkg/util/test/test1/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from scratch
1 change: 1 addition & 0 deletions pkg/util/test/test2/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from scratch
49 changes: 49 additions & 0 deletions pkg/util/util.go
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
}
32 changes: 32 additions & 0 deletions pkg/util/util_test.go
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")

}

0 comments on commit 1b3f9cd

Please sign in to comment.