Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file option + fix various close leaks + fix makefiles #203

Merged
merged 2 commits into from
Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/cmds/test
/pkg/test
.idea
*.sw[pqo]
main
Expand Down
82 changes: 82 additions & 0 deletions cmds/ocm/commands/ocmcmds/common/options/fileoption/option.go
Original file line number Diff line number Diff line change
@@ -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:]
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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")
Expand Down
24 changes: 20 additions & 4 deletions cmds/ocm/commands/ocmcmds/common/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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()))
}
Expand Down
23 changes: 13 additions & 10 deletions cmds/ocm/commands/ocmcmds/componentarchive/create/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -39,7 +40,6 @@ type Command struct {

Handler comparch.FormatHandler
Force bool
Path string
Format string

Component string
Expand All @@ -51,14 +51,17 @@ 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 {
return &cobra.Command{
Use: "[<options>] <component> <version> --provider <provider-name> {--provider <label>=<value>} {<label>=<value>}",
Args: cobra.MinimumNArgs(2),
Short: "create new component archive",
Example: `
$ ocm create componentarchive --file myfirst --provider acme.org --provider [email protected] amcme.org/demo 1.0
`,
Long: `
Create a new component archive. This might be either a directory prepared
to host component version content or a tar/tgz file (see option --type).
Expand All @@ -72,7 +75,6 @@ func (o *Command) AddFlags(fs *pflag.FlagSet) {
o.BaseCommand.AddFlags(fs)
fs.BoolVarP(&o.Force, "force", "f", false, "remove existing content")
fs.StringArrayVarP(&o.providerattrs, "provider", "p", nil, "provider attribute")
fs.StringVarP(&o.Path, "file", "F", "component-archive", "target file/directory")
}

func (o *Command) Complete(args []string) error {
Expand Down Expand Up @@ -115,20 +117,21 @@ func (o *Command) Complete(args []string) error {
func (o *Command) Run() error {
mode := formatoption.From(o).Mode()
fs := o.Context.FileSystem()
if ok, err := vfs.Exists(fs, o.Path); ok || err != nil {
fp := fileoption.From(o).Path
if ok, err := vfs.Exists(fs, fp); ok || err != nil {
if err != nil {
return err
}
if o.Force {
err = fs.RemoveAll(o.Path)
err = fs.RemoveAll(fp)
if err != nil {
return errors.Wrapf(err, "cannot remove old %q", o.Path)
return errors.Wrapf(err, "cannot remove old %q", fp)
}
}
}
obj, err := comparch.Create(o.Context.OCMContext(), accessobj.ACC_CREATE, o.Path, mode, o.Handler, fs)
obj, err := comparch.Create(o.Context.OCMContext(), accessobj.ACC_CREATE, fp, mode, o.Handler, fs)
if err != nil {
fs.RemoveAll(o.Path)
fs.RemoveAll(fp)
return err
}
desc := obj.GetDescriptor()
Expand All @@ -142,12 +145,12 @@ func (o *Command) Run() error {
err = compdesc.Validate(desc)
if err != nil {
obj.Close()
fs.RemoveAll(o.Path)
fs.RemoveAll(fp)
return errors.Newf("invalid component info: %s", err)
}
err = obj.Close()
if err != nil {
fs.RemoveAll(o.Path)
fs.RemoveAll(fp)
}
return err
}
3 changes: 2 additions & 1 deletion cmds/ocm/commands/ocmcmds/components/transfer/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ func (o *Command) Complete(args []string) error {
func (o *Command) Run() error {
session := ocm.NewSession(nil)
defer session.Close()

session.Finalize(o.OCMContext())

err := o.ProcessOnOptions(ocmcommon.CompleteOptionsWithSession(o, session))
if err != nil {
return err
Expand Down
9 changes: 5 additions & 4 deletions cmds/ocm/commands/ocmcmds/components/transfer/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const OCIHOST = "alias"
func Check(env *TestEnv, ldesc *artdesc.Descriptor, out string) {
tgt, err := ctfocm.Open(env.OCMContext(), accessobj.ACC_READONLY, out, 0, accessio.PathFileSystem(env.FileSystem()))
Expect(err).To(Succeed())
defer tgt.Close()
defer Close(tgt, "ctf")

list, err := tgt.ComponentLister().GetComponents("", true)
Expect(err).To(Succeed())
Expand All @@ -51,6 +51,7 @@ func Check(env *TestEnv, ldesc *artdesc.Descriptor, out string) {
func CheckComponent(env *TestEnv, ldesc *artdesc.Descriptor, tgt ocm.Repository) {
comp, err := tgt.LookupComponentVersion(COMPONENT, VERSION)
Expect(err).To(Succeed())
defer Close(comp, "comvers")
Expect(len(comp.GetDescriptor().Resources)).To(Equal(3))

data, err := json.Marshal(comp.GetDescriptor().Resources[2].Access)
Expand All @@ -74,6 +75,7 @@ func CheckComponent(env *TestEnv, ldesc *artdesc.Descriptor, tgt ocm.Repository)

blob, err := set.GetBlob(ldesc.Digest)
Expect(err).To(Succeed())
defer Close(blob, "blob")
data, err = blob.Get()
Expect(err).To(Succeed())
Expect(string(data)).To(Equal("manifestlayer"))
Expand Down Expand Up @@ -165,14 +167,13 @@ transferring version "github.com/mandelsoft/test2:v1"...
Expect(env.DirExists(OUT)).To(BeTrue())
tgt, err := ctfocm.Open(env.OCMContext(), accessobj.ACC_READONLY, OUT, 0, accessio.PathFileSystem(env.FileSystem()))
Expect(err).To(Succeed())
defer tgt.Close()
defer Close(tgt, "ctf")

list, err := tgt.ComponentLister().GetComponents("", true)
Expect(err).To(Succeed())
Expect(list).To(ContainElements([]string{COMPONENT2, COMPONENT}))

_, err = tgt.LookupComponentVersion(COMPONENT2, VERSION)
Expect(err).To(Succeed())
Expect(tgt.ExistsComponentVersion(COMPONENT2, VERSION)).To(BeTrue())

CheckComponent(env, ldesc, tgt)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,11 @@ transferring version "github.com/compa:1.0.0"...
`))

copy := Must(ctfocm.Open(ctx, accessobj.ACC_READONLY, COPY, 0700, env))
ocopy := accessio.OnceCloser(copy)
defer Close(ocopy)
defer Close(copy)

// check type
cv2 := Must(copy.LookupComponentVersion(COMP, VERS))
ocv2 := accessio.OnceCloser(cv2)
defer Close(ocv2)
defer Close(cv2)
ra := Must(cv2.GetResourceByIndex(0))
acc := Must(ra.Access())
Expect(acc.GetKind()).To(Equal(ociartifact.Type))
Expand Down
11 changes: 4 additions & 7 deletions cmds/ocm/commands/ocmcmds/references/add/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,16 @@ type Command struct {
func NewCommand(ctx clictx.Context, names ...string) *cobra.Command {
return utils.SetupCommand(
&Command{
common.ResourceAdderCommand{
BaseCommand: utils.NewBaseCommand(ctx),
Adder: NewReferenceSpecificatonProvider(),
},
common.NewResourceAdderCommand(ctx, NewReferenceSpecificatonProvider()),
},
utils.Names(Names, names...)...,
)
}

func (o *Command) ForName(name string) *cobra.Command {
return &cobra.Command{
Use: "[<options>] <target> {<referencefile> | <var>=<value>}",
Args: cobra.MinimumNArgs(1),
Use: "[<options>] [<target>] {<referencefile> | <var>=<value>}",
Args: cobra.MinimumNArgs(0),
Short: "add aggregation information to a component version",
Long: `
Add aggregation information specified in a reference file to a component version.
Expand All @@ -53,7 +50,7 @@ description scheme of the component descriptor.
Example: `
Add a reference directly by options
<pre>
$ ocm add references path/to/ca --name myref --component github.com/my/component --version ${VERSION}
$ ocm add references --file path/to/ca --name myref --component github.com/my/component --version ${VERSION}
</pre>

Add a reference by a description file:
Expand Down
Loading