Skip to content

Commit 4e9ec42

Browse files
committed
cmd: support nerdctl manifeset inspect
- Add nerdctl manifest inspect to display image manifest details, with optional --verbose output. - Implement manifest parsing, formatting, and related type definitions. - Integrate the new command into the CLI. Signed-off-by: ChengyuZhu6 <[email protected]>
1 parent 38596c8 commit 4e9ec42

File tree

6 files changed

+542
-0
lines changed

6 files changed

+542
-0
lines changed

cmd/nerdctl/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"github.com/containerd/nerdctl/v2/cmd/nerdctl/internal"
4141
"github.com/containerd/nerdctl/v2/cmd/nerdctl/ipfs"
4242
"github.com/containerd/nerdctl/v2/cmd/nerdctl/login"
43+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/manifest"
4344
"github.com/containerd/nerdctl/v2/cmd/nerdctl/namespace"
4445
"github.com/containerd/nerdctl/v2/cmd/nerdctl/network"
4546
"github.com/containerd/nerdctl/v2/cmd/nerdctl/system"
@@ -344,6 +345,9 @@ Config file ($NERDCTL_TOML): %s
344345

345346
// IPFS
346347
ipfs.NewIPFSCommand(),
348+
349+
// Manifest
350+
manifest.Command(),
347351
)
348352
addApparmorCommand(rootCmd)
349353
container.AddCpCommand(rootCmd)

cmd/nerdctl/manifest/manifest.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright 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 manifest
18+
19+
import (
20+
"github.com/spf13/cobra"
21+
22+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
23+
)
24+
25+
func Command() *cobra.Command {
26+
cmd := &cobra.Command{
27+
Annotations: map[string]string{helpers.Category: helpers.Management},
28+
Use: "manifest",
29+
Short: "Manage image manifests.",
30+
RunE: helpers.UnknownSubcommandAction,
31+
SilenceUsage: true,
32+
SilenceErrors: true,
33+
}
34+
35+
cmd.AddCommand(
36+
InspectCommand(),
37+
)
38+
39+
return cmd
40+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
Copyright 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 manifest
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/spf13/cobra"
23+
24+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion"
25+
"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers"
26+
"github.com/containerd/nerdctl/v2/pkg/api/types"
27+
"github.com/containerd/nerdctl/v2/pkg/cmd/manifest"
28+
"github.com/containerd/nerdctl/v2/pkg/formatter"
29+
)
30+
31+
func InspectCommand() *cobra.Command {
32+
var cmd = &cobra.Command{
33+
Use: "inspect MANIFEST",
34+
Short: "Display the contents of a manifest or image index/manifest list",
35+
Args: cobra.MinimumNArgs(1),
36+
RunE: inspectAction,
37+
ValidArgsFunction: inspectShellComplete,
38+
SilenceUsage: true,
39+
SilenceErrors: true,
40+
}
41+
cmd.Flags().Bool("verbose", false, "Verbose output additional info including layers and platform")
42+
cmd.Flags().Bool("insecure", false, "Allow communication with an insecure registry")
43+
return cmd
44+
}
45+
46+
func processInspectFlags(cmd *cobra.Command) (types.ManifestInspectOptions, error) {
47+
globalOptions, err := helpers.ProcessRootCmdFlags(cmd)
48+
if err != nil {
49+
return types.ManifestInspectOptions{}, err
50+
}
51+
verbose, err := cmd.Flags().GetBool("verbose")
52+
if err != nil {
53+
return types.ManifestInspectOptions{}, err
54+
}
55+
insecure, err := cmd.Flags().GetBool("insecure")
56+
if err != nil {
57+
return types.ManifestInspectOptions{}, err
58+
}
59+
return types.ManifestInspectOptions{
60+
Stdout: cmd.OutOrStdout(),
61+
GOptions: globalOptions,
62+
Verbose: verbose,
63+
Insecure: insecure,
64+
}, nil
65+
}
66+
67+
func inspectAction(cmd *cobra.Command, args []string) error {
68+
inspectOptions, err := processInspectFlags(cmd)
69+
if err != nil {
70+
return err
71+
}
72+
rawRef := args[0]
73+
res, err := manifest.Inspect(cmd.Context(), rawRef, inspectOptions)
74+
if err != nil {
75+
return err
76+
}
77+
78+
// Output format: single object for single result, array for multiple results
79+
if len(res) == 1 {
80+
jsonStr, err := formatter.ToJSON(res[0], "", " ")
81+
if err != nil {
82+
return err
83+
}
84+
fmt.Fprint(inspectOptions.Stdout, jsonStr)
85+
} else {
86+
if formatErr := formatter.FormatSlice("", inspectOptions.Stdout, res); formatErr != nil {
87+
return formatErr
88+
}
89+
}
90+
return nil
91+
}
92+
93+
func inspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
94+
return completion.ImageNames(cmd)
95+
}

pkg/api/types/manifest_types.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright 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 types
18+
19+
import "io"
20+
21+
// ManifestInspectOptions specifies options for `nerdctl manifest inspect`.
22+
type ManifestInspectOptions struct {
23+
Stdout io.Writer
24+
GOptions GlobalCommandOptions
25+
// Verbose output additional info including layers and platform
26+
Verbose bool
27+
// Allow communication with an insecure registry
28+
Insecure bool
29+
}

0 commit comments

Comments
 (0)