github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/command/image/build/dockerignore.go (about)

     1  package build
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	"github.com/docker/docker/builder/dockerignore"
     8  	"github.com/docker/docker/pkg/fileutils"
     9  )
    10  
    11  // ReadDockerignore reads the .dockerignore file in the context directory and
    12  // returns the list of paths to exclude
    13  func ReadDockerignore(contextDir string) ([]string, error) {
    14  	var excludes []string
    15  
    16  	f, err := os.Open(filepath.Join(contextDir, ".dockerignore"))
    17  	switch {
    18  	case os.IsNotExist(err):
    19  		return excludes, nil
    20  	case err != nil:
    21  		return nil, err
    22  	}
    23  	defer f.Close()
    24  
    25  	return dockerignore.ReadAll(f)
    26  }
    27  
    28  // TrimBuildFilesFromExcludes removes the named Dockerfile and .dockerignore from
    29  // the list of excluded files. The daemon will remove them from the final context
    30  // but they must be in available in the context when passed to the API.
    31  func TrimBuildFilesFromExcludes(excludes []string, dockerfile string, dockerfileFromStdin bool) []string {
    32  	if keep, _ := fileutils.Matches(".dockerignore", excludes); keep {
    33  		excludes = append(excludes, "!.dockerignore")
    34  	}
    35  	if keep, _ := fileutils.Matches(dockerfile, excludes); keep && !dockerfileFromStdin {
    36  		excludes = append(excludes, "!"+dockerfile)
    37  	}
    38  	return excludes
    39  }