Skip to content

Commit 0c87604

Browse files
authored
Merge pull request #630 from Random-Liu/add-cli-package
Add CLI package for `ctr` to use.
2 parents 582975a + 22b1d52 commit 0c87604

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

cli/cli.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Copyright 2018 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 cli
18+
19+
import (
20+
gocontext "context"
21+
"fmt"
22+
"path/filepath"
23+
24+
api "github.com/containerd/cri/pkg/api/v1"
25+
"github.com/containerd/cri/pkg/client"
26+
"github.com/pkg/errors"
27+
"github.com/urfave/cli"
28+
)
29+
30+
// Command is the cli command for cri plugin.
31+
var Command = cli.Command{
32+
Name: "cri",
33+
Usage: "interact with cri plugin",
34+
Subcommands: cli.Commands{
35+
loadCommand,
36+
},
37+
}
38+
39+
var loadCommand = cli.Command{
40+
Name: "load",
41+
Usage: "load one or more images from tar archives.",
42+
ArgsUsage: "[flags] TAR [TAR, ...]",
43+
Description: "load one or more images from tar archives.",
44+
Flags: []cli.Flag{},
45+
Action: func(context *cli.Context) error {
46+
var (
47+
ctx = gocontext.Background()
48+
address = context.GlobalString("address")
49+
timeout = context.GlobalDuration("timeout")
50+
cancel gocontext.CancelFunc
51+
)
52+
cl, err := client.NewCRIContainerdClient(address, timeout)
53+
if err != nil {
54+
return fmt.Errorf("failed to create grpc client: %v", err)
55+
}
56+
if timeout > 0 {
57+
ctx, cancel = gocontext.WithTimeout(gocontext.Background(), timeout)
58+
} else {
59+
ctx, cancel = gocontext.WithCancel(ctx)
60+
}
61+
defer cancel()
62+
for _, path := range context.Args() {
63+
absPath, err := filepath.Abs(path)
64+
if err != nil {
65+
return errors.Wrap(err, "failed to get absolute path")
66+
}
67+
res, err := cl.LoadImage(ctx, &api.LoadImageRequest{FilePath: absPath})
68+
if err != nil {
69+
return errors.Wrap(err, "failed to load image")
70+
}
71+
images := res.GetImages()
72+
for _, image := range images {
73+
fmt.Println("Loaded image:", image)
74+
}
75+
}
76+
return nil
77+
},
78+
}

0 commit comments

Comments
 (0)