github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/container_commit.go (about) 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 main 18 19 import ( 20 "github.com/containerd/nerdctl/pkg/api/types" 21 "github.com/containerd/nerdctl/pkg/clientutil" 22 "github.com/containerd/nerdctl/pkg/cmd/container" 23 "github.com/spf13/cobra" 24 ) 25 26 func newCommitCommand() *cobra.Command { 27 var commitCommand = &cobra.Command{ 28 Use: "commit [flags] CONTAINER REPOSITORY[:TAG]", 29 Short: "Create a new image from a container's changes", 30 Args: IsExactArgs(2), 31 RunE: commitAction, 32 ValidArgsFunction: commitShellComplete, 33 SilenceUsage: true, 34 SilenceErrors: true, 35 } 36 commitCommand.Flags().StringP("author", "a", "", `Author (e.g., "nerdctl contributor <nerdctl-dev@example.com>")`) 37 commitCommand.Flags().StringP("message", "m", "", "Commit message") 38 commitCommand.Flags().StringArrayP("change", "c", nil, "Apply Dockerfile instruction to the created image (supported directives: [CMD, ENTRYPOINT])") 39 commitCommand.Flags().BoolP("pause", "p", true, "Pause container during commit") 40 return commitCommand 41 } 42 43 func processCommitCommandOptions(cmd *cobra.Command) (types.ContainerCommitOptions, error) { 44 globalOptions, err := processRootCmdFlags(cmd) 45 if err != nil { 46 return types.ContainerCommitOptions{}, err 47 } 48 author, err := cmd.Flags().GetString("author") 49 if err != nil { 50 return types.ContainerCommitOptions{}, err 51 } 52 message, err := cmd.Flags().GetString("message") 53 if err != nil { 54 return types.ContainerCommitOptions{}, err 55 } 56 pause, err := cmd.Flags().GetBool("pause") 57 if err != nil { 58 return types.ContainerCommitOptions{}, err 59 } 60 61 change, err := cmd.Flags().GetStringArray("change") 62 if err != nil { 63 return types.ContainerCommitOptions{}, err 64 } 65 66 return types.ContainerCommitOptions{ 67 Stdout: cmd.OutOrStdout(), 68 GOptions: globalOptions, 69 Author: author, 70 Message: message, 71 Pause: pause, 72 Change: change, 73 }, nil 74 75 } 76 77 func commitAction(cmd *cobra.Command, args []string) error { 78 options, err := processCommitCommandOptions(cmd) 79 if err != nil { 80 return err 81 } 82 83 client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address) 84 if err != nil { 85 return err 86 } 87 defer cancel() 88 89 return container.Commit(ctx, client, args[1], args[0], options) 90 } 91 92 func commitShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 93 if len(args) == 0 { 94 return shellCompleteContainerNames(cmd, nil) 95 } 96 return nil, cobra.ShellCompDirectiveNoFileComp 97 }