Skip to content

Commit

Permalink
Move DiscoverContainerfile to pkg/util directory
Browse files Browse the repository at this point in the history
Then podman can use it

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
  • Loading branch information
rhatdan committed Aug 18, 2021
1 parent 197b9a1 commit 54f22eb
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 42 deletions.
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 := buildahcli.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 54f22eb

Please sign in to comment.