Skip to content

Instantly share code, notes, and snippets.

@douglasmiranda
Forked from cpuguy83/pull.go
Created April 3, 2019 00:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglasmiranda/3b9838489dfef4fcacf1b89d0a46d0f1 to your computer and use it in GitHub Desktop.
Save douglasmiranda/3b9838489dfef4fcacf1b89d0a46d0f1 to your computer and use it in GitHub Desktop.
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