Created
March 26, 2021 11:43
-
-
Save developer-guy/e9bee872f145d2716b702152175c4f12 to your computer and use it in GitHub Desktop.
Get OCI Image Config programmatically using go-containerregistry as Go module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"log" | |
"os" | |
"github.com/google/go-containerregistry/pkg/authn" | |
"github.com/google/go-containerregistry/pkg/name" | |
"github.com/google/go-containerregistry/pkg/v1/remote" | |
) | |
func main() { | |
basicAuthn := &authn.Basic{ | |
Username: os.Getenv("DOCKER_USERNAME"), | |
Password: os.Getenv("DOCKER_PASSWORD"), | |
} | |
withAuthOption := remote.WithAuth(basicAuthn) | |
options := []remote.Option{withAuthOption} | |
imageName := os.Args[1] | |
ref, err := name.ParseReference(imageName) | |
if err != nil { | |
log.Fatalf("cannot parse reference of the image %s , detail: %v", imageName, err) | |
} | |
descriptor, err := remote.Get(ref, options...) | |
if err != nil { | |
log.Fatalf("cannot get image %s , detail: %v", imageName, err) | |
} | |
image, err := descriptor.Image() | |
if err != nil { | |
log.Fatalf("cannot convert image %s descriptor to v1.Image, detail: %v", imageName, err) | |
} | |
configFile, err := image.ConfigFile() | |
if err != nil { | |
log.Fatalf("cannot extract config file of image %s, detail: %v", imageName, err) | |
} | |
prettyJSON, err := json.MarshalIndent(configFile, "", " ") | |
log.Println(string(prettyJSON)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ DOCKER_USERNAME=<username> DOCKER_PASSWORD=<password> go run -v ./main.go <image> | jq .