From 183b8606feb39ba126086b44803cae55e224b39e Mon Sep 17 00:00:00 2001 From: Uwe Krueger Date: Mon, 5 Dec 2022 17:52:01 +0100 Subject: [PATCH] default dest for download cv --- .../ocmcmds/components/download/cmd.go | 24 +++++++++++++------ pkg/common/accessio/format.go | 9 +++++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/cmds/ocm/commands/ocmcmds/components/download/cmd.go b/cmds/ocm/commands/ocmcmds/components/download/cmd.go index dd329cba05..ea72a29c83 100644 --- a/cmds/ocm/commands/ocmcmds/components/download/cmd.go +++ b/cmds/ocm/commands/ocmcmds/components/download/cmd.go @@ -104,13 +104,16 @@ func (d *action) Close() error { func (d *action) Out() error { list := errors.ErrListf("downloading component versions") dest := destoption.From(d.cmd) + format := formatoption.From(d.cmd) if len(d.data) == 1 { - return d.Save(d.data[0], dest.Destination) + f := dest.Destination + if f == "" { + f = DefaultFileName(d.data[0]) + format.Format.Suffix() + } + return d.Save(d.data[0], f) } else { for _, e := range d.data { - f := e.Spec.UniformRepositorySpec.String() - f = strings.ReplaceAll(f, "::", "-") - f = path.Join(f, e.Spec.Component, *e.Spec.Version) + f := DefaultFileName(e) + format.Format.Suffix() err := d.Save(e, f) if err != nil { list.Add(err) @@ -121,12 +124,12 @@ func (d *action) Out() error { return list.Result() } -func (d *action) Save(o *comphdlr.Object, f string) error { +func (d *action) Save(o *comphdlr.Object, f string) (err error) { dest := destoption.From(d.cmd) src := o.ComponentVersion dir := path.Dir(f) - err := dest.PathFilesystem.MkdirAll(dir, 0o770) + err = dest.PathFilesystem.MkdirAll(dir, 0o770) if err != nil { return err } @@ -136,7 +139,7 @@ func (d *action) Save(o *comphdlr.Object, f string) error { if err != nil { return err } - defer set.Close() + defer errors.PropagateError(&err, set.Close) nv := common.NewNameVersion(src.GetName(), src.GetVersion()) hist := common.History{nv} @@ -147,3 +150,10 @@ func (d *action) Save(o *comphdlr.Object, f string) error { } return err } + +func DefaultFileName(obj *comphdlr.Object) string { + f := obj.Spec.UniformRepositorySpec.String() + f = strings.ReplaceAll(f, "::", "-") + f = path.Join(f, obj.Spec.Component, *obj.Spec.Version) + return f +} diff --git a/pkg/common/accessio/format.go b/pkg/common/accessio/format.go index 465316e0d4..e483483811 100644 --- a/pkg/common/accessio/format.go +++ b/pkg/common/accessio/format.go @@ -23,6 +23,10 @@ func (f FileFormat) String() string { return string(f) } +func (f FileFormat) Suffix() string { + return suffixes[f] +} + func (o FileFormat) ApplyOption(options Options) error { if o != "" { options.SetFileFormat(o) @@ -36,6 +40,11 @@ const ( FormatDirectory FileFormat = "directory" ) +var suffixes = map[FileFormat]string{ + FormatTar: "." + string(FormatTar), + FormatTGZ: "." + string(FormatTGZ), +} + func ErrInvalidFileFormat(fmt string) error { return errors.ErrInvalid(KIND_FILEFORMAT, fmt) }