83 lines
2.0 KiB
Go
83 lines
2.0 KiB
Go
/*
|
|
* Copyright 2026 Oleg Borodin <onborodin@gmail.com>
|
|
*/
|
|
package imagecmd
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"mstore/pkg/auxtool"
|
|
"mstore/pkg/auxutar"
|
|
"mstore/pkg/gcrcli"
|
|
"mstore/pkg/repocli"
|
|
)
|
|
|
|
// PushImage
|
|
type PushImageParams struct {
|
|
Imagepath string
|
|
Filepath string
|
|
}
|
|
|
|
type PushImageResult struct{}
|
|
|
|
func (util *ImageUtil) PushImage(cmd *cobra.Command, args []string) {
|
|
util.pushImageParams.Filepath = args[0]
|
|
util.pushImageParams.Imagepath = args[1]
|
|
|
|
res, err := util.pushImage(&util.commonImageParams, &util.pushImageParams)
|
|
printResponse(res, err)
|
|
}
|
|
|
|
func (util *ImageUtil) pushImage(common *CommonImageParams, params *PushImageParams) (*PushImageResult, error) {
|
|
var err error
|
|
res := &PushImageResult{}
|
|
|
|
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)
|
|
|
|
imageDir := auxtool.MakeTmpFilename(params.Filepath)
|
|
err = auxutar.Unarchive(params.Filepath, imageDir)
|
|
defer os.RemoveAll(imageDir)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
err = load.Push(ctx, imageDir, ref.Raw(), "", "")
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
|
|
return res, err
|
|
}
|
|
|
|
func (util *ImageUtil) XXXpushImage(common *CommonImageParams, params *PushImageParams) (*PushImageResult, error) {
|
|
var err error
|
|
ctx := context.Background()
|
|
res := &PushImageResult{}
|
|
|
|
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.PushImage(ctx, params.Filepath, params.Imagepath)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
return res, err
|
|
}
|