From 209323687c3c4ca5c148553cab8f1d43054aad31 Mon Sep 17 00:00:00 2001 From: Emily Casey Date: Fri, 21 Feb 2025 08:57:54 -0500 Subject: [PATCH] Using stream layer Signed-off-by: Emily Casey --- main.go | 56 +++++++++++++++++++--------------------------- pkg/layer/layer.go | 4 ++++ pkg/utils/utils.go | 43 ++++++++++++++++++----------------- 3 files changed, 50 insertions(+), 53 deletions(-) diff --git a/main.go b/main.go index 8db1153..541dd45 100644 --- a/main.go +++ b/main.go @@ -12,7 +12,9 @@ import ( "github.com/google/go-containerregistry/pkg/v1/empty" "github.com/google/go-containerregistry/pkg/v1/mutate" "github.com/google/go-containerregistry/pkg/v1/remote" + "github.com/google/go-containerregistry/pkg/v1/stream" "github.com/google/go-containerregistry/pkg/v1/types" + "github.com/klauspost/compress/gzip" "github.com/docker/model-distribution/pkg/layer" "github.com/docker/model-distribution/pkg/utils" @@ -30,26 +32,26 @@ func main() { os.Exit(1) } - fmt.Println("1. Creating reference for target image...") + fmt.Println("Creating reference for target image...") ref, err := name.ParseReference(*tag) if err != nil { log.Fatalf("parsing reference: %v", err) } fmt.Printf(" Reference: %s\n", ref.String()) - fmt.Printf("2. Reading from source: %s\n", *source) - fileContent, err := utils.ReadContent(*source) + fmt.Printf("Creating layer from source file: %s\n", *source) + f, err := utils.Open(*source) if err != nil { log.Fatalf("reading content: %v", err) } - fmt.Printf(" Size: %s\n", utils.FormatBytes(len(fileContent))) + defer f.Close() - fmt.Println("3. Creating imgLayer from file content...") - l := layer.New(fileContent) - layerSize, _ := l.Size() - fmt.Printf(" Layer size: %s\n", utils.FormatBytes(int(layerSize))) + l := stream.NewLayer(f, + stream.WithMediaType(layer.MediaTypeGGUF), + stream.WithCompressionLevel(gzip.NoCompression), + ) - fmt.Println("4. Creating empty image with artifact configuration...") + fmt.Println("Creating empty image with artifact configuration...") img := empty.Image configFile := &v1.ConfigFile{ @@ -67,34 +69,13 @@ func main() { img = mutate.MediaType(img, types.OCIManifestSchema1) img = mutate.ConfigMediaType(img, "application/vnd.docker.ai.model.config.v1+json") - fmt.Println("5. Appending imgLayer to image...") + fmt.Println("Appending imgLayer to image...") img, err = mutate.AppendLayers(img, l) if err != nil { log.Fatalf("appending imgLayer: %v", err) } - - fmt.Println("6. Getting manifest details...") - manifest, err := img.Manifest() - if err != nil { - log.Fatalf("getting manifest: %v", err) - } - - fmt.Println("\nManifest details:") - fmt.Printf(" MediaType: %s\n", manifest.MediaType) - fmt.Printf(" Config:") - fmt.Printf(" MediaType: %s\n", manifest.Config.MediaType) - fmt.Printf(" Size: %d bytes\n", manifest.Config.Size) - fmt.Printf(" Digest: %s\n", manifest.Config.Digest) - fmt.Printf(" Layers:\n") - for i, imgLayer := range manifest.Layers { - fmt.Printf(" Layer %d:\n", i+1) - fmt.Printf(" MediaType: %s\n", imgLayer.MediaType) - fmt.Printf(" Size: %d bytes\n", imgLayer.Size) - fmt.Printf(" Digest: %s\n", imgLayer.Digest) - } - fmt.Println() - - fmt.Println("7. Pushing image to registry...") + // + fmt.Println("Pushing image to registry...") // Create progress channel progressChan := make(chan v1.Update, 1) @@ -118,5 +99,14 @@ func main() { log.Fatalf("writing image: %v", err) } + fmt.Println("Getting manifest details...") + manifest, err := img.RawManifest() + if err != nil { + log.Fatalf("getting manifest: %v", err) + } + + fmt.Println("\nManifest:") + fmt.Println(string(manifest)) + fmt.Printf("Successfully pushed %s\n", ref.String()) } diff --git a/pkg/layer/layer.go b/pkg/layer/layer.go index dbe4181..37be59b 100644 --- a/pkg/layer/layer.go +++ b/pkg/layer/layer.go @@ -10,6 +10,10 @@ import ( "github.com/google/go-containerregistry/pkg/v1/types" ) +const ( + MediaTypeGGUF = "application/vnd.docker.ai.model.file.v1+gguf" +) + // Raw implements v1.Layer for raw content type Raw struct { content []byte diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 514494b..31fc034 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -44,8 +44,8 @@ func ShowProgress(operation string, progressChan chan int64, totalSize int64) { fmt.Println() // Move to new line after progress } -// ReadContent reads content from a local file or URL -func ReadContent(source string) ([]byte, error) { +// Open reads content from a local file or URL +func Open(source string) (io.ReadCloser, error) { // Check if the source is a URL if strings.HasPrefix(source, "http://") || strings.HasPrefix(source, "https://") { // Parse the URL @@ -62,30 +62,33 @@ func ReadContent(source string) ([]byte, error) { defer resp.Body.Close() if resp.StatusCode != http.StatusOK { + _ = resp.Body.Close() return nil, fmt.Errorf("failed to download file: HTTP status %d", resp.StatusCode) } + return resp.Body, nil - // Create progress reader - contentLength := resp.ContentLength - progressChan := make(chan int64, 100) - - // Start progress reporting goroutine - go ShowProgress("Downloading", progressChan, contentLength) - - // Create a wrapper reader to track progress - progressReader := &ProgressReader{ - Reader: resp.Body, - ProgressChan: progressChan, - } - - // Read the content - content, err := io.ReadAll(progressReader) - close(progressChan) - return content, err + //// Create progress reader + //contentLength := resp.ContentLength + //progressChan := make(chan int64, 100) + // + //// Start progress reporting goroutine + //go ShowProgress("Downloading", progressChan, contentLength) + // + //// Create a wrapper reader to track progress + //progressReader := &ProgressReader{ + // Reader: resp.Body, + // ProgressChan: progressChan, + //} + // + //// Read the content + //content, err := io.ReadAll(progressReader) + //close(progressChan) + //return content, err } + return os.Open(source) // If not a URL, treat as local file path - return os.ReadFile(source) + //return os.ReadFile(source) } // ProgressReader wraps an io.Reader to track reading progress