-
Notifications
You must be signed in to change notification settings - Fork 9
/
image.go
189 lines (162 loc) · 4.43 KB
/
image.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package debug
import (
"archive/tar"
"bytes"
"context"
"fmt"
"github.com/dustin/go-humanize"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/daemon"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/spf13/cobra"
"namespacelabs.dev/foundation/internal/artifacts/oci"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/console"
)
func newImageCmd() *cobra.Command {
var (
insecure bool
contents bool
docker bool
filename string
)
cmd := &cobra.Command{
Use: "get-image",
Short: "Fetches information about an OCI image.",
Args: cobra.ArbitraryArgs,
RunE: fncobra.RunE(func(ctx context.Context, args []string) error {
for _, arg := range args {
if docker {
ref, err := oci.ParseRef(arg, insecure)
if err != nil {
return err
}
img, err := daemon.Image(ref)
if err != nil {
return err
}
if contents {
if err := printContents(ctx, img, filename); err != nil {
return err
}
} else {
if err := printImage(ctx, img); err != nil {
return err
}
}
} else {
d, err := fetchImage(ctx, arg, oci.ResolveOpts{InsecureRegistry: insecure})
if err != nil {
return err
}
if contents {
img, err := d.Image()
if err != nil {
return err
}
if err := printContents(ctx, img, filename); err != nil {
return err
}
} else {
if err := printRemote(ctx, d); err != nil {
return err
}
}
}
}
return nil
}),
}
cmd.Flags().BoolVar(&insecure, "insecure", false, "Set to true to access registries over insecure communications.")
cmd.Flags().BoolVar(&contents, "contents", false, "Set to true to list the contents of the image.")
cmd.Flags().BoolVar(&docker, "docker", docker, "If true, fetch from docker instead of a registry.")
cmd.Flags().StringVar(&filename, "filename", "", "If set, outputs the content of the specified file to stdout.")
return cmd
}
func printRemote(ctx context.Context, d *remote.Descriptor) error {
fmt.Println(d.Digest.String())
fmt.Println(d.MediaType)
img, err := d.Image()
if err != nil {
return err
}
return printImage(ctx, img)
}
func printImage(ctx context.Context, img v1.Image) error {
im, err := img.Manifest()
if err != nil {
return err
}
out := console.Stdout(ctx)
m := im.Config
fmt.Fprintf(out, "Image: %s\n", m.Digest.String())
fmt.Fprintf(out, " size: %v\n", humanize.Bytes(uint64(m.Size)))
fmt.Fprintf(out, " urls: %v\n", m.URLs)
fmt.Fprintf(out, " mediaType: %v\n", m.MediaType)
fmt.Fprintf(out, " annotations: %v\n", m.Annotations)
fmt.Fprintf(out, " platform: %v\n", m.Platform)
layers, err := img.Layers()
if err != nil {
return err
}
var totalSize uint64
for _, layer := range layers {
d, _ := layer.Digest()
size, _ := layer.Size()
mediaType, _ := layer.MediaType()
fmt.Fprintf(out, "\n Layer: %s\n", d)
fmt.Fprintf(out, " size: %v\n", humanize.Bytes(uint64(size)))
fmt.Fprintf(out, " mediaType: %v\n", mediaType)
totalSize += uint64(size)
}
fmt.Fprintf(out, "\n totalSize: %v\n\n", humanize.Bytes(totalSize))
return nil
}
func printContents(ctx context.Context, img v1.Image, filename string) error {
out := console.Stdout(ctx)
if filename != "" {
contents, err := oci.ReadFileFromImage(ctx, img, filename)
if err == nil {
_, _ = console.Stdout(ctx).Write(contents)
}
return err
}
var buf bytes.Buffer
return oci.VisitFilesFromImage(img, func(layer, path string, typ byte, contents []byte) error {
fmt.Fprintf(&buf, "%s: %s", layer, path)
switch typ {
case tar.TypeReg:
fmt.Fprintf(&buf, " (%d bytes)", len(contents))
case tar.TypeSymlink:
fmt.Fprintf(&buf, " --> %s", contents)
default:
fmt.Fprintf(&buf, " (%s)", typName(typ))
}
fmt.Fprintln(&buf)
_, _ = buf.WriteTo(out)
return nil
})
}
func typName(typ byte) string {
switch typ {
case tar.TypeLink:
return "link"
case tar.TypeSymlink:
return "symlink"
case tar.TypeChar:
return "char-device"
case tar.TypeBlock:
return "block-device"
case tar.TypeDir:
return "dir"
case tar.TypeFifo:
return "fifo-file"
case tar.TypeReg:
return "file"
default:
return "unknown"
}
}