1717package main
1818
1919import (
20- "context"
2120 "fmt"
22- "time"
2321
24- "github.com/containerd/nerdctl/pkg/clientutil"
25- "github.com/containerd/nerdctl/pkg/containerinspector"
26- "github.com/containerd/nerdctl/pkg/formatter"
27- "github.com/containerd/nerdctl/pkg/idutil/containerwalker"
28- "github.com/containerd/nerdctl/pkg/inspecttypes/dockercompat"
22+ "github.com/containerd/nerdctl/pkg/api/types"
23+ "github.com/containerd/nerdctl/pkg/cmd/container"
2924
3025 "github.com/spf13/cobra"
3126)
@@ -52,74 +47,33 @@ func newContainerInspectCommand() *cobra.Command {
5247 return containerInspectCommand
5348}
5449
50+ var validModeType = map [string ]bool {
51+ "native" : true ,
52+ "dockercompat" : true ,
53+ }
54+
5555func containerInspectAction (cmd * cobra.Command , args []string ) error {
5656 globalOptions , err := processRootCmdFlags (cmd )
5757 if err != nil {
5858 return err
5959 }
60- client , ctx , cancel , err := clientutil .NewClient (cmd .Context (), globalOptions .Namespace , globalOptions .Address )
61- if err != nil {
62- return err
63- }
64- defer cancel ()
65-
6660 mode , err := cmd .Flags ().GetString ("mode" )
6761 if err != nil {
6862 return err
6963 }
70- f := & containerInspector {
71- mode : mode ,
72- }
73- walker := & containerwalker.ContainerWalker {
74- Client : client ,
75- OnFound : f .Handler ,
76- }
77-
78- var errs []error
79- for _ , req := range args {
80- n , err := walker .Walk (ctx , req )
81- if err != nil {
82- errs = append (errs , err )
83- } else if n == 0 {
84- errs = append (errs , fmt .Errorf ("no such object: %s" , req ))
85- }
86- }
87- if len (errs ) > 0 {
88- return fmt .Errorf ("%d errors: %v" , len (errs ), errs )
64+ if len (mode ) > 0 && ! validModeType [mode ] {
65+ return fmt .Errorf ("%q is not a valid value for --mode" , mode )
8966 }
9067 format , err := cmd .Flags ().GetString ("format" )
9168 if err != nil {
9269 return err
9370 }
94- return formatter .FormatSlice (format , cmd .OutOrStdout (), f .entries )
95- }
96-
97- type containerInspector struct {
98- mode string
99- entries []interface {}
100- }
101-
102- func (x * containerInspector ) Handler (ctx context.Context , found containerwalker.Found ) error {
103- ctx , cancel := context .WithTimeout (ctx , 5 * time .Second )
104- defer cancel ()
105-
106- n , err := containerinspector .Inspect (ctx , found .Container )
107- if err != nil {
108- return err
109- }
110- switch x .mode {
111- case "native" :
112- x .entries = append (x .entries , n )
113- case "dockercompat" :
114- d , err := dockercompat .ContainerFromNative (n )
115- if err != nil {
116- return err
117- }
118- x .entries = append (x .entries , d )
119- default :
120- return fmt .Errorf ("unknown mode %q" , x .mode )
121- }
122- return nil
71+ return container .Inspect (cmd .Context (), types.ContainerInspectCommandOptions {
72+ GOptions : globalOptions ,
73+ Format : format ,
74+ Mode : mode ,
75+ Containers : args ,
76+ }, cmd .OutOrStdout ())
12377}
12478
12579func containerInspectShellComplete (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
0 commit comments