working commit

This commit is contained in:
2026-02-23 00:07:38 +02:00
parent 685880c8a8
commit b6efc19f7a
41 changed files with 11938 additions and 93 deletions
+19
View File
@@ -58,3 +58,22 @@ func repackReference(ref string) (string, string, string, error) {
ref = uri.Host + uri.Path
return ref, username, password, err
}
func repackSource(ref string) (string, string, string, error) {
var err error
var username string
var password string
if !strings.Contains(ref, `://`) {
ref = "https://" + ref
}
uri, err := url.Parse(ref)
if err != nil {
return ref, username, password, err
}
username = uri.User.Username()
password, _ = uri.User.Password()
uri.User = nil
ref = uri.Host
return ref, username, password, err
}
+131 -18
View File
@@ -11,24 +11,26 @@ package client
import (
"context"
"encoding/json"
"fmt"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/name"
pkg "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)
type ImageDescr struct {
ConfigFile *pkg.ConfigFile `json:"configFile,omitempty"`
Manifest *pkg.Manifest `json:"manifest,omitempty"`
Tags []string `json:"avilableTags,omitempty"`
}
const (
indexMediaType = "application/vnd.oci.image.index.v1+json"
manifestMediaType = "application/vnd.oci.image.manifest.v1+json"
)
func (cli *Client) ImageInfo(ctx context.Context, imagepath string) (*ImageDescr, error) {
func (cli *Client) ImageManifest(ctx context.Context, imagepath string) (any, error) {
var err error
res := &ImageDescr{}
var res any
imagepath, username, password, err := repackReference(imagepath)
if err != nil {
@@ -46,6 +48,7 @@ func (cli *Client) ImageInfo(ctx context.Context, imagepath string) (*ImageDescr
return res, err
}
// Manifest
if username != "" && password != "" {
defaultTransport := NewRoundTripper(cli.skipTLSVerify)
scopes := []string{repo.Scope(transport.PullScope)}
@@ -69,25 +72,59 @@ func (cli *Client) ImageInfo(ctx context.Context, imagepath string) (*ImageDescr
options = append(options, crane.WithTransport(transport))
}
image, err := crane.Pull(imagepath, options...)
manifestBytes, err := crane.Manifest(imagepath, options...)
if err != nil {
return res, err
}
config, err := image.ConfigFile()
var descr ocispec.Descriptor
err = json.Unmarshal(manifestBytes, &descr)
if err != nil {
return res, err
}
res.ConfigFile = config
switch descr.MediaType {
case indexMediaType:
var index ocispec.Index
err = json.Unmarshal(manifestBytes, &index)
if err != nil {
return res, err
}
res = index
case manifestMediaType:
var manifest ocispec.Manifest
err = json.Unmarshal(manifestBytes, &manifest)
if err != nil {
return res, err
}
res = &manifest
manifest, err := image.Manifest()
if err != nil {
return res, err
default:
err = fmt.Errorf("Unknown media type: %s", descr.MediaType)
}
res.Manifest = manifest
return res, err
}
func (cli *Client) ImageTags(ctx context.Context, imagepath string) ([]string, error) {
var err error
res := make([]string, 0)
imagepath, username, password, err := repackReference(imagepath)
if err != nil {
return res, err
}
options := make([]crane.Option, 0)
options = append(options, crane.WithContext(ctx))
ref, err := name.ParseReference(imagepath)
if err != nil {
return res, err
}
repo := ref.Context()
if err != nil {
return res, err
}
// Tags
remoteOptions := make([]remote.Option, 0)
remoteOptions = append(remoteOptions, remote.WithContext(ctx))
@@ -118,7 +155,83 @@ func (cli *Client) ImageInfo(ctx context.Context, imagepath string) (*ImageDescr
if err != nil {
return res, err
}
res.Tags = tags
res = tags
return res, err
}
func (cli *Client) ImageConfig(ctx context.Context, imagepath string) (*ocispec.Image, error) {
var err error
res := &ocispec.Image{}
imagepath, username, password, err := repackReference(imagepath)
if err != nil {
return res, err
}
options := make([]crane.Option, 0)
options = append(options, crane.WithContext(ctx))
ref, err := name.ParseReference(imagepath)
if err != nil {
return res, err
}
repo := ref.Context()
if err != nil {
return res, err
}
// Config
if username != "" && password != "" {
defaultTransport := NewRoundTripper(cli.skipTLSVerify)
scopes := []string{repo.Scope(transport.PullScope)}
regName := repo.RegistryStr()
reg, err := name.NewRegistry(regName)
if err != nil {
return res, err
}
basicAuth := &authn.Basic{
Username: username,
Password: password,
}
authTransport, err := transport.NewWithContext(ctx, reg, basicAuth, defaultTransport, scopes)
if err != nil {
return res, err
}
options = append(options, crane.WithTransport(authTransport))
} else {
transport := NewRoundTripper(cli.skipTLSVerify)
options = append(options, crane.WithTransport(transport))
}
configBytes, err := crane.Config(imagepath, options...)
if err != nil {
return res, err
}
var config ocispec.Image
err = json.Unmarshal(configBytes, &config)
if err != nil {
return res, err
}
res = &config
return res, err
}
func (cli *Client) CatalogImages(ctx context.Context, source string) ([]string, error) {
var err error
res := make([]string, 0)
source, _, _, err = repackSource(source)
if err != nil {
return res, err
}
options := make([]crane.Option, 0)
options = append(options, crane.WithContext(ctx))
// Config
transport := NewRoundTripper(cli.skipTLSVerify)
options = append(options, crane.WithTransport(transport))
res, err = crane.Catalog(source, options...)
if err != nil {
return res, err
}
return res, err
}