Skip to content

Commit d78e829

Browse files
committed
Merge the same TOML file writing logic
Signed-off-by: Woa <me@wuzy.cn>
1 parent 52902b0 commit d78e829

2 files changed

Lines changed: 131 additions & 69 deletions

File tree

internal/build/container_ops.go

Lines changed: 37 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -207,92 +207,60 @@ func findMount(info types.ContainerJSON, dst string) (types.MountPoint, error) {
207207
return types.MountPoint{}, fmt.Errorf("no matching mount found for %s", dst)
208208
}
209209

210-
// WriteProjectMetadata
211-
func WriteProjectMetadata(p string, metadata platform.ProjectMetadata, os string) ContainerOperation {
212-
return func(ctrClient DockerClient, ctx context.Context, containerID string, stdout, stderr io.Writer) error {
213-
buf := &bytes.Buffer{}
214-
err := toml.NewEncoder(buf).Encode(metadata)
215-
if err != nil {
216-
return errors.Wrap(err, "marshaling project metadata")
217-
}
210+
func writeToml(ctrClient DockerClient, ctx context.Context, data interface{}, dstPath string, containerID string, os string, stdout, stderr io.Writer) error {
211+
buf := &bytes.Buffer{}
212+
err := toml.NewEncoder(buf).Encode(data)
213+
if err != nil {
214+
return errors.Wrap(err, "marshaling data")
215+
}
218216

219-
tarBuilder := archive.TarBuilder{}
217+
tarBuilder := archive.TarBuilder{}
220218

221-
tarPath := p
222-
if os == "windows" {
223-
tarPath = paths.WindowsToSlash(p)
224-
}
219+
tarPath := dstPath
220+
if os == "windows" {
221+
tarPath = paths.WindowsToSlash(dstPath)
222+
}
225223

226-
tarBuilder.AddFile(tarPath, 0755, archive.NormalizedDateTime, buf.Bytes())
227-
reader := tarBuilder.Reader(archive.DefaultTarWriterFactory())
228-
defer reader.Close()
224+
tarBuilder.AddFile(tarPath, 0755, archive.NormalizedDateTime, buf.Bytes())
225+
reader := tarBuilder.Reader(archive.DefaultTarWriterFactory())
226+
defer reader.Close()
229227

230-
if os == "windows" {
231-
dirName := paths.WindowsDir(p)
232-
return copyDirWindows(ctx, ctrClient, containerID, reader, dirName, stdout, stderr)
233-
}
228+
if os == "windows" {
229+
dirName := paths.WindowsDir(dstPath)
230+
return copyDirWindows(ctx, ctrClient, containerID, reader, dirName, stdout, stderr)
231+
}
232+
233+
return ctrClient.CopyToContainer(ctx, containerID, "/", reader, types.CopyToContainerOptions{})
234+
}
234235

235-
return ctrClient.CopyToContainer(ctx, containerID, "/", reader, types.CopyToContainerOptions{})
236+
// WriteToml writes a `data.toml` for test only.
237+
func WriteToml(dstPath string, data interface{}, os string) ContainerOperation {
238+
return func(ctrClient DockerClient, ctx context.Context, containerID string, stdout, stderr io.Writer) error {
239+
return writeToml(ctrClient, ctx, data, dstPath, containerID, os, stdout, stderr)
240+
}
241+
}
242+
243+
// WriteProjectMetadata writes a `project-metadata.toml` based on the ProjectMetadata provided to the destination path.
244+
func WriteProjectMetadata(dstPath string, metadata platform.ProjectMetadata, os string) ContainerOperation {
245+
return func(ctrClient DockerClient, ctx context.Context, containerID string, stdout, stderr io.Writer) error {
246+
return writeToml(ctrClient, ctx, metadata, dstPath, containerID, os, stdout, stderr)
236247
}
237248
}
238249

239250
// WriteStackToml writes a `stack.toml` based on the StackMetadata provided to the destination path.
240251
func WriteStackToml(dstPath string, stack builder.StackMetadata, os string) ContainerOperation {
241252
return func(ctrClient DockerClient, ctx context.Context, containerID string, stdout, stderr io.Writer) error {
242-
buf := &bytes.Buffer{}
243-
err := toml.NewEncoder(buf).Encode(stack)
244-
if err != nil {
245-
return errors.Wrap(err, "marshaling stack metadata")
246-
}
247-
248-
tarBuilder := archive.TarBuilder{}
249-
250-
tarPath := dstPath
251-
if os == "windows" {
252-
tarPath = paths.WindowsToSlash(dstPath)
253-
}
254-
255-
tarBuilder.AddFile(tarPath, 0755, archive.NormalizedDateTime, buf.Bytes())
256-
reader := tarBuilder.Reader(archive.DefaultTarWriterFactory())
257-
defer reader.Close()
258-
259-
if os == "windows" {
260-
dirName := paths.WindowsDir(dstPath)
261-
return copyDirWindows(ctx, ctrClient, containerID, reader, dirName, stdout, stderr)
262-
}
263-
264-
return ctrClient.CopyToContainer(ctx, containerID, "/", reader, types.CopyToContainerOptions{})
253+
return writeToml(ctrClient, ctx, stack, dstPath, containerID, os, stdout, stderr)
265254
}
266255
}
267256

268257
// WriteRunToml writes a `run.toml` based on the RunConfig provided to the destination path.
269258
func WriteRunToml(dstPath string, runImages []builder.RunImageMetadata, os string) ContainerOperation {
259+
runImageData := builder.RunImages{
260+
Images: runImages,
261+
}
270262
return func(ctrClient DockerClient, ctx context.Context, containerID string, stdout, stderr io.Writer) error {
271-
buf := &bytes.Buffer{}
272-
err := toml.NewEncoder(buf).Encode(builder.RunImages{
273-
Images: runImages,
274-
})
275-
if err != nil {
276-
return errors.Wrap(err, "marshaling run metadata")
277-
}
278-
279-
tarBuilder := archive.TarBuilder{}
280-
281-
tarPath := dstPath
282-
if os == "windows" {
283-
tarPath = paths.WindowsToSlash(dstPath)
284-
}
285-
286-
tarBuilder.AddFile(tarPath, 0755, archive.NormalizedDateTime, buf.Bytes())
287-
reader := tarBuilder.Reader(archive.DefaultTarWriterFactory())
288-
defer reader.Close()
289-
290-
if os == "windows" {
291-
dirName := paths.WindowsDir(dstPath)
292-
return copyDirWindows(ctx, ctrClient, containerID, reader, dirName, stdout, stderr)
293-
}
294-
295-
return ctrClient.CopyToContainer(ctx, containerID, "/", reader, types.CopyToContainerOptions{})
263+
return writeToml(ctrClient, ctx, runImageData, dstPath, containerID, os, stdout, stderr)
296264
}
297265
}
298266

internal/build/container_ops_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,100 @@ drwxr-xr-x 2 123 456 (.*) some-vol
646646
`)
647647
})
648648
})
649+
650+
when("#WriteToml", func() {
651+
it("writes file", func() {
652+
containerDir := "/layers-vol"
653+
p := "/layers-vol/data.toml"
654+
if osType == "windows" {
655+
containerDir = `c:\layers-vol`
656+
p = `c:\layers-vol\data.toml`
657+
}
658+
659+
ctrCmd := []string{"ls", "-al", "/layers-vol/data.toml"}
660+
if osType == "windows" {
661+
ctrCmd = []string{"cmd", "/c", `dir /q /n c:\layers-vol\data.toml`}
662+
}
663+
ctx := context.Background()
664+
ctr, err := createContainer(ctx, imageName, containerDir, osType, ctrCmd...)
665+
h.AssertNil(t, err)
666+
defer cleanupContainer(ctx, ctr.ID)
667+
668+
writeOp := build.WriteToml(p, platform.ProjectMetadata{
669+
Source: &platform.ProjectSource{
670+
Type: "project",
671+
Version: map[string]interface{}{
672+
"declared": "1.0.2",
673+
},
674+
Metadata: map[string]interface{}{
675+
"url": "https://github.com/buildpacks/pack",
676+
},
677+
},
678+
}, osType)
679+
680+
var outBuf, errBuf bytes.Buffer
681+
err = writeOp(ctrClient, ctx, ctr.ID, &outBuf, &errBuf)
682+
h.AssertNil(t, err)
683+
684+
err = container.RunWithHandler(ctx, ctrClient, ctr.ID, container.DefaultHandler(&outBuf, &errBuf))
685+
h.AssertNil(t, err)
686+
687+
h.AssertEq(t, errBuf.String(), "")
688+
if osType == "windows" {
689+
h.AssertContains(t, outBuf.String(), `01/01/1980 12:00 AM 137 ... data.toml`)
690+
} else {
691+
h.AssertContains(t, outBuf.String(), `-rwxr-xr-x 1 root root 137 Jan 1 1980 /layers-vol/data.toml`)
692+
}
693+
})
694+
695+
it("has expected contents", func() {
696+
containerDir := "/layers-vol"
697+
p := "/layers-vol/data.toml"
698+
if osType == "windows" {
699+
containerDir = `c:\layers-vol`
700+
p = `c:\layers-vol\data.toml`
701+
}
702+
703+
ctrCmd := []string{"cat", "/layers-vol/data.toml"}
704+
if osType == "windows" {
705+
ctrCmd = []string{"cmd", "/c", `type c:\layers-vol\data.toml`}
706+
}
707+
708+
ctx := context.Background()
709+
ctr, err := createContainer(ctx, imageName, containerDir, osType, ctrCmd...)
710+
h.AssertNil(t, err)
711+
defer cleanupContainer(ctx, ctr.ID)
712+
713+
writeOp := build.WriteProjectMetadata(p, platform.ProjectMetadata{
714+
Source: &platform.ProjectSource{
715+
Type: "project",
716+
Version: map[string]interface{}{
717+
"declared": "1.0.2",
718+
},
719+
Metadata: map[string]interface{}{
720+
"url": "https://github.com/buildpacks/pack",
721+
},
722+
},
723+
}, osType)
724+
725+
var outBuf, errBuf bytes.Buffer
726+
err = writeOp(ctrClient, ctx, ctr.ID, &outBuf, &errBuf)
727+
h.AssertNil(t, err)
728+
729+
err = container.RunWithHandler(ctx, ctrClient, ctr.ID, container.DefaultHandler(&outBuf, &errBuf))
730+
h.AssertEq(t, errBuf.String(), "")
731+
h.AssertNil(t, err)
732+
733+
h.AssertContains(t, outBuf.String(), `[source]
734+
type = "project"
735+
[source.version]
736+
declared = "1.0.2"
737+
[source.metadata]
738+
url = "https://github.com/buildpacks/pack"
739+
`)
740+
})
741+
})
742+
649743
when("#EnsureVolumeAccess", func() {
650744
it("changes owner of volume", func() {
651745
h.SkipIf(t, osType != "windows", "no-op for linux")

0 commit comments

Comments
 (0)