|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package image |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/spf13/cobra" |
| 23 | + |
| 24 | + "github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" |
| 25 | + "github.com/containerd/nerdctl/v2/pkg/api/types" |
| 26 | + "github.com/containerd/nerdctl/v2/pkg/clientutil" |
| 27 | + "github.com/containerd/nerdctl/v2/pkg/cmd/image" |
| 28 | +) |
| 29 | + |
| 30 | +func addSquashFlags(cmd *cobra.Command) { |
| 31 | + cmd.Flags().IntP("layer-count", "c", 0, "The number of layers that can be compressed") |
| 32 | + cmd.Flags().StringP("layer-digest", "d", "", "The digest of the layer to be compressed") |
| 33 | + cmd. Flags(). StringP( "author", "a", "", `Author (e.g., "nerdctl contributor <[email protected]>")`) |
| 34 | + cmd.Flags().StringP("message", "m", "", "Commit message") |
| 35 | +} |
| 36 | + |
| 37 | +func NewSquashCommand() *cobra.Command { |
| 38 | + var squashCommand = &cobra.Command{ |
| 39 | + Use: "squash [flags] SOURCE_IMAGE TAG_IMAGE", |
| 40 | + Short: "Compress the number of layers of the image", |
| 41 | + Args: helpers.IsExactArgs(2), |
| 42 | + RunE: squashAction, |
| 43 | + SilenceUsage: true, |
| 44 | + SilenceErrors: true, |
| 45 | + } |
| 46 | + addSquashFlags(squashCommand) |
| 47 | + return squashCommand |
| 48 | +} |
| 49 | + |
| 50 | +func processSquashCommandFlags(cmd *cobra.Command, args []string) (options types.ImageSquashOptions, err error) { |
| 51 | + globalOptions, err := helpers.ProcessRootCmdFlags(cmd) |
| 52 | + if err != nil { |
| 53 | + return options, err |
| 54 | + } |
| 55 | + layerCount, err := cmd.Flags().GetInt("layer-count") |
| 56 | + if err != nil { |
| 57 | + return options, err |
| 58 | + } |
| 59 | + layerDigest, err := cmd.Flags().GetString("layer-digest") |
| 60 | + if err != nil { |
| 61 | + return options, err |
| 62 | + } |
| 63 | + author, err := cmd.Flags().GetString("author") |
| 64 | + if err != nil { |
| 65 | + return options, err |
| 66 | + } |
| 67 | + message, err := cmd.Flags().GetString("message") |
| 68 | + if err != nil { |
| 69 | + return options, err |
| 70 | + } |
| 71 | + |
| 72 | + options = types.ImageSquashOptions{ |
| 73 | + GOptions: globalOptions, |
| 74 | + |
| 75 | + Author: author, |
| 76 | + Message: message, |
| 77 | + |
| 78 | + SourceImageRef: args[0], |
| 79 | + TargetImageName: args[1], |
| 80 | + |
| 81 | + SquashLayerCount: layerCount, |
| 82 | + SquashLayerDigest: layerDigest, |
| 83 | + } |
| 84 | + return options, nil |
| 85 | +} |
| 86 | + |
| 87 | +func squashAction(cmd *cobra.Command, args []string) error { |
| 88 | + options, err := processSquashCommandFlags(cmd, args) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + if !options.GOptions.Experimental { |
| 93 | + return fmt.Errorf("squash is an experimental feature, please enable experimental mode") |
| 94 | + } |
| 95 | + client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address) |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + defer cancel() |
| 100 | + |
| 101 | + return image.Squash(ctx, client, options) |
| 102 | +} |
0 commit comments