-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
140 lines (121 loc) · 3.41 KB
/
main.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
package main
import (
"archive/tar"
"compress/gzip"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"time"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
)
func extractLayer(layer v1.Layer, destDir string) error {
layerReader, err := layer.Compressed()
if err != nil {
return fmt.Errorf("failed to get layer: %v", err)
}
defer layerReader.Close()
gzipReader, err := gzip.NewReader(layerReader)
if err != nil {
return fmt.Errorf("failed to create gzip reader: %v", err)
}
defer gzipReader.Close()
tarReader := tar.NewReader(gzipReader)
for {
header, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("error reading tar: %v", err)
}
if header.Typeflag == tar.TypeReg {
targetPath := filepath.Join(destDir, header.Name)
if err := os.MkdirAll(filepath.Dir(targetPath), 0755); err != nil {
return fmt.Errorf("failed to create directory: %v", err)
}
outFile, err := os.Create(targetPath)
if err != nil {
return fmt.Errorf("failed to create file: %v", err)
}
defer outFile.Close()
if _, err := io.Copy(outFile, tarReader); err != nil {
return fmt.Errorf("failed to copy file: %v", err)
}
fmt.Printf("Extracted: %s\n", targetPath)
}
}
return nil
}
func printImageMetadata(img v1.Image) {
digest, err := img.Digest()
if err != nil {
log.Fatalf("Error fetching image digest: %v", err)
}
fmt.Printf("Image Digest: %s\n", digest)
configFile, err := img.ConfigFile()
if err != nil {
log.Fatalf("Error fetching image config file: %v", err)
}
fmt.Printf("Architecture: %s\n", configFile.Architecture)
fmt.Printf("OS: %s\n", configFile.OS)
fmt.Printf("Created: %s\n", configFile.Created.Format(time.RFC3339))
if len(configFile.Config.Env) > 0 {
fmt.Println("Environment Variables:")
for _, env := range configFile.Config.Env {
fmt.Printf(" %s\n", env)
}
} else {
fmt.Println("No environment variables set.")
}
layers, err := img.Layers()
if err != nil {
log.Fatalf("Error fetching image layers: %v", err)
}
for i, layer := range layers {
size, err := layer.Size()
if err != nil {
log.Fatalf("Error fetching layer size: %v", err)
}
diffID, err := layer.DiffID()
if err != nil {
log.Fatalf("Error fetching layer DiffID: %v", err)
}
fmt.Printf("Layer %d - Size: %d bytes, DiffID: %s\n", i+1, size, diffID)
}
}
func main() {
imageRef := flag.String("image", "alpine:latest", "Image reference to fetch")
destDir := flag.String("dest", "./extracted_files", "Destination directory for extracted files")
imgArch := flag.String("arch", "amd64", "Image Architecture to fetch")
imgOs := flag.String("os", "linux", "Image OS to fetch")
flag.Parse()
ref, err := name.ParseReference(*imageRef)
if err != nil {
log.Fatalf("Error parsing image reference: %v", err)
}
img, err := remote.Image(ref, remote.WithPlatform(v1.Platform {
OS: *imgOs,
Architecture: *imgArch,
}))
if err != nil {
log.Fatalf("Error fetching image: %v", err)
}
printImageMetadata(img)
layers, err := img.Layers()
if err != nil {
log.Fatalf("Error getting image layers: %v", err)
}
for i, layer := range layers {
fmt.Printf("Processing Layer %d...\n", i+1)
err := extractLayer(layer, *destDir)
if err != nil {
log.Fatalf("Error extracting layer: %v", err)
}
}
fmt.Println("Extraction complete!")
}