Skip to content

Commit fa64898

Browse files
authored
Merge pull request #3961 from weiyuhang2011/fix-inspect
fix: inspect should return one array rather than a stream of array
2 parents c02abf5 + a46b567 commit fa64898

File tree

6 files changed

+106
-25
lines changed

6 files changed

+106
-25
lines changed

cmd/nerdctl/container/container_inspect.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@ import (
2121

2222
"github.com/spf13/cobra"
2323

24+
"github.com/containerd/log"
25+
2426
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
2527
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
2628
"github.com/containerd/nerdctl/v2/pkg/api/types"
2729
"github.com/containerd/nerdctl/v2/pkg/clientutil"
2830
"github.com/containerd/nerdctl/v2/pkg/cmd/container"
31+
"github.com/containerd/nerdctl/v2/pkg/formatter"
2932
)
3033

3134
func inspectCommand() *cobra.Command {
32-
var cmd = &cobra.Command{
35+
cmd := &cobra.Command{
3336
Use: "inspect [flags] CONTAINER [CONTAINER, ...]",
3437
Short: "Display detailed information on one or more containers.",
3538
Long: "Hint: set `--mode=native` for showing the full output",
@@ -100,7 +103,18 @@ func inspectAction(cmd *cobra.Command, args []string) error {
100103
}
101104
defer cancel()
102105

103-
return container.Inspect(ctx, client, args, opt)
106+
entries, err := container.Inspect(ctx, client, args, opt)
107+
if err != nil {
108+
return err
109+
}
110+
111+
// Display
112+
if len(entries) > 0 {
113+
if formatErr := formatter.FormatSlice(opt.Format, opt.Stdout, entries); formatErr != nil {
114+
log.G(ctx).Error(formatErr)
115+
}
116+
}
117+
return err
104118
}
105119

106120
func containerInspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {

cmd/nerdctl/image/image_inspect.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ import (
2121

2222
"github.com/spf13/cobra"
2323

24+
"github.com/containerd/log"
25+
2426
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
2527
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
2628
"github.com/containerd/nerdctl/v2/pkg/api/types"
2729
"github.com/containerd/nerdctl/v2/pkg/clientutil"
2830
"github.com/containerd/nerdctl/v2/pkg/cmd/image"
31+
"github.com/containerd/nerdctl/v2/pkg/formatter"
2932
)
3033

3134
func inspectCommand() *cobra.Command {
@@ -102,7 +105,18 @@ func imageInspectAction(cmd *cobra.Command, args []string) error {
102105
}
103106
defer cancel()
104107

105-
return image.Inspect(ctx, client, args, options)
108+
entries, err := image.Inspect(ctx, client, args, options)
109+
if err != nil {
110+
return err
111+
}
112+
113+
// Display
114+
if len(entries) > 0 {
115+
if formatErr := formatter.FormatSlice(options.Format, options.Stdout, entries); formatErr != nil {
116+
log.G(ctx).Error(formatErr)
117+
}
118+
}
119+
return err
106120
}
107121

108122
func imageInspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {

cmd/nerdctl/inspect/inspect.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222

2323
"github.com/spf13/cobra"
2424

25+
"github.com/containerd/log"
26+
2527
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
2628
containercmd "github.com/containerd/nerdctl/v2/cmd/nerdctl/container"
2729
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
@@ -30,12 +32,13 @@ import (
3032
"github.com/containerd/nerdctl/v2/pkg/clientutil"
3133
"github.com/containerd/nerdctl/v2/pkg/cmd/container"
3234
"github.com/containerd/nerdctl/v2/pkg/cmd/image"
35+
"github.com/containerd/nerdctl/v2/pkg/formatter"
3336
"github.com/containerd/nerdctl/v2/pkg/idutil/containerwalker"
3437
"github.com/containerd/nerdctl/v2/pkg/idutil/imagewalker"
3538
)
3639

3740
func Command() *cobra.Command {
38-
var cmd = &cobra.Command{
41+
cmd := &cobra.Command{
3942
Use: "inspect",
4043
Short: "Return low-level information on objects.",
4144
Args: cobra.MinimumNArgs(1),
@@ -79,6 +82,10 @@ func inspectAction(cmd *cobra.Command, args []string) error {
7982
}
8083
namespace := globalOptions.Namespace
8184
address := globalOptions.Address
85+
format, err := cmd.Flags().GetString("format")
86+
if err != nil {
87+
return err
88+
}
8289
inspectType, err := cmd.Flags().GetString("type")
8390
if err != nil {
8491
return err
@@ -130,6 +137,7 @@ func inspectAction(cmd *cobra.Command, args []string) error {
130137
}
131138

132139
var errs []error
140+
var entries []interface{}
133141
for _, req := range args {
134142
var ni int
135143
var nc int
@@ -150,12 +158,16 @@ func inspectAction(cmd *cobra.Command, args []string) error {
150158
if ni == 0 && nc == 0 {
151159
errs = append(errs, fmt.Errorf("no such object %s", req))
152160
} else if ni > 0 {
153-
if err := image.Inspect(ctx, client, []string{req}, imageInspectOptions); err != nil {
161+
if imageEntries, err := image.Inspect(ctx, client, []string{req}, imageInspectOptions); err != nil {
154162
errs = append(errs, err)
163+
} else {
164+
entries = append(entries, imageEntries...)
155165
}
156166
} else if nc > 0 {
157-
if err := container.Inspect(ctx, client, []string{req}, containerInspectOptions); err != nil {
167+
if containerEntries, err := container.Inspect(ctx, client, []string{req}, containerInspectOptions); err != nil {
158168
errs = append(errs, err)
169+
} else {
170+
entries = append(entries, containerEntries...)
159171
}
160172
}
161173
}
@@ -164,6 +176,10 @@ func inspectAction(cmd *cobra.Command, args []string) error {
164176
return fmt.Errorf("%d errors: %v", len(errs), errs)
165177
}
166178

179+
if formatErr := formatter.FormatSlice(format, cmd.OutOrStdout(), entries); formatErr != nil {
180+
log.G(ctx).Error(formatErr)
181+
}
182+
167183
return nil
168184
}
169185

cmd/nerdctl/inspect/inspect_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,60 @@
1717
package inspect
1818

1919
import (
20+
"encoding/json"
2021
"testing"
2122

23+
"gotest.tools/v3/assert"
24+
25+
"github.com/containerd/nerdctl/mod/tigron/test"
26+
27+
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
2228
"github.com/containerd/nerdctl/v2/pkg/testutil"
29+
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
2330
)
2431

2532
func TestMain(m *testing.M) {
2633
testutil.M(m)
2734
}
35+
36+
func TestInspectSimpleCase(t *testing.T) {
37+
nerdtest.Setup()
38+
testCase := &test.Case{
39+
Description: "inspect container and image return one single json array",
40+
Setup: func(data test.Data, helpers test.Helpers) {
41+
identifier := data.Identifier()
42+
helpers.Ensure("run", "-d", "--quiet", "--name", identifier, testutil.CommonImage, "sleep", nerdtest.Infinity)
43+
},
44+
Cleanup: func(data test.Data, helpers test.Helpers) {
45+
identifier := data.Identifier()
46+
helpers.Anyhow("rm", "-f", identifier)
47+
},
48+
Command: func(data test.Data, helpers test.Helpers) test.TestableCommand {
49+
return helpers.Command("inspect", testutil.CommonImage, data.Identifier())
50+
},
51+
Expected: func(data test.Data, helpers test.Helpers) *test.Expected {
52+
return &test.Expected{
53+
Output: func(stdout string, info string, t *testing.T) {
54+
var inspectResult []json.RawMessage
55+
err := json.Unmarshal([]byte(stdout), &inspectResult)
56+
assert.NilError(t, err, "Unable to unmarshal output\n"+info)
57+
assert.Equal(t, len(inspectResult), 2, "Unexpectedly got multiple results\n"+info)
58+
59+
var dci dockercompat.Image
60+
err = json.Unmarshal(inspectResult[0], &dci)
61+
assert.NilError(t, err, "Unable to unmarshal output\n"+info)
62+
inspecti := nerdtest.InspectImage(helpers, testutil.CommonImage)
63+
assert.Equal(t, dci.ID, inspecti.ID, info)
64+
65+
var dcc dockercompat.Container
66+
err = json.Unmarshal(inspectResult[1], &dcc)
67+
assert.NilError(t, err, "Unable to unmarshal output\n"+info)
68+
inspectc := nerdtest.InspectContainer(helpers, data.Identifier())
69+
assert.Assert(t, dcc.ID == inspectc.ID, info)
70+
},
71+
}
72+
},
73+
}
74+
75+
testCase.Run(t)
76+
}

pkg/cmd/container/inspect.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,17 @@ import (
2323

2424
containerd "github.com/containerd/containerd/v2/client"
2525
"github.com/containerd/containerd/v2/core/snapshots"
26-
"github.com/containerd/log"
2726

2827
"github.com/containerd/nerdctl/v2/pkg/api/types"
2928
"github.com/containerd/nerdctl/v2/pkg/containerdutil"
3029
"github.com/containerd/nerdctl/v2/pkg/containerinspector"
31-
"github.com/containerd/nerdctl/v2/pkg/formatter"
3230
"github.com/containerd/nerdctl/v2/pkg/idutil/containerwalker"
3331
"github.com/containerd/nerdctl/v2/pkg/imgutil"
3432
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
3533
)
3634

3735
// Inspect prints detailed information for each container in `containers`.
38-
func Inspect(ctx context.Context, client *containerd.Client, containers []string, options types.ContainerInspectOptions) error {
36+
func Inspect(ctx context.Context, client *containerd.Client, containers []string, options types.ContainerInspectOptions) ([]any, error) {
3937
f := &containerInspector{
4038
mode: options.Mode,
4139
size: options.Size,
@@ -48,13 +46,11 @@ func Inspect(ctx context.Context, client *containerd.Client, containers []string
4846
}
4947

5048
err := walker.WalkAll(ctx, containers, true)
51-
if len(f.entries) > 0 {
52-
if formatErr := formatter.FormatSlice(options.Format, options.Stdout, f.entries); formatErr != nil {
53-
log.L.Error(formatErr)
54-
}
49+
if err != nil {
50+
return []any{}, err
5551
}
5652

57-
return err
53+
return f.entries, nil
5854
}
5955

6056
type containerInspector struct {

pkg/cmd/image/inspect.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030

3131
"github.com/containerd/nerdctl/v2/pkg/api/types"
3232
"github.com/containerd/nerdctl/v2/pkg/containerdutil"
33-
"github.com/containerd/nerdctl/v2/pkg/formatter"
3433
"github.com/containerd/nerdctl/v2/pkg/imageinspector"
3534
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
3635
"github.com/containerd/nerdctl/v2/pkg/referenceutil"
@@ -87,7 +86,7 @@ func inspectIdentifier(ctx context.Context, client *containerd.Client, identifie
8786
}
8887

8988
// Inspect prints detailed information of each image in `images`.
90-
func Inspect(ctx context.Context, client *containerd.Client, identifiers []string, options types.ImageInspectOptions) error {
89+
func Inspect(ctx context.Context, client *containerd.Client, identifiers []string, options types.ImageInspectOptions) ([]any, error) {
9190
// Set a timeout
9291
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
9392
defer cancel()
@@ -189,16 +188,9 @@ func Inspect(ctx context.Context, client *containerd.Client, identifiers []strin
189188
}
190189
}
191190

192-
// Display
193-
if len(entries) > 0 {
194-
if formatErr := formatter.FormatSlice(options.Format, options.Stdout, entries); formatErr != nil {
195-
log.G(ctx).Error(formatErr)
196-
}
197-
}
198-
199191
if len(errs) > 0 {
200-
return fmt.Errorf("%d errors:\n%w", len(errs), errors.Join(errs...))
192+
return []any{}, fmt.Errorf("%d errors:\n%w", len(errs), errors.Join(errs...))
201193
}
202194

203-
return nil
195+
return entries, nil
204196
}

0 commit comments

Comments
 (0)