github.com/skf/moby@v1.13.1/builder/dockerignore.go (about)

     1  package builder
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/docker/docker/builder/dockerignore"
     7  	"github.com/docker/docker/pkg/fileutils"
     8  )
     9  
    10  // DockerIgnoreContext wraps a ModifiableContext to add a method
    11  // for handling the .dockerignore file at the root of the context.
    12  type DockerIgnoreContext struct {
    13  	ModifiableContext
    14  }
    15  
    16  // Process reads the .dockerignore file at the root of the embedded context.
    17  // If .dockerignore does not exist in the context, then nil is returned.
    18  //
    19  // It can take a list of files to be removed after .dockerignore is removed.
    20  // This is used for server-side implementations of builders that need to send
    21  // the .dockerignore file as well as the special files specified in filesToRemove,
    22  // but expect them to be excluded from the context after they were processed.
    23  //
    24  // For example, server-side Dockerfile builders are expected to pass in the name
    25  // of the Dockerfile to be removed after it was parsed.
    26  //
    27  // TODO: Don't require a ModifiableContext (use Context instead) and don't remove
    28  // files, instead handle a list of files to be excluded from the context.
    29  func (c DockerIgnoreContext) Process(filesToRemove []string) error {
    30  	f, err := c.Open(".dockerignore")
    31  	// Note that a missing .dockerignore file isn't treated as an error
    32  	if err != nil {
    33  		if os.IsNotExist(err) {
    34  			return nil
    35  		}
    36  		return err
    37  	}
    38  	excludes, _ := dockerignore.ReadAll(f)
    39  	f.Close()
    40  	filesToRemove = append([]string{".dockerignore"}, filesToRemove...)
    41  	for _, fileToRemove := range filesToRemove {
    42  		rm, _ := fileutils.Matches(fileToRemove, excludes)
    43  		if rm {
    44  			c.Remove(fileToRemove)
    45  		}
    46  	}
    47  	return nil
    48  }