reorder some source files
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
||||
*
|
||||
* This work is published and licensed under a Creative Commons
|
||||
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
|
||||
*
|
||||
* Distribution of this work is permitted, but commercial use and
|
||||
* modifications are strictly prohibited.
|
||||
*/
|
||||
package gcrcli
|
||||
|
||||
type Client struct {
|
||||
skipTLSVerify bool
|
||||
}
|
||||
|
||||
func NewClient(skipTLSVerify bool) *Client {
|
||||
return &Client{
|
||||
skipTLSVerify: skipTLSVerify,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
||||
*
|
||||
* This work is published and licensed under a Creative Commons
|
||||
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
|
||||
*
|
||||
* Distribution of this work is permitted, but commercial use and
|
||||
* modifications are strictly prohibited.
|
||||
*/
|
||||
package gcrcli
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type RoundTripper struct {
|
||||
skipTLSVerify bool
|
||||
}
|
||||
|
||||
func NewRoundTripper(skipTLSVerify bool) *RoundTripper {
|
||||
return &RoundTripper{
|
||||
skipTLSVerify: skipTLSVerify,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *RoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
tlsConfig := &tls.Config{
|
||||
InsecureSkipVerify: t.skipTLSVerify,
|
||||
}
|
||||
httpTransport := &http.Transport{
|
||||
TLSClientConfig: tlsConfig,
|
||||
}
|
||||
return httpTransport.RoundTrip(r)
|
||||
}
|
||||
|
||||
func repackReference(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
|
||||
|
||||
uri.Path, _ = url.JoinPath("/", uri.Path)
|
||||
if err != nil {
|
||||
return ref, username, password, err
|
||||
}
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
||||
*
|
||||
* This work is published and licensed under a Creative Commons
|
||||
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
|
||||
*
|
||||
* Distribution of this work is permitted, but commercial use and
|
||||
* modifications are strictly prohibited.
|
||||
*/
|
||||
package gcrcli
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/go-containerregistry/pkg/authn"
|
||||
"github.com/google/go-containerregistry/pkg/crane"
|
||||
"github.com/google/go-containerregistry/pkg/name"
|
||||
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
|
||||
)
|
||||
|
||||
func (cli *Client) DeleteImage(ctx context.Context, imagepath string) error {
|
||||
var err error
|
||||
|
||||
imagepath, username, password, err := repackReference(imagepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
options := make([]crane.Option, 0)
|
||||
options = append(options, crane.WithContext(ctx))
|
||||
|
||||
ref, err := name.ParseReference(imagepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
repo := ref.Context()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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 err
|
||||
}
|
||||
basicAuth := &authn.Basic{
|
||||
Username: username,
|
||||
Password: password,
|
||||
}
|
||||
authTransport, err := transport.NewWithContext(ctx, reg, basicAuth, defaultTransport, scopes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
options = append(options, crane.WithTransport(authTransport))
|
||||
} else {
|
||||
transport := NewRoundTripper(cli.skipTLSVerify)
|
||||
options = append(options, crane.WithTransport(transport))
|
||||
}
|
||||
|
||||
err = crane.Delete(imagepath, options...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
/*
|
||||
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
||||
*
|
||||
* This work is published and licensed under a Creative Commons
|
||||
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
|
||||
*
|
||||
* Distribution of this work is permitted, but commercial use and
|
||||
* modifications are strictly prohibited.
|
||||
*/
|
||||
package gcrcli
|
||||
|
||||
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"
|
||||
"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"
|
||||
)
|
||||
|
||||
const (
|
||||
indexMediaType = "application/vnd.oci.image.index.v1+json"
|
||||
manifestMediaType = "application/vnd.oci.image.manifest.v1+json"
|
||||
)
|
||||
|
||||
func (cli *Client) ImageManifest(ctx context.Context, imagepath string) (any, error) {
|
||||
var err error
|
||||
var res any
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// Manifest
|
||||
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))
|
||||
}
|
||||
|
||||
manifestBytes, err := crane.Manifest(imagepath, options...)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
|
||||
var descr ocispec.Descriptor
|
||||
err = json.Unmarshal(manifestBytes, &descr)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
switch descr.MediaType {
|
||||
case indexMediaType:
|
||||
var index ocispec.Index
|
||||
err = json.Unmarshal(manifestBytes, &index)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res = index
|
||||
//case manifestMediaType:
|
||||
default:
|
||||
var manifest ocispec.Manifest
|
||||
err = json.Unmarshal(manifestBytes, &manifest)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res = &manifest
|
||||
fmt.Printf("Unknown or empty media type: %s", descr.MediaType)
|
||||
}
|
||||
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))
|
||||
|
||||
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
|
||||
}
|
||||
remoteOptions = append(remoteOptions, remote.WithTransport(authTransport))
|
||||
} else {
|
||||
transport := NewRoundTripper(cli.skipTLSVerify)
|
||||
options = append(options, crane.WithTransport(transport))
|
||||
}
|
||||
|
||||
tags, err := remote.List(repo, remoteOptions...)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
||||
*
|
||||
* This work is published and licensed under a Creative Commons
|
||||
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
|
||||
*
|
||||
* Distribution of this work is permitted, but commercial use and
|
||||
* modifications are strictly prohibited.
|
||||
*/
|
||||
package gcrcli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"mstore/pkg/auxtool"
|
||||
"mstore/pkg/auxutar"
|
||||
|
||||
"github.com/google/go-containerregistry/pkg/authn"
|
||||
"github.com/google/go-containerregistry/pkg/crane"
|
||||
"github.com/google/go-containerregistry/pkg/name"
|
||||
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
|
||||
)
|
||||
|
||||
func (cli *Client) PullImage(ctx context.Context, imagepath, filepath string) error {
|
||||
var err error
|
||||
|
||||
imagepath, username, password, err := repackReference(imagepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
options := make([]crane.Option, 0)
|
||||
options = append(options, crane.WithContext(ctx))
|
||||
|
||||
if username != "" && password != "" {
|
||||
ref, err := name.ParseReference(imagepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
repo := ref.Context()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defaultTransport := NewRoundTripper(cli.skipTLSVerify)
|
||||
|
||||
scopes := []string{repo.Scope(transport.PullScope)}
|
||||
|
||||
regName := repo.RegistryStr()
|
||||
reg, err := name.NewRegistry(regName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
basicAuth := &authn.Basic{
|
||||
Username: username,
|
||||
Password: password,
|
||||
}
|
||||
authTransport, err := transport.NewWithContext(ctx, reg, basicAuth, defaultTransport, scopes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
options = append(options, crane.WithTransport(authTransport))
|
||||
} else {
|
||||
transport := NewRoundTripper(cli.skipTLSVerify)
|
||||
options = append(options, crane.WithTransport(transport))
|
||||
}
|
||||
|
||||
image, err := crane.Pull(imagepath, options...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dstdir := auxtool.MakeTmpFilename(filepath)
|
||||
err = os.MkdirAll(dstdir, 0750)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = crane.SaveOCI(image, dstdir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = auxutar.Archive(dstdir, filepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.RemoveAll(dstdir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
/*
|
||||
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
||||
*
|
||||
* This work is published and licensed under a Creative Commons
|
||||
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
|
||||
*
|
||||
* Distribution of this work is permitted, but commercial use and
|
||||
* modifications are strictly prohibited.
|
||||
*/
|
||||
package gcrcli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"mstore/pkg/auxtool"
|
||||
"mstore/pkg/auxutar"
|
||||
|
||||
"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/layout"
|
||||
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
|
||||
)
|
||||
|
||||
func (cli *Client) PushImage(ctx context.Context, filepath, imagepath string) error {
|
||||
var err error
|
||||
|
||||
imagepath, username, password, err := repackReference(imagepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
options := make([]crane.Option, 0)
|
||||
options = append(options, crane.WithContext(ctx))
|
||||
if username != "" && password != "" {
|
||||
ref, err := name.ParseReference(imagepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
repo := ref.Context()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defaultTransport := NewRoundTripper(cli.skipTLSVerify)
|
||||
|
||||
scopes := []string{
|
||||
repo.Scope(transport.PushScope),
|
||||
repo.Scope(transport.PullScope),
|
||||
}
|
||||
|
||||
regName := repo.RegistryStr()
|
||||
reg, err := name.NewRegistry(regName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
basicAuth := &authn.Basic{
|
||||
Username: username,
|
||||
Password: password,
|
||||
}
|
||||
|
||||
authTransport, err := transport.NewWithContext(ctx, reg, basicAuth, defaultTransport, scopes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
options = append(options, crane.WithTransport(authTransport))
|
||||
} else {
|
||||
defaultTransport := NewRoundTripper(cli.skipTLSVerify)
|
||||
options = append(options, crane.WithTransport(defaultTransport))
|
||||
}
|
||||
|
||||
dstdir := auxtool.MakeTmpFilename(filepath)
|
||||
err = auxutar.Unarchive(filepath, dstdir)
|
||||
if err != nil {
|
||||
os.RemoveAll(dstdir)
|
||||
return err
|
||||
}
|
||||
image, err := imageLoader(dstdir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = crane.Push(image, imagepath, options...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.RemoveAll(dstdir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func imageLoader(dirPath string) (pkg.Image, error) {
|
||||
var err error
|
||||
var res pkg.Image
|
||||
layoutPath, err := layout.FromPath(dirPath)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
imageIndex, err := layoutPath.ImageIndex()
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
indexManifest, err := imageIndex.IndexManifest()
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if indexManifest == nil {
|
||||
err := fmt.Errorf("Empty indexManifest referency")
|
||||
return res, err
|
||||
}
|
||||
|
||||
manifest := indexManifest.Manifests[0]
|
||||
image, err := layoutPath.Image(manifest.Digest)
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
res = image
|
||||
return res, err
|
||||
}
|
||||
Reference in New Issue
Block a user