From 96e4380f0c7483f36a9d401c6b7420c59f7c002f Mon Sep 17 00:00:00 2001 From: Uwe Krueger Date: Mon, 28 Nov 2022 23:45:30 +0100 Subject: [PATCH 1/2] file option + fix various close leaks + fix makefiles --- .../common/options/fileoption/option.go | 82 +++++++++++++++++++ .../common/options/signoption/option.go | 4 +- cmds/ocm/commands/ocmcmds/common/resources.go | 24 +++++- .../ocmcmds/componentarchive/create/cmd.go | 23 +++--- .../ocmcmds/components/transfer/cmd.go | 3 +- .../ocmcmds/components/transfer/cmd_test.go | 9 +- .../components/transfer/upload_test.go | 6 +- .../commands/ocmcmds/references/add/cmd.go | 11 +-- .../ocmcmds/references/add/cmd_test.go | 18 ++-- .../ocm/commands/ocmcmds/resources/add/cmd.go | 13 ++- .../ocmcmds/resources/add/cmd_test.go | 40 ++++++--- cmds/ocm/commands/ocmcmds/sources/add/cmd.go | 12 +-- .../commands/ocmcmds/sources/add/cmd_test.go | 8 +- components/demoplugin/Makefile | 13 +-- components/helmdemo/Makefile | 16 ++-- components/helminstaller/Dockerfile | 3 +- components/helminstaller/Makefile | 20 +++-- components/ocmcli/Makefile | 3 +- docs/reference/ocm_add_references.md | 5 +- docs/reference/ocm_add_resources.md | 7 +- docs/reference/ocm_add_sources.md | 9 +- docs/reference/ocm_create_componentarchive.md | 6 ++ examples/make/Makefile | 2 +- go.mod | 12 +-- go.sum | 18 ++++ .../artifactset/artifactset_test.go | 38 ++++----- .../generic/ocirepo/blobhandler.go | 1 + pkg/contexts/ocm/session.go | 1 + 28 files changed, 280 insertions(+), 127 deletions(-) create mode 100644 cmds/ocm/commands/ocmcmds/common/options/fileoption/option.go diff --git a/cmds/ocm/commands/ocmcmds/common/options/fileoption/option.go b/cmds/ocm/commands/ocmcmds/common/options/fileoption/option.go new file mode 100644 index 000000000..bdf360c79 --- /dev/null +++ b/cmds/ocm/commands/ocmcmds/common/options/fileoption/option.go @@ -0,0 +1,82 @@ +// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors. +// +// SPDX-License-Identifier: Apache-2.0 + +package fileoption + +import ( + "archive/tar" + "fmt" + + "github.com/mandelsoft/vfs/pkg/vfs" + "github.com/spf13/pflag" + + "github.com/open-component-model/ocm/cmds/ocm/pkg/options" + "github.com/open-component-model/ocm/pkg/common/accessio" + "github.com/open-component-model/ocm/pkg/common/compression" +) + +func From(o options.OptionSetProvider) *Option { + var opt *Option + o.AsOptionSet().Get(&opt) + return opt +} + +func NewCompArch() *Option { + return New("component-archive") +} + +func New(def string, us ...interface{}) *Option { + usage := fmt.Sprint(us...) + if usage == "" { + usage = "target file/directory" + } + return &Option{def: def, usage: usage} +} + +type Option struct { + flag *pflag.Flag + def string + usage string + Path string +} + +func (o *Option) AddFlags(fs *pflag.FlagSet) { + o.flag = fs.StringVarPF(&o.Path, "file", "F", o.def, o.usage) +} + +func (o *Option) IsSet() bool { + return o.flag.Changed +} + +// GetPath return a path depending on the option setting and the first argument. +// if the option is not set and the first argument denotes a path to a directory or tar file, +// the first argument if chosen as path. +func (o *Option) GetPath(args []string, fss ...vfs.FileSystem) (string, []string) { + if o.IsSet() || len(args) == 0 { + return o.Path, args + } + + fs := accessio.FileSystem(fss...) + if ok, err := vfs.Exists(fs, args[0]); !ok || err != nil { + return o.Path, args + } + if ok, _ := vfs.IsDir(fs, args[0]); ok { + return args[0], args[1:] + } + + file, err := fs.Open(args[0]) + if err != nil { + return o.Path, args + } + defer file.Close() + r, _, err := compression.AutoDecompress(file) + if err != nil { + return o.Path, args + } + _, err = tar.NewReader(r).Next() + if err != nil { + return o.Path, args + } + return args[0], args[1:] +} diff --git a/cmds/ocm/commands/ocmcmds/common/options/signoption/option.go b/cmds/ocm/commands/ocmcmds/common/options/signoption/option.go index 79ab1cfaf..d306e7aa5 100644 --- a/cmds/ocm/commands/ocmcmds/common/options/signoption/option.go +++ b/cmds/ocm/commands/ocmcmds/common/options/signoption/option.go @@ -152,7 +152,7 @@ func (o *Option) Complete(ctx clictx.Context) error { } func (o *Option) handleKeys(ctx clictx.Context, desc string, keys []string, add func(string, interface{})) error { - for i, k := range keys { + for _, k := range keys { name := "" if len(o.SignatureNames) > 0 { name = o.SignatureNames[0] @@ -161,7 +161,7 @@ func (o *Option) handleKeys(ctx clictx.Context, desc string, keys []string, add sep := strings.Index(k, "=") if sep >= 0 { name = k[:sep] - file = k[i+1:] + file = k[sep+1:] } if len(file) == 0 { return errors.Newf("empty file name") diff --git a/cmds/ocm/commands/ocmcmds/common/resources.go b/cmds/ocm/commands/ocmcmds/common/resources.go index b8e6f6fe3..75dd6674c 100644 --- a/cmds/ocm/commands/ocmcmds/common/resources.go +++ b/cmds/ocm/commands/ocmcmds/common/resources.go @@ -20,6 +20,8 @@ import ( "k8s.io/apimachinery/pkg/util/validation/field" "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs" + "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/options/fileoption" + "github.com/open-component-model/ocm/cmds/ocm/pkg/options" "github.com/open-component-model/ocm/cmds/ocm/pkg/template" "github.com/open-component-model/ocm/cmds/ocm/pkg/utils" "github.com/open-component-model/ocm/pkg/cobrautils/flagsets" @@ -379,12 +381,21 @@ type ResourceAdderCommand struct { Templating template.Options Adder ResourceSpecificationsProvider - Archive string Resources []ResourceSpecifications Envs []string + + Archive string +} + +func NewResourceAdderCommand(ctx clictx.Context, provider ResourceSpecificationsProvider, opts ...options.Options) ResourceAdderCommand { + return ResourceAdderCommand{ + BaseCommand: utils.NewBaseCommand(ctx, append(opts, fileoption.NewCompArch())...), + Adder: provider, + } } func (o *ResourceAdderCommand) AddFlags(fs *pflag.FlagSet) { + o.BaseCommand.AddFlags(fs) fs.StringArrayVarP(&o.Envs, "settings", "s", nil, "settings file with variable settings (yaml)") o.Templating.AddFlags(fs) if o.Adder != nil { @@ -393,7 +404,12 @@ func (o *ResourceAdderCommand) AddFlags(fs *pflag.FlagSet) { } func (o *ResourceAdderCommand) Complete(args []string) error { - o.Archive = args[0] + err := o.OptionSet.ProcessOnOptions(options.CompleteOptionsWithCLIContext(o.Context)) + if err != nil { + return err + } + + o.Archive, args = fileoption.From(o).GetPath(args, o.Context.FileSystem()) o.Templating.Complete(o.Context.FileSystem()) if o.Adder != nil { @@ -409,12 +425,12 @@ func (o *ResourceAdderCommand) Complete(args []string) error { o.Resources = append(o.Resources, rsc...) } - err := o.Templating.ParseSettings(o.Context.FileSystem(), o.Envs...) + err = o.Templating.ParseSettings(o.Context.FileSystem(), o.Envs...) if err != nil { return err } - paths := o.Templating.FilterSettings(args[1:]...) + paths := o.Templating.FilterSettings(args...) for _, p := range paths { o.Resources = append(o.Resources, NewResourceSpecificationsFile(p, o.FileSystem())) } diff --git a/cmds/ocm/commands/ocmcmds/componentarchive/create/cmd.go b/cmds/ocm/commands/ocmcmds/componentarchive/create/cmd.go index b3afe2d0d..a7dab97f2 100644 --- a/cmds/ocm/commands/ocmcmds/componentarchive/create/cmd.go +++ b/cmds/ocm/commands/ocmcmds/componentarchive/create/cmd.go @@ -14,6 +14,7 @@ import ( "github.com/open-component-model/ocm/cmds/ocm/commands/common/options/formatoption" "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common" + "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/options/fileoption" "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/options/schemaoption" "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/names" "github.com/open-component-model/ocm/cmds/ocm/commands/verbs" @@ -39,7 +40,6 @@ type Command struct { Handler comparch.FormatHandler Force bool - Path string Format string Component string @@ -51,7 +51,7 @@ type Command struct { // NewCommand creates a new ctf command. func NewCommand(ctx clictx.Context, names ...string) *cobra.Command { - return utils.SetupCommand(&Command{BaseCommand: utils.NewBaseCommand(ctx, formatoption.New(comparch.GetFormats()...), schemaoption.New(compdesc.DefaultSchemeVersion))}, utils.Names(Names, names...)...) + return utils.SetupCommand(&Command{BaseCommand: utils.NewBaseCommand(ctx, formatoption.New(comparch.GetFormats()...), fileoption.NewCompArch(), schemaoption.New(compdesc.DefaultSchemeVersion))}, utils.Names(Names, names...)...) } func (o *Command) ForName(name string) *cobra.Command { @@ -59,6 +59,9 @@ func (o *Command) ForName(name string) *cobra.Command { Use: "[] --provider {--provider