2015-09-06 13:26:40 -04:00
|
|
|
package builder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
2015-12-14 05:15:00 -05:00
|
|
|
"github.com/docker/docker/builder/dockerignore"
|
2015-09-06 13:26:40 -04:00
|
|
|
"github.com/docker/docker/pkg/fileutils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DockerIgnoreContext wraps a ModifiableContext to add a method
|
|
|
|
// for handling the .dockerignore file at the root of the context.
|
|
|
|
type DockerIgnoreContext struct {
|
|
|
|
ModifiableContext
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process reads the .dockerignore file at the root of the embedded context.
|
|
|
|
// If .dockerignore does not exist in the context, then nil is returned.
|
|
|
|
//
|
|
|
|
// It can take a list of files to be removed after .dockerignore is removed.
|
|
|
|
// This is used for server-side implementations of builders that need to send
|
|
|
|
// the .dockerignore file as well as the special files specified in filesToRemove,
|
|
|
|
// but expect them to be excluded from the context after they were processed.
|
|
|
|
//
|
|
|
|
// For example, server-side Dockerfile builders are expected to pass in the name
|
|
|
|
// of the Dockerfile to be removed after it was parsed.
|
|
|
|
//
|
|
|
|
// TODO: Don't require a ModifiableContext (use Context instead) and don't remove
|
|
|
|
// files, instead handle a list of files to be excluded from the context.
|
|
|
|
func (c DockerIgnoreContext) Process(filesToRemove []string) error {
|
2015-12-14 05:15:00 -05:00
|
|
|
f, err := c.Open(".dockerignore")
|
2015-09-06 13:26:40 -04:00
|
|
|
// Note that a missing .dockerignore file isn't treated as an error
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2015-12-14 05:15:00 -05:00
|
|
|
excludes, _ := dockerignore.ReadAll(f)
|
2016-06-24 23:57:21 -04:00
|
|
|
f.Close()
|
2015-09-06 13:26:40 -04:00
|
|
|
filesToRemove = append([]string{".dockerignore"}, filesToRemove...)
|
|
|
|
for _, fileToRemove := range filesToRemove {
|
|
|
|
rm, _ := fileutils.Matches(fileToRemove, excludes)
|
|
|
|
if rm {
|
|
|
|
c.Remove(fileToRemove)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|