commit.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "context"
  16. "github.com/docker/cli/cli/command"
  17. "github.com/docker/cli/opts"
  18. "github.com/docker/compose/v2/pkg/api"
  19. "github.com/spf13/cobra"
  20. )
  21. type commitOptions struct {
  22. *ProjectOptions
  23. service string
  24. reference string
  25. pause bool
  26. comment string
  27. author string
  28. changes opts.ListOpts
  29. index int
  30. }
  31. func commitCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *cobra.Command {
  32. options := commitOptions{
  33. ProjectOptions: p,
  34. }
  35. cmd := &cobra.Command{
  36. Use: "commit [OPTIONS] SERVICE [REPOSITORY[:TAG]]",
  37. Short: "Create a new image from a service container's changes",
  38. Args: cobra.RangeArgs(1, 2),
  39. PreRunE: Adapt(func(ctx context.Context, args []string) error {
  40. options.service = args[0]
  41. if len(args) > 1 {
  42. options.reference = args[1]
  43. }
  44. return nil
  45. }),
  46. RunE: Adapt(func(ctx context.Context, args []string) error {
  47. return runCommit(ctx, dockerCli, backend, options)
  48. }),
  49. ValidArgsFunction: completeServiceNames(dockerCli, p),
  50. }
  51. flags := cmd.Flags()
  52. flags.IntVar(&options.index, "index", 0, "index of the container if service has multiple replicas.")
  53. flags.BoolVarP(&options.pause, "pause", "p", true, "Pause container during commit")
  54. flags.StringVarP(&options.comment, "message", "m", "", "Commit message")
  55. flags.StringVarP(&options.author, "author", "a", "", `Author (e.g., "John Hannibal Smith <[email protected]>")`)
  56. options.changes = opts.NewListOpts(nil)
  57. flags.VarP(&options.changes, "change", "c", "Apply Dockerfile instruction to the created image")
  58. return cmd
  59. }
  60. func runCommit(ctx context.Context, dockerCli command.Cli, backend api.Service, options commitOptions) error {
  61. projectName, err := options.toProjectName(ctx, dockerCli)
  62. if err != nil {
  63. return err
  64. }
  65. commitOptions := api.CommitOptions{
  66. Service: options.service,
  67. Reference: options.reference,
  68. Pause: options.pause,
  69. Comment: options.comment,
  70. Author: options.author,
  71. Changes: options.changes,
  72. Index: options.index,
  73. }
  74. return backend.Commit(ctx, projectName, commitOptions)
  75. }