github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/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  	filesToRemove = append([]string{".dockerignore"}, filesToRemove...)
    40  	for _, fileToRemove := range filesToRemove {
    41  		rm, _ := fileutils.Matches(fileToRemove, excludes)
    42  		if rm {
    43  			c.Remove(fileToRemove)
    44  		}
    45  	}
    46  	return nil
    47  }