dockerignore.go 1.6 KB

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