208 lines
5.7 KiB
Go
208 lines
5.7 KiB
Go
/*
|
|
* 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 main
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
"path"
|
|
"strings"
|
|
"time"
|
|
|
|
"mstore/pkg/client"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func packUserinfo(resurseuri, username, password string) (string, error) {
|
|
var err error
|
|
var res string
|
|
if !strings.Contains(resurseuri, "://") {
|
|
resurseuri = "https://" + resurseuri
|
|
}
|
|
uri, err := url.Parse(resurseuri)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
uri.Path = path.Clean(uri.Path)
|
|
if username != "" && password != "" {
|
|
uri.User = url.UserPassword(username, password)
|
|
}
|
|
res = uri.String()
|
|
return res, err
|
|
}
|
|
|
|
func (util *ImageUtil) CreateImageCmds() *cobra.Command {
|
|
const defaultTimeout uint64 = 30 // Second
|
|
|
|
var subCmd = &cobra.Command{
|
|
Use: "image",
|
|
Short: "Image operation",
|
|
}
|
|
|
|
// ImageInfo
|
|
var imageInfoCmd = &cobra.Command{
|
|
Use: "info",
|
|
Short: "Show container image info",
|
|
Run: util.ImageInfo,
|
|
}
|
|
imageInfoCmd.Flags().StringVarP(&util.imageInfoParams.Username, "username", "u", "", "Username")
|
|
imageInfoCmd.Flags().StringVarP(&util.imageInfoParams.Password, "password", "p", "", "Password")
|
|
imageInfoCmd.Flags().StringVarP(&util.imageInfoParams.Imagepath, "image", "i", "", "Remote image path")
|
|
imageInfoCmd.Flags().Uint64VarP(&util.imageInfoParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
|
|
imageInfoCmd.MarkFlagRequired("image")
|
|
|
|
subCmd.AddCommand(imageInfoCmd)
|
|
|
|
// PullImage
|
|
var pullImageCmd = &cobra.Command{
|
|
Use: "pull",
|
|
Short: "Pull container image into local file",
|
|
Run: util.PullImage,
|
|
}
|
|
pullImageCmd.Flags().StringVarP(&util.pullImageParams.Username, "username", "u", "", "Username")
|
|
pullImageCmd.Flags().StringVarP(&util.pullImageParams.Password, "password", "p", "", "Password")
|
|
pullImageCmd.Flags().StringVarP(&util.pullImageParams.Imagepath, "image", "i", "", "Remote image path")
|
|
pullImageCmd.Flags().StringVarP(&util.pullImageParams.Filepath, "file", "f", "", "Local file path")
|
|
pullImageCmd.Flags().Uint64VarP(&util.pullImageParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
|
|
pullImageCmd.MarkFlagRequired("image")
|
|
pullImageCmd.MarkFlagRequired("file")
|
|
subCmd.AddCommand(pullImageCmd)
|
|
|
|
// PushImage
|
|
var pushImageCmd = &cobra.Command{
|
|
Use: "push",
|
|
Short: "Pull container image into local file",
|
|
Run: util.PushImage,
|
|
}
|
|
pushImageCmd.Flags().StringVarP(&util.pushImageParams.Username, "username", "u", "", "Username")
|
|
pushImageCmd.Flags().StringVarP(&util.pushImageParams.Password, "password", "p", "", "Password")
|
|
pushImageCmd.Flags().StringVarP(&util.pushImageParams.Imagepath, "image", "i", "", "Remote image path")
|
|
pushImageCmd.Flags().StringVarP(&util.pushImageParams.Filepath, "file", "f", "", "Local file path")
|
|
pushImageCmd.Flags().Uint64VarP(&util.pushImageParams.Timeout, "timeout", "t", defaultTimeout, "Operation timeout")
|
|
pushImageCmd.MarkFlagRequired("image")
|
|
pushImageCmd.MarkFlagRequired("file")
|
|
subCmd.AddCommand(pushImageCmd)
|
|
|
|
return subCmd
|
|
}
|
|
|
|
type ImageUtil struct {
|
|
imageInfoParams ImageInfoParams
|
|
pullImageParams PullImageParams
|
|
pushImageParams PushImageParams
|
|
}
|
|
|
|
// ImageInfo
|
|
type ImageInfoParams struct {
|
|
Imagepath string
|
|
Timeout uint64
|
|
Username string
|
|
Password string
|
|
}
|
|
|
|
type ImageInfoResult struct {
|
|
ImageInfo *client.ImageDescr `json:"imageInfo"`
|
|
}
|
|
|
|
func (util *ImageUtil) ImageInfo(cmd *cobra.Command, args []string) {
|
|
res, err := util.imageInfo(&util.imageInfoParams)
|
|
printResponse(res, err)
|
|
}
|
|
|
|
func (util *ImageUtil) imageInfo(params *ImageInfoParams) (*ImageInfoResult, error) {
|
|
var err error
|
|
res := &ImageInfoResult{}
|
|
ctx := context.Background()
|
|
|
|
cli := client.NewClient()
|
|
timeout := time.Duration(params.Timeout) * time.Second
|
|
|
|
params.Imagepath, err = packUserinfo(params.Imagepath, params.Username, params.Password)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
opres, err := cli.ImageInfo(ctx, params.Imagepath, timeout)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
res.ImageInfo = opres
|
|
return res, err
|
|
}
|
|
|
|
// PullImage
|
|
type PullImageParams struct {
|
|
Imagepath string
|
|
Filepath string
|
|
Timeout uint64
|
|
Username string
|
|
Password string
|
|
}
|
|
|
|
type PullImageResult struct{}
|
|
|
|
func (util *ImageUtil) PullImage(cmd *cobra.Command, args []string) {
|
|
res, err := util.pullImage(&util.pullImageParams)
|
|
printResponse(res, err)
|
|
}
|
|
|
|
func (util *ImageUtil) pullImage(params *PullImageParams) (*PullImageResult, error) {
|
|
var err error
|
|
|
|
ctx := context.Background()
|
|
res := &PullImageResult{}
|
|
|
|
cli := client.NewClient()
|
|
timeout := time.Duration(params.Timeout) * time.Second
|
|
params.Imagepath, err = packUserinfo(params.Imagepath, params.Username, params.Password)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
err = cli.PullImage(ctx, params.Imagepath, params.Filepath, timeout)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
return res, err
|
|
}
|
|
|
|
// PushImage
|
|
type PushImageParams struct {
|
|
Imagepath string
|
|
Filepath string
|
|
Timeout uint64
|
|
Username string
|
|
Password string
|
|
}
|
|
|
|
type PushImageResult struct{}
|
|
|
|
func (util *ImageUtil) PushImage(cmd *cobra.Command, args []string) {
|
|
res, err := util.pushImage(&util.pushImageParams)
|
|
printResponse(res, err)
|
|
}
|
|
|
|
func (util *ImageUtil) pushImage(params *PushImageParams) (*PushImageResult, error) {
|
|
var err error
|
|
ctx := context.Background()
|
|
res := &PushImageResult{}
|
|
|
|
cli := client.NewClient()
|
|
timeout := time.Duration(params.Timeout) * time.Second
|
|
params.Imagepath, err = packUserinfo(params.Imagepath, params.Username, params.Password)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
err = cli.PushImage(ctx, params.Filepath, params.Imagepath, timeout)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
return res, err
|
|
}
|