Skip to content

Commit 88a998d

Browse files
authored
Merge pull request #1822 from ESWZY/merge-toml-writing
Merge the same TOML file writing logic
2 parents c3f78c7 + ce6972f commit 88a998d

2 files changed

Lines changed: 31 additions & 69 deletions

File tree

internal/build/container_ops.go

Lines changed: 30 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -207,92 +207,53 @@ 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.Wrapf(err, "marshaling data to %s", dstPath)
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+
}
234232

235-
return ctrClient.CopyToContainer(ctx, containerID, "/", reader, types.CopyToContainerOptions{})
233+
return ctrClient.CopyToContainer(ctx, containerID, "/", reader, types.CopyToContainerOptions{})
234+
}
235+
236+
// WriteProjectMetadata writes a `project-metadata.toml` based on the ProjectMetadata provided to the destination path.
237+
func WriteProjectMetadata(dstPath string, metadata platform.ProjectMetadata, os string) ContainerOperation {
238+
return func(ctrClient DockerClient, ctx context.Context, containerID string, stdout, stderr io.Writer) error {
239+
return writeToml(ctrClient, ctx, metadata, dstPath, containerID, os, stdout, stderr)
236240
}
237241
}
238242

239243
// WriteStackToml writes a `stack.toml` based on the StackMetadata provided to the destination path.
240244
func WriteStackToml(dstPath string, stack builder.StackMetadata, os string) ContainerOperation {
241245
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{})
246+
return writeToml(ctrClient, ctx, stack, dstPath, containerID, os, stdout, stderr)
265247
}
266248
}
267249

268250
// WriteRunToml writes a `run.toml` based on the RunConfig provided to the destination path.
269251
func WriteRunToml(dstPath string, runImages []builder.RunImageMetadata, os string) ContainerOperation {
252+
runImageData := builder.RunImages{
253+
Images: runImages,
254+
}
270255
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{})
256+
return writeToml(ctrClient, ctx, runImageData, dstPath, containerID, os, stdout, stderr)
296257
}
297258
}
298259

internal/build/container_ops_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ drwxrwxrwx 2 123 456 (.*) some-vol
638638
`)
639639
})
640640
})
641+
641642
when("#EnsureVolumeAccess", func() {
642643
it("changes owner of volume", func() {
643644
h.SkipIf(t, osType != "windows", "no-op for linux")

0 commit comments

Comments
 (0)