working commit

This commit is contained in:
2026-03-16 20:55:36 +02:00
parent 5c1da77f4c
commit 13b1905e05
31 changed files with 177 additions and 151 deletions
+106
View File
@@ -0,0 +1,106 @@
/*
* 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 imagecmd
import (
"context"
"os"
"time"
"github.com/spf13/cobra"
"mstore/pkg/auxtool"
"mstore/pkg/auxutar"
"mstore/pkg/gcrcli"
"mstore/pkg/repocli"
)
// PullImage
type PullImageParams struct {
Imagepath string
Filepath string
}
type PullImageResult struct {
Filepath string `json:"filepath"`
Size int64 `json:"size"`
}
func (util *ImageUtil) PullImage(cmd *cobra.Command, args []string) {
util.pullImageParams.Imagepath = args[0]
util.pullImageParams.Filepath = args[1]
res, err := util.pullImage(&util.commonImageParams, &util.pullImageParams)
printResponse(res, err)
}
func (util *ImageUtil) pullImage(common *CommonImageParams, params *PullImageParams) (*PullImageResult, error) {
var err error
res := &PullImageResult{}
timeout := time.Duration(common.Timeout) * time.Second
ctx, _ := context.WithTimeout(context.Background(), timeout)
ref, err := repocli.NewReferer(params.Imagepath)
if err != nil {
return res, err
}
ref.SetUserinfo(common.Username, common.Password)
mw := repocli.NewBasicAuthMiddleware(ref.Userinfo())
cli := repocli.NewClient(nil, mw)
load := repocli.NewLoader(cli)
if err != nil {
return res, err
}
imageDir := auxtool.MakeTmpFilename(params.Filepath)
err = load.Pull(ctx, ref.Raw(), imageDir, "", "")
if err != nil {
return res, err
}
err = auxutar.Archive(imageDir, params.Filepath)
defer os.RemoveAll(imageDir)
if err != nil {
return res, err
}
filestat, err := os.Stat(params.Filepath)
if err != nil {
return res, err
}
res.Size = filestat.Size()
res.Filepath = params.Filepath
return res, err
}
func (util *ImageUtil) XXXpullImage(common *CommonImageParams, params *PullImageParams) (*PullImageResult, error) {
var err error
ctx := context.Background()
res := &PullImageResult{}
cli := gcrcli.NewClient(common.SkipTLSVerify)
timeout := time.Duration(common.Timeout) * time.Second
params.Imagepath, err = packUserinfo(params.Imagepath, common.Username, common.Password)
if err != nil {
return res, err
}
ctx, _ = context.WithTimeout(ctx, timeout)
err = cli.PullImage(ctx, params.Imagepath, params.Filepath)
if err != nil {
return res, err
}
filestat, err := os.Stat(params.Filepath)
if err != nil {
return res, err
}
res.Size = filestat.Size()
res.Filepath = params.Filepath
return res, err
}