forked from genuinetools/img
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspect.go
79 lines (62 loc) · 1.76 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
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"github.com/containerd/containerd/namespaces"
"github.com/genuinetools/img/client"
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/session"
)
const inspectUsageShortHelp = `Return the JSON-encoded OCI image config. The output format is not compatible with "docker inspect".`
const inspectUsageLongHelp = `Return the JSON-encoded OCI image config. The output format is not compatible with "docker inspect".`
func newInspectCommand() *cobra.Command {
inspect := &inspectCommand{}
cmd := &cobra.Command{
Use: "inspect NAME[:TAG]",
DisableFlagsInUseLine: true,
SilenceUsage: true,
Short: inspectUsageShortHelp,
Long: inspectUsageLongHelp,
Args: inspect.ValidateArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return inspect.Run(args)
},
}
return cmd
}
type inspectCommand struct {
image string
}
func (cmd *inspectCommand) ValidateArgs(c *cobra.Command, args []string) error {
if len(args) < 1 {
return fmt.Errorf("must pass an image to inspect")
}
return nil
}
func (cmd *inspectCommand) Run(args []string) (err error) {
reexec()
// Get the specified image and target.
cmd.image = args[0]
// Create the context.
id := identity.NewID()
ctx := session.NewContext(context.Background(), id)
ctx = namespaces.WithNamespace(ctx, "buildkit")
// Create the client.
c, err := client.New(stateDir, backend, nil)
if err != nil {
return err
}
defer c.Close()
image, err := c.InspectImage(ctx, cmd.image)
if err != nil {
return err
}
fmted, err := json.MarshalIndent(image, "", "\t")
if err != nil {
return err
}
fmt.Println(string(fmted))
return nil
}