runSave performs a save against the engine based on the specified options
(ctx context.Context, dockerCLI command.Cli, opts saveOptions)
| 53 | |
| 54 | // runSave performs a save against the engine based on the specified options |
| 55 | func runSave(ctx context.Context, dockerCLI command.Cli, opts saveOptions) error { |
| 56 | var options []client.ImageSaveOption |
| 57 | |
| 58 | platformList := []ocispec.Platform{} |
| 59 | for _, p := range opts.platform { |
| 60 | pp, err := platforms.Parse(p) |
| 61 | if err != nil { |
| 62 | return fmt.Errorf("invalid platform: %w", err) |
| 63 | } |
| 64 | platformList = append(platformList, pp) |
| 65 | } |
| 66 | if len(platformList) > 0 { |
| 67 | options = append(options, client.ImageSaveWithPlatforms(platformList...)) |
| 68 | } |
| 69 | |
| 70 | var output io.Writer |
| 71 | if opts.output == "" { |
| 72 | if dockerCLI.Out().IsTerminal() { |
| 73 | return errors.New("cowardly refusing to save to a terminal. Use the -o flag or redirect") |
| 74 | } |
| 75 | output = dockerCLI.Out() |
| 76 | } else { |
| 77 | writer, err := atomicwriter.New(opts.output, 0o600) |
| 78 | if err != nil { |
| 79 | return fmt.Errorf("failed to save image: %w", err) |
| 80 | } |
| 81 | defer writer.Close() |
| 82 | output = writer |
| 83 | } |
| 84 | |
| 85 | responseBody, err := dockerCLI.Client().ImageSave(ctx, opts.images, options...) |
| 86 | if err != nil { |
| 87 | return err |
| 88 | } |
| 89 | defer responseBody.Close() |
| 90 | |
| 91 | _, err = io.Copy(output, responseBody) |
| 92 | return err |
| 93 | } |
no test coverage detected
searching dependent graphs…