Skip to content

Instantly share code, notes, and snippets.

@justincormack
Created November 23, 2021 15:15
Show Gist options
  • Save justincormack/67926d2b93e86f251e951e61b1744d08 to your computer and use it in GitHub Desktop.
Save justincormack/67926d2b93e86f251e951e61b1744d08 to your computer and use it in GitHub Desktop.
package main
import (
"errors"
"fmt"
"io"
"os"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/remote"
)
func main() {
if len(os.Args) != 2 {
fmt.Println("usage: tmp image")
os.Exit(1)
}
image := os.Args[1]
ref, err := name.ParseReference(image)
if err != nil {
fmt.Println("Cannot parse image name %s: ", image, err)
os.Exit(1)
}
auth := remote.WithAuthFromKeychain(authn.DefaultKeychain)
desc, err := remote.Get(ref, auth)
if err != nil {
fmt.Println("Cannot access remote index: ", err)
os.Exit(1)
}
idx, err := desc.ImageIndex()
if err != nil {
fmt.Println("This program expects an index not a plain image, sorry: ", err)
os.Exit(1)
}
annotations := map[string]string{"test": "value"}
newIdx := mutate.Annotations(idx, annotations).(v1.ImageIndex)
ch := make(chan v1.Update, 100)
go func() {
_ = remote.WriteIndex(ref, newIdx, auth, remote.WithProgress(ch))
}()
for update := range ch {
switch {
case update.Error != nil && errors.Is(update.Error, io.EOF):
fmt.Fprintf(os.Stderr, "receive error message: %v\n", err)
fmt.Printf("%d/%d", update.Complete, update.Total)
break
case update.Error != nil:
fmt.Printf("error writing tarball: %v\n", update.Error)
os.Exit(1)
default:
fmt.Fprintf(os.Stderr, "receive update: %#v\n", update)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment