From 0eb383b2a92e1bd0fef5d0a2a068eeb5a8aaeaea Mon Sep 17 00:00:00 2001 From: Kevin Cui Date: Wed, 12 Jun 2024 11:41:14 +0800 Subject: [PATCH] refactor(build): improve err when file specified by -f does not exist When the user specifies a Containerfile or Dockfile with the -f flag in podman build, if the file does not exist, the error should be intuitive to the user. Fixed: #22940 Signed-off-by: Kevin Cui --- cmd/podman/common/build.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/cmd/podman/common/build.go b/cmd/podman/common/build.go index 2abf62520e8d..819caffd2998 100644 --- a/cmd/podman/common/build.go +++ b/cmd/podman/common/build.go @@ -202,10 +202,7 @@ func ParseBuildOpts(cmd *cobra.Command, args []string, buildOpts *BuildFlagsWrap // No context directory or URL was specified. Try to use the home of // the first locally-available Containerfile. for i := range containerFiles { - if strings.HasPrefix(containerFiles[i], "http://") || - strings.HasPrefix(containerFiles[i], "https://") || - strings.HasPrefix(containerFiles[i], "git://") || - strings.HasPrefix(containerFiles[i], "github.com/") { + if isUrl(containerFiles[i]) { continue } absFile, err := filepath.Abs(containerFiles[i]) @@ -239,6 +236,16 @@ func ParseBuildOpts(cmd *cobra.Command, args []string, buildOpts *BuildFlagsWrap default: return nil, fmt.Errorf("no Containerfile or Dockerfile specified or found in context directory, %s: %w", contextDir, syscall.ENOENT) } + } else { + for _, f := range containerFiles { + if isUrl(f) || f == "/dev/stdin" { + continue + } + if !utils.FileExists(f) { + return nil, fmt.Errorf("the specified Containerfile or Dockerfile does not exist, %s: %w", f, syscall.ENOENT) + } + } + } var logFile *os.File @@ -628,3 +635,10 @@ func parseDockerignore(ignoreFile string) ([]string, error) { } return excludes, nil } + +func isUrl(s string) bool { + return strings.HasPrefix(s, "http://") || + strings.HasPrefix(s, "https://") || + strings.HasPrefix(s, "git://") || + strings.HasPrefix(s, "github.com/") +}