-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
file option + fix various close leaks + fix makefiles (#203)
* file option + fix various close leaks + fix makefiles * more close leaks
- Loading branch information
1 parent
6355207
commit 811497e
Showing
38 changed files
with
299 additions
and
349 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
/cmds/test | ||
/pkg/test | ||
.idea | ||
*.sw[pqo] | ||
main | ||
|
82 changes: 82 additions & 0 deletions
82
cmds/ocm/commands/ocmcmds/common/options/fileoption/option.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,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). | ||
|
@@ -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 { | ||
|
@@ -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() | ||
|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.