Skip to content

Instantly share code, notes, and snippets.

@douglasmiranda douglasmiranda/pull.go

forked from cpuguy83/pull.go
Created Apr 3, 2019
Embed
What would you like to do?
package main
import (
"context"
"fmt"
"io/ioutil"
"os"
"github.com/containerd/containerd/content/local"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/remotes"
"github.com/containerd/containerd/remotes/docker"
)
func main() {
var csDir string
if len(os.Args) > 1 {
dir, err := ioutil.TempDir("", "testPull")
if err != nil {
panic(err)
}
defer func() {
e := recover()
if e == nil {
return
}
os.RemoveAll(dir)
panic(e)
}()
} else {
csDir = os.Args[1]
}
// this store is a local content addressible store
// it satifies the "Ingestor" interface used by the call to `images.Dispatch`
cs, err := local.NewStore(csDir)
if err != nil {
panic(err)
}
fmt.Println(csDir)
resolver := docker.NewResolver(docker.ResolverOptions{})
ctx := context.Background()
name, desc, err := resolver.Resolve(ctx, "docker.io/tianon/true:latest")
if err != nil {
panic(err)
}
fetcher, err := resolver.Fetcher(ctx, name)
if err != nil {
panic(err)
}
r, err := fetcher.Fetch(ctx, desc)
if err != nil {
panic(err)
}
defer r.Close()
// Handler which reads a descriptor and fetches the referenced data (e.g. image layers) from the remote
h := remotes.FetchHandler(cs, fetcher)
// This traverses the OCI descriptor to fetch the image and store it into the local store initialized above.
// All content hashes are verified in this step
if err := images.Dispatch(ctx, h, desc); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.