/
inspect.go
170 lines (145 loc) · 5.03 KB
/
inspect.go
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
Copyright The 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/containerd/nerdctl/pkg/api/types"
"github.com/containerd/nerdctl/pkg/clientutil"
"github.com/containerd/nerdctl/pkg/cmd/container"
"github.com/containerd/nerdctl/pkg/cmd/image"
"github.com/containerd/nerdctl/pkg/idutil/containerwalker"
"github.com/containerd/nerdctl/pkg/idutil/imagewalker"
"github.com/spf13/cobra"
)
func newInspectCommand() *cobra.Command {
var inspectCommand = &cobra.Command{
Use: "inspect",
Short: "Return low-level information on objects.",
Args: cobra.MinimumNArgs(1),
RunE: inspectAction,
ValidArgsFunction: inspectShellComplete,
SilenceUsage: true,
SilenceErrors: true,
}
addInspectFlags(inspectCommand)
return inspectCommand
}
var validInspectType = map[string]bool{
"container": true,
"image": true,
}
func addInspectFlags(cmd *cobra.Command) {
cmd.Flags().StringP("format", "f", "", "Format the output using the given Go template, e.g, '{{json .}}'")
cmd.RegisterFlagCompletionFunc("format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"json"}, cobra.ShellCompDirectiveNoFileComp
})
cmd.Flags().String("type", "", "Return JSON for specified type")
cmd.RegisterFlagCompletionFunc("type", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"image", "container", ""}, cobra.ShellCompDirectiveNoFileComp
})
cmd.Flags().String("mode", "dockercompat", `Inspect mode, "dockercompat" for Docker-compatible output, "native" for containerd-native output`)
cmd.RegisterFlagCompletionFunc("mode", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"dockercompat", "native"}, cobra.ShellCompDirectiveNoFileComp
})
}
func inspectAction(cmd *cobra.Command, args []string) error {
globalOptions, err := processRootCmdFlags(cmd)
if err != nil {
return err
}
namespace := globalOptions.Namespace
address := globalOptions.Address
inspectType, err := cmd.Flags().GetString("type")
if err != nil {
return err
}
if len(inspectType) > 0 && !validInspectType[inspectType] {
return fmt.Errorf("%q is not a valid value for --type", inspectType)
}
// container and image inspect can share the same client, since no `platform`
// flag will be passed for image inspect.
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), namespace, address)
if err != nil {
return err
}
defer cancel()
imagewalker := &imagewalker.ImageWalker{
Client: client,
OnFound: func(ctx context.Context, found imagewalker.Found) error {
return nil
},
}
containerwalker := &containerwalker.ContainerWalker{
Client: client,
OnFound: func(ctx context.Context, found containerwalker.Found) error {
return nil
},
}
inspectImage := len(inspectType) == 0 || inspectType == "image"
inspectContainer := len(inspectType) == 0 || inspectType == "container"
var imageInspectOptions types.ImageInspectOptions
var containerInspectOptions types.ContainerInspectOptions
if inspectImage {
platform := ""
imageInspectOptions, err = processImageInspectOptions(cmd, &platform)
if err != nil {
return err
}
}
if inspectContainer {
containerInspectOptions, err = processContainerInspectOptions(cmd)
if err != nil {
return err
}
}
var errs []error
for _, req := range args {
var ni int
var nc int
if inspectImage {
ni, err = imagewalker.Walk(ctx, req)
if err != nil {
return err
}
}
if inspectContainer {
nc, err = containerwalker.Walk(ctx, req)
if err != nil {
return err
}
}
if ni == 0 && nc == 0 {
errs = append(errs, fmt.Errorf("no such object %s", req))
} else if ni > 0 {
if err := image.Inspect(ctx, client, []string{req}, imageInspectOptions); err != nil {
errs = append(errs, err)
}
} else if nc > 0 {
if err := container.Inspect(ctx, client, []string{req}, containerInspectOptions); err != nil {
errs = append(errs, err)
}
}
}
if len(errs) > 0 {
return fmt.Errorf("%d errors: %v", len(errs), errs)
}
return nil
}
func inspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// show container names
containers, _ := shellCompleteContainerNames(cmd, nil)
// show image names
images, _ := shellCompleteImageNames(cmd)
return append(containers, images...), cobra.ShellCompDirectiveNoFileComp
}