cli/command/image: move build-context detection to build
Removes direct imports of github.com/docker/docker/builder in the image package, to be moved later. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
39
cli/command/image/build/context_detect.go
Normal file
39
cli/command/image/build/context_detect.go
Normal file
@ -0,0 +1,39 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/docker/docker/builder/remotecontext/urlutil"
|
||||
)
|
||||
|
||||
// ContextType describes the type (source) of build-context specified.
|
||||
type ContextType string
|
||||
|
||||
const (
|
||||
ContextTypeStdin ContextType = "stdin" // ContextTypeStdin indicates that the build-context is a TAR archive passed through STDIN.
|
||||
ContextTypeLocal ContextType = "local" // ContextTypeLocal indicates that the build-context is a local directory.
|
||||
ContextTypeRemote ContextType = "remote" // ContextTypeRemote indicates that the build-context is a remote URL.
|
||||
ContextTypeGit ContextType = "git" // ContextTypeGit indicates that the build-context is a GIT URL.
|
||||
)
|
||||
|
||||
// DetectContextType detects the type (source) of the build-context.
|
||||
func DetectContextType(specifiedContext string) (ContextType, error) {
|
||||
switch {
|
||||
case specifiedContext == "-":
|
||||
return ContextTypeStdin, nil
|
||||
case isLocalDir(specifiedContext):
|
||||
return ContextTypeLocal, nil
|
||||
case urlutil.IsGitURL(specifiedContext):
|
||||
return ContextTypeGit, nil
|
||||
case urlutil.IsURL(specifiedContext):
|
||||
return ContextTypeRemote, nil
|
||||
default:
|
||||
return "", fmt.Errorf("unable to prepare context: path %q not found", specifiedContext)
|
||||
}
|
||||
}
|
||||
|
||||
func isLocalDir(c string) bool {
|
||||
_, err := os.Stat(c)
|
||||
return err == nil
|
||||
}
|
||||
Reference in New Issue
Block a user