70 lines
2.0 KiB
Go
70 lines
2.0 KiB
Go
package cmd
|
||
|
||
import (
|
||
"context"
|
||
"flag"
|
||
"io"
|
||
"strings"
|
||
|
||
reg "reg/internal/reg"
|
||
)
|
||
|
||
// RunExport реализует команду `reg export <image> [options]`.
|
||
// Сохраняет образ в формате docker save (tar с manifest.json, config и слоями).
|
||
func RunExport(ctx context.Context, client *reg.RegistryClient, args []string) int {
|
||
if len(args) == 0 {
|
||
reg.PrintError(MsgExportRequiresImage.String())
|
||
return 1
|
||
}
|
||
imageArg := args[0]
|
||
|
||
cmdFlags := flag.NewFlagSet("export", flag.ContinueOnError)
|
||
cmdFlags.SetOutput(io.Discard)
|
||
outputFlag := cmdFlags.String("o", "", "output tar file (default: <repo>-<tag>.tar in current directory)")
|
||
platformFlag := cmdFlags.String("platform", "", "platform (os/arch[/variant]); default — current")
|
||
cmdFlags.Usage = func() {}
|
||
if code, ok := parseFlags(cmdFlags, args[1:]); !ok {
|
||
return code
|
||
}
|
||
|
||
ref, err := reg.ParseFullRef(imageArg)
|
||
if err != nil {
|
||
reg.PrintError(err.Error())
|
||
return 1
|
||
}
|
||
|
||
srcClient := clientForRef(client, ref)
|
||
|
||
platform := reg.DefaultPlatform()
|
||
if *platformFlag != "" {
|
||
platform, err = reg.ParsePlatform(*platformFlag)
|
||
if err != nil {
|
||
reg.PrintError(err.Error())
|
||
return 1
|
||
}
|
||
}
|
||
|
||
outPath := *outputFlag
|
||
if outPath == "" {
|
||
imageRef := ®.ImageRef{Repository: ref.Repository, Tag: ref.Tag, Digest: ref.Digest}
|
||
outPath = reg.DefaultOutputDir(imageRef) + ".tar"
|
||
}
|
||
if !strings.HasSuffix(outPath, ".tar") {
|
||
outPath += ".tar"
|
||
}
|
||
|
||
if _, err := reg.ExportDockerSave(ctx, srcClient, ref, platform, outPath); err != nil {
|
||
reg.PrintError(MsgExportError.F(ref.String(), err))
|
||
return 1
|
||
}
|
||
reg.PrintJSON(map[string]interface{}{
|
||
"repository": ref.Repository,
|
||
"tag": ref.Tag,
|
||
"digest": ref.Digest,
|
||
"platform": platform.String(),
|
||
"path": outPath,
|
||
"exported": true,
|
||
})
|
||
return 0
|
||
}
|