build.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package build
  2. import (
  3. "io"
  4. "strings"
  5. "github.com/docker/docker/integration-cli/cli/build/fakecontext"
  6. "github.com/gotestyourself/gotestyourself/icmd"
  7. )
  8. type testingT interface {
  9. Fatal(args ...interface{})
  10. Fatalf(string, ...interface{})
  11. }
  12. // WithStdinContext sets the build context from the standard input with the specified reader
  13. func WithStdinContext(closer io.ReadCloser) func(*icmd.Cmd) func() {
  14. return func(cmd *icmd.Cmd) func() {
  15. cmd.Command = append(cmd.Command, "-")
  16. cmd.Stdin = closer
  17. return func() {
  18. // FIXME(vdemeester) we should not ignore the error here…
  19. closer.Close()
  20. }
  21. }
  22. }
  23. // WithDockerfile creates / returns a CmdOperator to set the Dockerfile for a build operation
  24. func WithDockerfile(dockerfile string) func(*icmd.Cmd) func() {
  25. return func(cmd *icmd.Cmd) func() {
  26. cmd.Command = append(cmd.Command, "-")
  27. cmd.Stdin = strings.NewReader(dockerfile)
  28. return nil
  29. }
  30. }
  31. // WithoutCache makes the build ignore cache
  32. func WithoutCache(cmd *icmd.Cmd) func() {
  33. cmd.Command = append(cmd.Command, "--no-cache")
  34. return nil
  35. }
  36. // WithContextPath sets the build context path
  37. func WithContextPath(path string) func(*icmd.Cmd) func() {
  38. return func(cmd *icmd.Cmd) func() {
  39. cmd.Command = append(cmd.Command, path)
  40. return nil
  41. }
  42. }
  43. // WithExternalBuildContext use the specified context as build context
  44. func WithExternalBuildContext(ctx *fakecontext.Fake) func(*icmd.Cmd) func() {
  45. return func(cmd *icmd.Cmd) func() {
  46. cmd.Dir = ctx.Dir
  47. cmd.Command = append(cmd.Command, ".")
  48. return nil
  49. }
  50. }
  51. // WithBuildContext sets up the build context
  52. func WithBuildContext(t testingT, contextOperators ...func(*fakecontext.Fake) error) func(*icmd.Cmd) func() {
  53. // FIXME(vdemeester) de-duplicate that
  54. ctx := fakecontext.New(t, "", contextOperators...)
  55. return func(cmd *icmd.Cmd) func() {
  56. cmd.Dir = ctx.Dir
  57. cmd.Command = append(cmd.Command, ".")
  58. return closeBuildContext(t, ctx)
  59. }
  60. }
  61. // WithFile adds the specified file (with content) in the build context
  62. func WithFile(name, content string) func(*fakecontext.Fake) error {
  63. return fakecontext.WithFile(name, content)
  64. }
  65. func closeBuildContext(t testingT, ctx *fakecontext.Fake) func() {
  66. return func() {
  67. if err := ctx.Close(); err != nil {
  68. t.Fatal(err)
  69. }
  70. }
  71. }