-
Notifications
You must be signed in to change notification settings - Fork 810
Expand file tree
/
Copy pathcommit.go
More file actions
105 lines (91 loc) · 2.61 KB
/
Copy pathcommit.go
File metadata and controls
105 lines (91 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
Copyright (C) nerdctl authors.
Copyright (C) containerd authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"fmt"
"github.com/AkihiroSuda/nerdctl/pkg/idutil/containerwalker"
"github.com/AkihiroSuda/nerdctl/pkg/imgutil/commit"
refdocker "github.com/containerd/containerd/reference/docker"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
)
var (
commitCommand = &cli.Command{
Name: "commit",
Usage: "[flags] CONTAINER REPOSITORY[:TAG]",
Description: "Create a new image from a container's changes",
Action: commitAction,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "author",
Aliases: []string{"a"},
Usage: `Author (e.g., "nerdctl contributor <nerdctl-dev@example.com>")`,
},
&cli.StringFlag{
Name: "message",
Aliases: []string{"m"},
Usage: `Commit message`,
},
},
}
)
func commitAction(clicontext *cli.Context) error {
if clicontext.NArg() != 2 {
return errors.New("need container and commit image name")
}
opts, err := newCommitOpts(clicontext)
if err != nil {
return err
}
client, ctx, cancel, err := newClient(clicontext)
if err != nil {
return err
}
defer cancel()
walker := &containerwalker.ContainerWalker{
Client: client,
OnFound: func(ctx context.Context, found containerwalker.Found) error {
if found.MatchCount > 1 {
return errors.Errorf("ambiguous ID %q", found.Req)
}
imageID, err := commit.Commit(ctx, client, found.Container.ID(), opts)
if err != nil {
return err
}
_, err = fmt.Fprintf(clicontext.App.Writer, "%s\n", imageID)
return err
},
}
req := clicontext.Args().First()
n, err := walker.Walk(ctx, req)
if err != nil {
return err
} else if n == 0 {
return errors.Errorf("no such container %s", req)
}
return nil
}
func newCommitOpts(clicontext *cli.Context) (*commit.Opts, error) {
rawRef := clicontext.Args().Get(1)
named, err := refdocker.ParseDockerRef(rawRef)
if err != nil {
return nil, err
}
return &commit.Opts{
Author: clicontext.String("author"),
Message: clicontext.String("message"),
Ref: named.String(),
}, nil
}