|
| 1 | +/* |
| 2 | + Copyright 2020 Docker Compose CLI 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 compose |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + |
| 22 | + "github.com/docker/cli/cli/command" |
| 23 | + "github.com/docker/cli/opts" |
| 24 | + "github.com/docker/compose/v2/pkg/api" |
| 25 | + "github.com/spf13/cobra" |
| 26 | +) |
| 27 | + |
| 28 | +type commitOptions struct { |
| 29 | + *ProjectOptions |
| 30 | + |
| 31 | + service string |
| 32 | + reference string |
| 33 | + |
| 34 | + pause bool |
| 35 | + comment string |
| 36 | + author string |
| 37 | + changes opts.ListOpts |
| 38 | + |
| 39 | + index int |
| 40 | +} |
| 41 | + |
| 42 | +func commitCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command { |
| 43 | + options := commitOptions{ |
| 44 | + ProjectOptions: p, |
| 45 | + } |
| 46 | + cmd := &cobra.Command{ |
| 47 | + Use: "commit [OPTIONS] SERVICE [REPOSITORY[:TAG]]", |
| 48 | + Short: "Create a new image from a service container's changes", |
| 49 | + Args: cobra.RangeArgs(1, 2), |
| 50 | + PreRunE: Adapt(func(ctx context.Context, args []string) error { |
| 51 | + options.service = args[0] |
| 52 | + if len(args) > 1 { |
| 53 | + options.reference = args[1] |
| 54 | + } |
| 55 | + |
| 56 | + return nil |
| 57 | + }), |
| 58 | + RunE: Adapt(func(ctx context.Context, args []string) error { |
| 59 | + return runCommit(ctx, dockerCli, backend, options) |
| 60 | + }), |
| 61 | + ValidArgsFunction: completeServiceNames(dockerCli, p), |
| 62 | + } |
| 63 | + |
| 64 | + flags := cmd.Flags() |
| 65 | + flags.IntVar(&options.index, "index", 0, "index of the container if service has multiple replicas.") |
| 66 | + |
| 67 | + flags.BoolVarP(&options.pause, "pause", "p", true, "Pause container during commit") |
| 68 | + flags.StringVarP(&options.comment, "message", "m", "", "Commit message") |
| 69 | + flags. StringVarP( &options. author, "author", "a", "", `Author (e.g., "John Hannibal Smith <[email protected]>")`) |
| 70 | + options.changes = opts.NewListOpts(nil) |
| 71 | + flags.VarP(&options.changes, "change", "c", "Apply Dockerfile instruction to the created image") |
| 72 | + |
| 73 | + return cmd |
| 74 | +} |
| 75 | + |
| 76 | +func runCommit(ctx context.Context, dockerCli command.Cli, backend api.Service, options commitOptions) error { |
| 77 | + projectName, err := options.toProjectName(ctx, dockerCli) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + commitOptions := api.CommitOptions{ |
| 83 | + Service: options.service, |
| 84 | + Reference: options.reference, |
| 85 | + Pause: options.pause, |
| 86 | + Comment: options.comment, |
| 87 | + Author: options.author, |
| 88 | + Changes: options.changes, |
| 89 | + Index: options.index, |
| 90 | + } |
| 91 | + |
| 92 | + return backend.Commit(ctx, projectName, commitOptions) |
| 93 | +} |
0 commit comments