repo.me/x/t2/main.go

62 lines
1.1 KiB
Go
Raw Normal View History

2024-04-15 18:02:54 +08:00
package main
import (
"context"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"io"
"log"
"os/signal"
"syscall"
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
defer cancel()
puller, err := remote.NewPuller()
if err != nil {
log.Panic(1, err)
}
//imageName := "loveuer/hello:v2.0.1"
imageName := "alpine:latest"
tn, err := name.NewTag(imageName)
if err != nil {
log.Panic(2, err)
}
des, err := puller.Get(ctx, tn)
if err != nil {
log.Panic(3, err)
}
img, err := des.Image()
if err != nil {
log.Panic(4, err)
}
lys, err := img.Layers()
if err != nil {
log.Panic(5, err)
}
rs := make([]io.Reader, 0)
for _, item := range lys {
r, err := item.Uncompressed()
if err != nil {
log.Panic(6, err)
}
rs = append(rs, r)
}
allr := io.MultiReader(rs...)
bs, err := io.ReadAll(allr)
if err != nil {
log.Panic(7, err)
}
log.Printf("size: %.2f MB", float64(len(bs))/1024/1024)
}