From 978344d2de263b447f2bbb51a2d35d33a311a52a Mon Sep 17 00:00:00 2001 From: Hilmar Falkenberg Date: Tue, 20 Feb 2024 14:24:43 +0100 Subject: [PATCH 01/12] introduce PathOptions --- .../ocmcmds/common/inputs/options/standard.go | 58 +++++++------- pkg/cobrautils/flagsets/types.go | 80 ++++++++++++++++++- 2 files changed, 105 insertions(+), 33 deletions(-) diff --git a/cmds/ocm/commands/ocmcmds/common/inputs/options/standard.go b/cmds/ocm/commands/ocmcmds/common/inputs/options/standard.go index 0ba2712520..dc7391b91d 100644 --- a/cmds/ocm/commands/ocmcmds/common/inputs/options/standard.go +++ b/cmds/ocm/commands/ocmcmds/common/inputs/options/standard.go @@ -1,7 +1,3 @@ -// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors. -// -// SPDX-License-Identifier: Apache-2.0 - package options import ( @@ -14,41 +10,45 @@ var ( MediaTypeOption = options.MediatypeOption ) -var PathOption = flagsets.NewStringOptionType("inputPath", "path field for input") - +// string options var ( - CompressOption = flagsets.NewBoolOptionType("inputCompress", "compress option for input") - ExcludeOption = flagsets.NewStringArrayOptionType("inputExcludes", "excludes (path) for inputs") + VersionOption = flagsets.NewStringOptionType("inputVersion", "version info for inputs") + TextOption = flagsets.NewStringOptionType("inputText", "utf8 text") + HelmRepositoryOption = flagsets.NewStringOptionType("inputHelmRepository", "helm repository base URL") +) +var ( + VariantsOption = flagsets.NewStringArrayOptionType("inputVariants", "(platform) variants for inputs") + ExcludeOption = flagsets.NewStringArrayOptionType("inputExcludes", "excludes (path) for inputs") + PlatformsOption = flagsets.NewStringArrayOptionType("inputPlatforms", "input filter for image platforms ([os]/[architecture])") ) +// path options var ( - IncludeOption = flagsets.NewStringArrayOptionType("inputIncludes", "includes (path) for inputs") - PreserveDirOption = flagsets.NewBoolOptionType("inputPreserveDir", "preserve directory in archive for inputs") + PathOption = flagsets.NewPathOptionType("inputPath", "path field for input") +) +var ( + IncludeOption = flagsets.NewPathArrayOptionType("inputIncludes", "includes (path) for inputs") + LibrariesOption = flagsets.NewPathArrayOptionType("inputLibraries", "library path for inputs") ) +// boolean options var ( + CompressOption = flagsets.NewBoolOptionType("inputCompress", "compress option for input") + PreserveDirOption = flagsets.NewBoolOptionType("inputPreserveDir", "preserve directory in archive for inputs") FollowSymlinksOption = flagsets.NewBoolOptionType("inputFollowSymlinks", "follow symbolic links during archive creation for inputs") - VariantsOption = flagsets.NewStringArrayOptionType("inputVariants", "(platform) variants for inputs") ) -var LibrariesOption = flagsets.NewStringArrayOptionType("inputLibraries", "library path for inputs") - -var VersionOption = flagsets.NewStringOptionType("inputVersion", "version info for inputs") - -var ValuesOption = flagsets.NewValueMapYAMLOptionType("inputValues", "YAML based generic values for inputs") - -var DataOption = flagsets.NewBytesOptionType("inputData", "data (string, !!string or !") - -var TextOption = flagsets.NewStringOptionType("inputText", "utf8 text") - -var YAMLOption = flagsets.NewYAMLOptionType("inputYaml", "YAML formatted text") - -var JSONOption = flagsets.NewYAMLOptionType("inputJson", "JSON formatted text") - -var FormattedJSONOption = flagsets.NewYAMLOptionType("inputFormattedJson", "JSON formatted text") - -var HelmRepositoryOption = flagsets.NewStringOptionType("inputHelmRepository", "helm repository base URL") +// data options +var ( + DataOption = flagsets.NewBytesOptionType("inputData", "data (string, !!string or !") +) +// yaml/json options var ( - PlatformsOption = flagsets.NewStringArrayOptionType("inputPlatforms", "input filter for image platforms ([os]/[architecture])") + YAMLOption = flagsets.NewYAMLOptionType("inputYaml", "YAML formatted text") + JSONOption = flagsets.NewYAMLOptionType("inputJson", "JSON formatted text") + FormattedJSONOption = flagsets.NewYAMLOptionType("inputFormattedJson", "JSON formatted text") +) +var ( + ValuesOption = flagsets.NewValueMapYAMLOptionType("inputValues", "YAML based generic values for inputs") ) diff --git a/pkg/cobrautils/flagsets/types.go b/pkg/cobrautils/flagsets/types.go index bb1c165d77..719c7d7189 100644 --- a/pkg/cobrautils/flagsets/types.go +++ b/pkg/cobrautils/flagsets/types.go @@ -1,7 +1,3 @@ -// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors. -// -// SPDX-License-Identifier: Apache-2.0 - package flagsets import ( @@ -149,6 +145,82 @@ func (o *StringArrayOption) Value() interface{} { return o.value } +// PathOptionType ////////////////////////////////////////////////////////////////////////////// + +type Path string + +type PathOptionType struct { + TypeOptionBase +} + +func NewPathOptionType(name string, description string) ConfigOptionType { + return &PathOptionType{ + TypeOptionBase: TypeOptionBase{name, description}, + } +} + +func (s *PathOptionType) Equal(optionType ConfigOptionType) bool { + return reflect.DeepEqual(s, optionType) +} + +func (s *PathOptionType) Create() Option { + return &PathOption{ + OptionBase: NewOptionBase(s), + } +} + +type PathOption struct { + OptionBase + value string // TODO replace with Path? +} + +var _ Option = (*PathOption)(nil) + +func (o *PathOption) AddFlags(fs *pflag.FlagSet) { + o.TweakFlag(flag.StringVarPF(fs, &o.value, o.otyp.GetName(), "", "", o.otyp.GetDescription())) +} + +func (o *PathOption) Value() interface{} { + return o.value +} + +// PathArrayOptionType ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +type PathArrayOptionType struct { + TypeOptionBase +} + +func NewPathArrayOptionType(name string, description string) ConfigOptionType { + return &PathArrayOptionType{ + TypeOptionBase: TypeOptionBase{name, description}, + } +} + +func (s *PathArrayOptionType) Equal(optionType ConfigOptionType) bool { + return reflect.DeepEqual(s, optionType) +} + +func (s *PathArrayOptionType) Create() Option { + return &PathArrayOption{ + OptionBase: NewOptionBase(s), + } +} + +type PathArrayOption struct { + OptionBase + value []string // TODO replace with Path? +} + +var _ Option = (*PathArrayOption)(nil) + +func (o *PathArrayOption) AddFlags(fs *pflag.FlagSet) { + o.TweakFlag(flag.StringArrayVarPF(fs, &o.value, o.otyp.GetName(), "", nil, o.otyp.GetDescription())) +} + +func (o *PathArrayOption) Value() interface{} { + return o.value +} + //////////////////////////////////////////////////////////////////////////////// type BoolOptionType struct { From c7b9b9640e05a0f0cf15e6352d41e2cc42ed525d Mon Sep 17 00:00:00 2001 From: Hilmar Falkenberg Date: Tue, 20 Feb 2024 16:10:08 +0100 Subject: [PATCH 02/12] ExcludeOption as Path --- cmds/ocm/commands/ocmcmds/common/inputs/options/standard.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmds/ocm/commands/ocmcmds/common/inputs/options/standard.go b/cmds/ocm/commands/ocmcmds/common/inputs/options/standard.go index dc7391b91d..d87863f365 100644 --- a/cmds/ocm/commands/ocmcmds/common/inputs/options/standard.go +++ b/cmds/ocm/commands/ocmcmds/common/inputs/options/standard.go @@ -18,7 +18,6 @@ var ( ) var ( VariantsOption = flagsets.NewStringArrayOptionType("inputVariants", "(platform) variants for inputs") - ExcludeOption = flagsets.NewStringArrayOptionType("inputExcludes", "excludes (path) for inputs") PlatformsOption = flagsets.NewStringArrayOptionType("inputPlatforms", "input filter for image platforms ([os]/[architecture])") ) @@ -28,6 +27,7 @@ var ( ) var ( IncludeOption = flagsets.NewPathArrayOptionType("inputIncludes", "includes (path) for inputs") + ExcludeOption = flagsets.NewPathArrayOptionType("inputExcludes", "excludes (path) for inputs") LibrariesOption = flagsets.NewPathArrayOptionType("inputLibraries", "library path for inputs") ) From 54746275d011d1f536295995e9bf6f158fa7c440 Mon Sep 17 00:00:00 2001 From: Hilmar Falkenberg Date: Tue, 20 Feb 2024 16:11:59 +0100 Subject: [PATCH 03/12] TDD - now we have a failing test ;-) --- .../common/inputs/types/helm/input_test.go | 39 +++++++++++++++++++ .../common/inputs/types/helm/suite_test.go | 13 +++++++ .../inputs/types/helm/testdata/component.yaml | 11 ++++++ 3 files changed, 63 insertions(+) create mode 100644 cmds/ocm/commands/ocmcmds/common/inputs/types/helm/input_test.go create mode 100644 cmds/ocm/commands/ocmcmds/common/inputs/types/helm/suite_test.go create mode 100644 cmds/ocm/commands/ocmcmds/common/inputs/types/helm/testdata/component.yaml diff --git a/cmds/ocm/commands/ocmcmds/common/inputs/types/helm/input_test.go b/cmds/ocm/commands/ocmcmds/common/inputs/types/helm/input_test.go new file mode 100644 index 0000000000..33c628d988 --- /dev/null +++ b/cmds/ocm/commands/ocmcmds/common/inputs/types/helm/input_test.go @@ -0,0 +1,39 @@ +package helm_test + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + me "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs/types/helm" + . "github.com/open-component-model/ocm/cmds/ocm/testhelper" +) + +var _ = Describe("Test Environment", func() { + + Context("https://github.com/open-component-model/ocm/issues/648", func() { + var env *TestEnv + + BeforeEach(func() { + env = NewTestEnv(TestData()) + }) + + AfterEach(func() { + env.Cleanup() + }) + + FIt("creates ctf and adds component", func() { + /* + # Create component archive + ocm create ca --file ca --scheme ocm.software/v3alpha1 --provider test.com test.com/test 1.0.0 + + # Pull Helm chart (normally this will be a Helm chart that is build in a pipeline + helm pull oci://registry-1.docker.io/bitnamicharts/postgresql --version 14.0.5 + + # Add resource (notice the full path here, because that is causing the issue) + ocm add resources --file ca --name postgresql --type helmChart --inputType helm --inputPath E:\t\bugrepo\postgresql-14.0.5.tgz --inputVersion 14.0.5 + */ + + Expect(env.Execute("add", "resources", "--file", "ca", "--name", "postgresql", "--type", "helmChart", "--inputType", me.TYPE, "--inputPath", `E:\t\bugrepo\postgresql-14.0.5.tgz`, "--inputVersion", "14.0.5")).To(Succeed()) + }) + }) +}) diff --git a/cmds/ocm/commands/ocmcmds/common/inputs/types/helm/suite_test.go b/cmds/ocm/commands/ocmcmds/common/inputs/types/helm/suite_test.go new file mode 100644 index 0000000000..8f5e654b86 --- /dev/null +++ b/cmds/ocm/commands/ocmcmds/common/inputs/types/helm/suite_test.go @@ -0,0 +1,13 @@ +package helm_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestConfig(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "--inputType helm") +} diff --git a/cmds/ocm/commands/ocmcmds/common/inputs/types/helm/testdata/component.yaml b/cmds/ocm/commands/ocmcmds/common/inputs/types/helm/testdata/component.yaml new file mode 100644 index 0000000000..bb803d0dd0 --- /dev/null +++ b/cmds/ocm/commands/ocmcmds/common/inputs/types/helm/testdata/component.yaml @@ -0,0 +1,11 @@ +name: ocm.software/demo/test +version: 1.0.0 +provider: + name: ocm.software + +resources: + - name: image + type: ociImage + input: + type: ociArtifact + path: alias.alias/ocm/index:v2.0-index From aab6cddd7a3eb6f0d0aa402e0e5d076527ef0ba7 Mon Sep 17 00:00:00 2001 From: Hilmar Falkenberg Date: Thu, 22 Feb 2024 12:38:26 +0100 Subject: [PATCH 04/12] path --- pkg/cobrautils/flag/path.go | 58 ++++++++++++++++++++++++++++ pkg/cobrautils/flag/path_test.go | 34 ++++++++++++++++ pkg/cobrautils/flag/path_win_test.go | 34 ++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 pkg/cobrautils/flag/path.go create mode 100644 pkg/cobrautils/flag/path_test.go create mode 100644 pkg/cobrautils/flag/path_win_test.go diff --git a/pkg/cobrautils/flag/path.go b/pkg/cobrautils/flag/path.go new file mode 100644 index 0000000000..9a864df1d7 --- /dev/null +++ b/pkg/cobrautils/flag/path.go @@ -0,0 +1,58 @@ +package flag + +import ( + "strings" + + "github.com/mandelsoft/filepath/pkg/filepath" + "github.com/spf13/pflag" +) + +type pathValue string + +func newPathValue(val string, p *string) *pathValue { + *p = pathConv(val) + return (*pathValue)(p) +} + +func (s *pathValue) Set(val string) error { + *s = pathValue(pathConv(val)) + return nil +} + +func (s *pathValue) Type() string { return "filepath" } + +func (s *pathValue) String() string { return string(*s) } + +func pathConv(sval string) string { + vol, paths, rooted := filepath.SplitPath(sval) + if rooted { + return vol + "/" + strings.Join(paths, "/") + } + return vol + strings.Join(paths, "/") +} + +// PathVar defines a filepath flag with specified name, default value, and usage string. +// The argument p points to a string variable in which to store the value of the flag. +func PathVar(f *pflag.FlagSet, p *string, name string, value string, usage string) { + f.VarP(newPathValue(value, p), name, "", usage) +} + +// PathVarP is like PathVar, but accepts a shorthand letter that can be used after a single dash. +func PathVarP(f *pflag.FlagSet, p *string, name, shorthand string, value string, usage string) { + f.VarP(newPathValue(value, p), name, shorthand, usage) +} + +// Path defines a filepath flag with specified name, default value, and usage string. +// The return value is the address of a string variable that stores the value of the flag. +func Path(f *pflag.FlagSet, name string, value string, usage string) *string { + p := new(string) + PathVarP(f, p, name, "", value, usage) + return p +} + +// PathP is like Path, but accepts a shorthand letter that can be used after a single dash. +func PathP(f *pflag.FlagSet, name, shorthand string, value string, usage string) *string { + p := new(string) + PathVarP(f, p, name, shorthand, value, usage) + return p +} diff --git a/pkg/cobrautils/flag/path_test.go b/pkg/cobrautils/flag/path_test.go new file mode 100644 index 0000000000..034c5f83fe --- /dev/null +++ b/pkg/cobrautils/flag/path_test.go @@ -0,0 +1,34 @@ +//go:build !windows +// +build !windows + +package flag_test + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/spf13/pflag" + + . "github.com/open-component-model/ocm/pkg/cobrautils/flag" +) + +var _ = Describe("path flags", func() { + var flags *pflag.FlagSet + + BeforeEach(func() { + flags = pflag.NewFlagSet("test", pflag.ContinueOnError) + }) + + It("parse linux path", func() { + var val string + PathVarP(flags, &val, "path", "p", "", "help message") + flags.Parse([]string{"-p", `/t/bugrepo/postgresql-14.0.5.tgz`}) + Expect(val).To(Equal("/t/bugrepo/postgresql-14.0.5.tgz")) + }) + + It("parse default path", func() { + var val string + PathVarP(flags, &val, "path", "p", `/t/bugrepo/postgresql-14.0.5.tgz`, "help message") + Expect(val).To(Equal("/t/bugrepo/postgresql-14.0.5.tgz")) + }) + +}) diff --git a/pkg/cobrautils/flag/path_win_test.go b/pkg/cobrautils/flag/path_win_test.go new file mode 100644 index 0000000000..fb3efba3a8 --- /dev/null +++ b/pkg/cobrautils/flag/path_win_test.go @@ -0,0 +1,34 @@ +//go:build windows +// +build windows + +package flag_test + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/spf13/pflag" + + . "github.com/open-component-model/ocm/pkg/cobrautils/flag" +) + +var _ = Describe("path flags", func() { + var flags *pflag.FlagSet + + BeforeEach(func() { + flags = pflag.NewFlagSet("test", pflag.ContinueOnError) + }) + + It("parse windows path", func() { + var val string + PathVarPF(flags, &val, "path", "p", "", "help message") + flags.Parse([]string{"-p", `E:\t\bugrepo\postgresql-14.0.5.tgz`}) + Expect(val).To(Equal("E:/t/bugrepo/postgresql-14.0.5.tgz")) + }) + + It("parse default path", func() { + var val string + PathVarP(flags, &val, "path", "p", `E:\t\bugrepo\postgresql-14.0.5.tgz`, "help message") + Expect(val).To(Equal("E:/t/bugrepo/postgresql-14.0.5.tgz")) + }) + +}) From 7f6dfa2723fe44d44d60dc7e374c59d9a0bf40e4 Mon Sep 17 00:00:00 2001 From: Hilmar Falkenberg Date: Thu, 22 Feb 2024 17:08:08 +0100 Subject: [PATCH 05/12] pathArray --- pkg/cobrautils/flag/path_array.go | 108 +++++++++++++++++++++ pkg/cobrautils/flag/path_array_test.go | 34 +++++++ pkg/cobrautils/flag/path_array_win_test.go | 34 +++++++ 3 files changed, 176 insertions(+) create mode 100644 pkg/cobrautils/flag/path_array.go create mode 100644 pkg/cobrautils/flag/path_array_test.go create mode 100644 pkg/cobrautils/flag/path_array_win_test.go diff --git a/pkg/cobrautils/flag/path_array.go b/pkg/cobrautils/flag/path_array.go new file mode 100644 index 0000000000..7452f12943 --- /dev/null +++ b/pkg/cobrautils/flag/path_array.go @@ -0,0 +1,108 @@ +package flag + +import ( + "path/filepath" + "strings" + + "github.com/spf13/pflag" +) + +type pathArrayValue struct { + value *[]string + changed bool +} + +func newPathArrayValue(val []string, p *[]string) *pathArrayValue { + ssv := new(pathArrayValue) + ssv.value = p + *ssv.value = pathArrayConv(val) + return ssv +} + +func (s *pathArrayValue) Set(val string) error { + if !s.changed { + *s.value = pathStringListConv(val) + s.changed = true + } else { + *s.value = append(*s.value, pathConv(val)) + } + return nil +} + +func (s *pathArrayValue) Append(val string) error { + *s.value = append(*s.value, pathConv(val)) + return nil +} + +func (s *pathArrayValue) Replace(val []string) error { + out := make([]string, len(val)) + for i, d := range val { + var err error + out[i] = pathConv(d) + if err != nil { + return err + } + } + *s.value = out + return nil +} + +func (s *pathArrayValue) GetSlice() []string { + out := make([]string, len(*s.value)) + for i, d := range *s.value { + out[i] = d + } + return out +} + +func (s *pathArrayValue) Type() string { + return "stringArray" +} + +func (s *pathArrayValue) String() string { + str := new(string) + *str = strings.Join(*s.value, string(filepath.ListSeparator)) + return *str +} + +// Converts every string into correct filepath format. See pathConv for more details. +func pathArrayConv(sval []string) []string { + for i, val := range sval { + sval[i] = pathConv(val) + } + return sval +} + +// pathStringListConv converts a string containing multiple filepaths seperated by filepath.ListSeparator into a list +// of filepaths. +func pathStringListConv(sval string) []string { + values := filepath.SplitList(sval) + values = pathArrayConv(values) + return values +} + +// PathArrayVar defines a filepath flag with specified name, default value, and usage string. +// The argument p points to a []string variable in which to store the values of the multiple flags. +func PathArrayVar(f *pflag.FlagSet, p *[]string, name string, value []string, usage string) { + f.VarP(newPathArrayValue(value, p), name, "", usage) +} + +// PathArrayVarP is like PathArrayVar, but accepts a shorthand letter that can be used after a single dash. +func PathArrayVarP(f *pflag.FlagSet, p *[]string, name, shorthand string, value []string, usage string) { + f.VarP(newPathArrayValue(value, p), name, shorthand, usage) +} + +// PathArray defines a filepath flag with specified name, default value, and usage string. +// The return value is the address of a []string variable that stores the value of the flag. +func PathArray(f *pflag.FlagSet, name string, value []string, usage string) *[]string { + p := []string{} + PathArrayVarP(f, &p, name, "", value, usage) + return &p +} + +// PathArrayP is like PathArray, but accepts a shorthand letter that can be used after a single dash. +func PathArrayP(f *pflag.FlagSet, name, shorthand string, value []string, usage string) *[]string { + p := []string{} + PathArrayVarP(f, &p, name, shorthand, value, usage) + return &p +} diff --git a/pkg/cobrautils/flag/path_array_test.go b/pkg/cobrautils/flag/path_array_test.go new file mode 100644 index 0000000000..4c34d18f02 --- /dev/null +++ b/pkg/cobrautils/flag/path_array_test.go @@ -0,0 +1,34 @@ +//go:build !windows +// +build !windows + +package flag_test + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/spf13/pflag" + + . "github.com/open-component-model/ocm/pkg/cobrautils/flag" +) + +var _ = Describe("path flags", func() { + var flags *pflag.FlagSet + + BeforeEach(func() { + flags = pflag.NewFlagSet("test", pflag.ContinueOnError) + }) + + It("parse windows path", func() { + var val []string + PathArrayVarPF(flags, &val, "path", "p", nil, "help message") + flags.Parse([]string{"-p", `/foo/bar:other/path`}) + Expect(val).To(Equal([]string{"/foo/bar", "other/path"})) + }) + + It("parse default path", func() { + var val []string + PathArrayVarPF(flags, &val, "path", "p", []string{`/foo/bar`, `other/path`}, "help message") + Expect(val).To(Equal([]string{"/foo/bar", "other/path"})) + }) + +}) diff --git a/pkg/cobrautils/flag/path_array_win_test.go b/pkg/cobrautils/flag/path_array_win_test.go new file mode 100644 index 0000000000..3428e4d962 --- /dev/null +++ b/pkg/cobrautils/flag/path_array_win_test.go @@ -0,0 +1,34 @@ +//go:build windows +// +build windows + +package flag_test + +import ( + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + "github.com/spf13/pflag" + + . "github.com/open-component-model/ocm/pkg/cobrautils/flag" +) + +var _ = Describe("path flags", func() { + var flags *pflag.FlagSet + + BeforeEach(func() { + flags = pflag.NewFlagSet("test", pflag.ContinueOnError) + }) + + It("parse windows path", func() { + var val []string + PathArrayVarPF(flags, &val, "path", "p", nil, "help message") + flags.Parse([]string{"-p", `C:\foo\bar;E:\other\path`}) + Expect(val).To(Equal([]string{"C:/foo/bar", "E:/other/path"})) + }) + + It("parse default path", func() { + var val []string + PathArrayVarPF(flags, &val, "path", "p", []string{`C:\foo\bar`, `E:\other\path`}, "help message") + Expect(val).To(Equal([]string{"C:/foo/bar", "E:/other/path"})) + }) + +}) From b607709dfddff5142079fac0e3b4ebfa5161bc31 Mon Sep 17 00:00:00 2001 From: Hilmar Falkenberg Date: Thu, 22 Feb 2024 17:09:53 +0100 Subject: [PATCH 06/12] replace --- pkg/cobrautils/flag/replace.go | 16 ++++++++++++---- pkg/cobrautils/flagsets/types.go | 10 ++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/pkg/cobrautils/flag/replace.go b/pkg/cobrautils/flag/replace.go index 5cc0f2bebe..c12895b7f5 100644 --- a/pkg/cobrautils/flag/replace.go +++ b/pkg/cobrautils/flag/replace.go @@ -1,7 +1,3 @@ -// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors. -// -// SPDX-License-Identifier: Apache-2.0 - package flag import ( @@ -31,3 +27,15 @@ func IntVarPF(f *pflag.FlagSet, p *int, name, shorthand string, value int, usage f.IntVarP(p, name, shorthand, value, usage) return f.Lookup(name) } + +// PathVarPF is like PathVarP, but returns the created flag. +func PathVarPF(f *pflag.FlagSet, p *string, name, shorthand string, value string, usage string) *pflag.Flag { + PathVarP(f, p, name, shorthand, value, usage) + return f.Lookup(name) +} + +// PathArrayVarPF is like PathArrayVarP, but returns the created flag. +func PathArrayVarPF(f *pflag.FlagSet, p *[]string, name, shorthand string, value []string, usage string) *pflag.Flag { + PathArrayVarP(f, p, name, shorthand, value, usage) + return f.Lookup(name) +} diff --git a/pkg/cobrautils/flagsets/types.go b/pkg/cobrautils/flagsets/types.go index 719c7d7189..02db17cbf0 100644 --- a/pkg/cobrautils/flagsets/types.go +++ b/pkg/cobrautils/flagsets/types.go @@ -147,8 +147,6 @@ func (o *StringArrayOption) Value() interface{} { // PathOptionType ////////////////////////////////////////////////////////////////////////////// -type Path string - type PathOptionType struct { TypeOptionBase } @@ -171,13 +169,13 @@ func (s *PathOptionType) Create() Option { type PathOption struct { OptionBase - value string // TODO replace with Path? + value string } var _ Option = (*PathOption)(nil) func (o *PathOption) AddFlags(fs *pflag.FlagSet) { - o.TweakFlag(flag.StringVarPF(fs, &o.value, o.otyp.GetName(), "", "", o.otyp.GetDescription())) + o.TweakFlag(flag.PathVarPF(fs, &o.value, o.otyp.GetName(), "", "", o.otyp.GetDescription())) } func (o *PathOption) Value() interface{} { @@ -208,13 +206,13 @@ func (s *PathArrayOptionType) Create() Option { type PathArrayOption struct { OptionBase - value []string // TODO replace with Path? + value []string } var _ Option = (*PathArrayOption)(nil) func (o *PathArrayOption) AddFlags(fs *pflag.FlagSet) { - o.TweakFlag(flag.StringArrayVarPF(fs, &o.value, o.otyp.GetName(), "", nil, o.otyp.GetDescription())) + o.TweakFlag(flag.PathArrayVarPF(fs, &o.value, o.otyp.GetName(), "", nil, o.otyp.GetDescription())) } func (o *PathArrayOption) Value() interface{} { From 705b6c1e9c7143f53e8ca235358267a3b62e5d04 Mon Sep 17 00:00:00 2001 From: Hilmar Falkenberg Date: Fri, 23 Feb 2024 12:43:07 +0100 Subject: [PATCH 07/12] remove non working tests --- .../common/inputs/types/helm/input_test.go | 39 ------------------- .../common/inputs/types/helm/suite_test.go | 13 ------- .../inputs/types/helm/testdata/component.yaml | 11 ------ 3 files changed, 63 deletions(-) delete mode 100644 cmds/ocm/commands/ocmcmds/common/inputs/types/helm/input_test.go delete mode 100644 cmds/ocm/commands/ocmcmds/common/inputs/types/helm/suite_test.go delete mode 100644 cmds/ocm/commands/ocmcmds/common/inputs/types/helm/testdata/component.yaml diff --git a/cmds/ocm/commands/ocmcmds/common/inputs/types/helm/input_test.go b/cmds/ocm/commands/ocmcmds/common/inputs/types/helm/input_test.go deleted file mode 100644 index 33c628d988..0000000000 --- a/cmds/ocm/commands/ocmcmds/common/inputs/types/helm/input_test.go +++ /dev/null @@ -1,39 +0,0 @@ -package helm_test - -import ( - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - me "github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/common/inputs/types/helm" - . "github.com/open-component-model/ocm/cmds/ocm/testhelper" -) - -var _ = Describe("Test Environment", func() { - - Context("https://github.com/open-component-model/ocm/issues/648", func() { - var env *TestEnv - - BeforeEach(func() { - env = NewTestEnv(TestData()) - }) - - AfterEach(func() { - env.Cleanup() - }) - - FIt("creates ctf and adds component", func() { - /* - # Create component archive - ocm create ca --file ca --scheme ocm.software/v3alpha1 --provider test.com test.com/test 1.0.0 - - # Pull Helm chart (normally this will be a Helm chart that is build in a pipeline - helm pull oci://registry-1.docker.io/bitnamicharts/postgresql --version 14.0.5 - - # Add resource (notice the full path here, because that is causing the issue) - ocm add resources --file ca --name postgresql --type helmChart --inputType helm --inputPath E:\t\bugrepo\postgresql-14.0.5.tgz --inputVersion 14.0.5 - */ - - Expect(env.Execute("add", "resources", "--file", "ca", "--name", "postgresql", "--type", "helmChart", "--inputType", me.TYPE, "--inputPath", `E:\t\bugrepo\postgresql-14.0.5.tgz`, "--inputVersion", "14.0.5")).To(Succeed()) - }) - }) -}) diff --git a/cmds/ocm/commands/ocmcmds/common/inputs/types/helm/suite_test.go b/cmds/ocm/commands/ocmcmds/common/inputs/types/helm/suite_test.go deleted file mode 100644 index 8f5e654b86..0000000000 --- a/cmds/ocm/commands/ocmcmds/common/inputs/types/helm/suite_test.go +++ /dev/null @@ -1,13 +0,0 @@ -package helm_test - -import ( - "testing" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" -) - -func TestConfig(t *testing.T) { - RegisterFailHandler(Fail) - RunSpecs(t, "--inputType helm") -} diff --git a/cmds/ocm/commands/ocmcmds/common/inputs/types/helm/testdata/component.yaml b/cmds/ocm/commands/ocmcmds/common/inputs/types/helm/testdata/component.yaml deleted file mode 100644 index bb803d0dd0..0000000000 --- a/cmds/ocm/commands/ocmcmds/common/inputs/types/helm/testdata/component.yaml +++ /dev/null @@ -1,11 +0,0 @@ -name: ocm.software/demo/test -version: 1.0.0 -provider: - name: ocm.software - -resources: - - name: image - type: ociImage - input: - type: ociArtifact - path: alias.alias/ocm/index:v2.0-index From 054b69a76f14a5d89e840972c5b7d732fd9938e2 Mon Sep 17 00:00:00 2001 From: Hilmar Falkenberg Date: Fri, 23 Feb 2024 12:49:44 +0100 Subject: [PATCH 08/12] upgrade mandelsoft: vfs + filepath --- go.mod | 4 ++-- go.sum | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index c5ab93ab11..f1229b5650 100644 --- a/go.mod +++ b/go.mod @@ -37,10 +37,10 @@ require ( github.com/imdario/mergo v0.3.16 github.com/klauspost/compress v1.17.2 github.com/klauspost/pgzip v1.2.6 - github.com/mandelsoft/filepath v0.0.0-20230412200429-36b1eb66bd27 + github.com/mandelsoft/filepath v0.0.0-20240223090642-3e2777258aa3 github.com/mandelsoft/logging v0.0.0-20230905123808-7042ee3aae45 github.com/mandelsoft/spiff v1.7.0-beta-5 - github.com/mandelsoft/vfs v0.4.1 + github.com/mandelsoft/vfs v0.4.3-0.20240223095430-def3828dd181 github.com/marstr/guid v1.1.0 github.com/mitchellh/copystructure v1.2.0 github.com/mittwald/go-helm-client v0.12.3 diff --git a/go.sum b/go.sum index 8a6fc4cf96..a9e45a2f42 100644 --- a/go.sum +++ b/go.sum @@ -1826,14 +1826,14 @@ github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7 github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= -github.com/mandelsoft/filepath v0.0.0-20230412200429-36b1eb66bd27 h1:VivN6K8H4H0G4bUmeQH68fQIROL5c8S2iyvVe4teufc= -github.com/mandelsoft/filepath v0.0.0-20230412200429-36b1eb66bd27/go.mod h1:LxhqC7khDoRENwooP6f/vWvia9ivj6TqLYrR39zqkN0= +github.com/mandelsoft/filepath v0.0.0-20240223090642-3e2777258aa3 h1:oo9nIgnyiBgYPbcZslRT4y29siuL5EoNJ/t1tr0xEVQ= +github.com/mandelsoft/filepath v0.0.0-20240223090642-3e2777258aa3/go.mod h1:LxhqC7khDoRENwooP6f/vWvia9ivj6TqLYrR39zqkN0= github.com/mandelsoft/logging v0.0.0-20230905123808-7042ee3aae45 h1:BGJBqw9q1Brn3XAYcj52hhonjV7aHUftVJ3SuJpXQ/M= github.com/mandelsoft/logging v0.0.0-20230905123808-7042ee3aae45/go.mod h1:J/kRqdfAOQmMPfJeAcV2pfX1QLV9/NlAQdAJzpnaU+g= github.com/mandelsoft/spiff v1.7.0-beta-5 h1:3kC10nTviDQhL8diSxp7i4IC2iSiDg6KPbH1CAq7Lfw= github.com/mandelsoft/spiff v1.7.0-beta-5/go.mod h1:TwEeOPuRZxlzQBCLEyVTlHmBSruSGGNdiQ2fovVJ8ao= -github.com/mandelsoft/vfs v0.4.1 h1:HvCC5wySpPIl7K3GFOU5MOftiaXbBlOSa7v+MEzln2g= -github.com/mandelsoft/vfs v0.4.1/go.mod h1:k83vb5I4cqRGJh3TUUVbf2oTF8FrYhvixQ+FwIAgP1Y= +github.com/mandelsoft/vfs v0.4.3-0.20240223095430-def3828dd181 h1:A3F9ihgRtezfoCSoOMtmTU2yOip3aYBTESa6X8hQ+Ek= +github.com/mandelsoft/vfs v0.4.3-0.20240223095430-def3828dd181/go.mod h1:zmbhx2ueQc96buqNXg2S88McBMm2mNFNeyGSpSebrHw= github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg= github.com/markbates/errx v1.1.0 h1:QDFeR+UP95dO12JgW+tgi2UVfo0V8YBHiUIOaeBPiEI= github.com/markbates/errx v1.1.0/go.mod h1:PLa46Oex9KNbVDZhKel8v1OT7hD5JZ2eI7AHhA0wswc= From 834e514fb79b657e87ede192c795f8f840e04d76 Mon Sep 17 00:00:00 2001 From: Hilmar Falkenberg Date: Fri, 23 Feb 2024 15:59:29 +0100 Subject: [PATCH 09/12] update mandelsoft/vfs --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index f1229b5650..484d4235ca 100644 --- a/go.mod +++ b/go.mod @@ -40,7 +40,7 @@ require ( github.com/mandelsoft/filepath v0.0.0-20240223090642-3e2777258aa3 github.com/mandelsoft/logging v0.0.0-20230905123808-7042ee3aae45 github.com/mandelsoft/spiff v1.7.0-beta-5 - github.com/mandelsoft/vfs v0.4.3-0.20240223095430-def3828dd181 + github.com/mandelsoft/vfs v0.4.3 github.com/marstr/guid v1.1.0 github.com/mitchellh/copystructure v1.2.0 github.com/mittwald/go-helm-client v0.12.3 diff --git a/go.sum b/go.sum index a9e45a2f42..1500917d6b 100644 --- a/go.sum +++ b/go.sum @@ -1832,8 +1832,8 @@ github.com/mandelsoft/logging v0.0.0-20230905123808-7042ee3aae45 h1:BGJBqw9q1Brn github.com/mandelsoft/logging v0.0.0-20230905123808-7042ee3aae45/go.mod h1:J/kRqdfAOQmMPfJeAcV2pfX1QLV9/NlAQdAJzpnaU+g= github.com/mandelsoft/spiff v1.7.0-beta-5 h1:3kC10nTviDQhL8diSxp7i4IC2iSiDg6KPbH1CAq7Lfw= github.com/mandelsoft/spiff v1.7.0-beta-5/go.mod h1:TwEeOPuRZxlzQBCLEyVTlHmBSruSGGNdiQ2fovVJ8ao= -github.com/mandelsoft/vfs v0.4.3-0.20240223095430-def3828dd181 h1:A3F9ihgRtezfoCSoOMtmTU2yOip3aYBTESa6X8hQ+Ek= -github.com/mandelsoft/vfs v0.4.3-0.20240223095430-def3828dd181/go.mod h1:zmbhx2ueQc96buqNXg2S88McBMm2mNFNeyGSpSebrHw= +github.com/mandelsoft/vfs v0.4.3 h1:2UMrxQkMXkcHyuqSFhgFDupQ1fmqpKLZuu04DOHx1PA= +github.com/mandelsoft/vfs v0.4.3/go.mod h1:zmbhx2ueQc96buqNXg2S88McBMm2mNFNeyGSpSebrHw= github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg= github.com/markbates/errx v1.1.0 h1:QDFeR+UP95dO12JgW+tgi2UVfo0V8YBHiUIOaeBPiEI= github.com/markbates/errx v1.1.0/go.mod h1:PLa46Oex9KNbVDZhKel8v1OT7hD5JZ2eI7AHhA0wswc= From 0ff3aa0fa07efda1f7dd0020effd53efa7755413 Mon Sep 17 00:00:00 2001 From: Hilmar Falkenberg Date: Fri, 23 Feb 2024 16:04:22 +0100 Subject: [PATCH 10/12] move VarPF into correct files --- pkg/cobrautils/flag/path.go | 6 ++++++ pkg/cobrautils/flag/path_array.go | 6 ++++++ pkg/cobrautils/flag/replace.go | 12 ------------ 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/pkg/cobrautils/flag/path.go b/pkg/cobrautils/flag/path.go index 9a864df1d7..5dac065003 100644 --- a/pkg/cobrautils/flag/path.go +++ b/pkg/cobrautils/flag/path.go @@ -56,3 +56,9 @@ func PathP(f *pflag.FlagSet, name, shorthand string, value string, usage string) PathVarP(f, p, name, shorthand, value, usage) return p } + +// PathVarPF is like PathVarP, but returns the created flag. +func PathVarPF(f *pflag.FlagSet, p *string, name, shorthand string, value string, usage string) *pflag.Flag { + PathVarP(f, p, name, shorthand, value, usage) + return f.Lookup(name) +} diff --git a/pkg/cobrautils/flag/path_array.go b/pkg/cobrautils/flag/path_array.go index 7452f12943..1d9578a4ca 100644 --- a/pkg/cobrautils/flag/path_array.go +++ b/pkg/cobrautils/flag/path_array.go @@ -106,3 +106,9 @@ func PathArrayP(f *pflag.FlagSet, name, shorthand string, value []string, usage PathArrayVarP(f, &p, name, shorthand, value, usage) return &p } + +// PathArrayVarPF is like PathArrayVarP, but returns the created flag. +func PathArrayVarPF(f *pflag.FlagSet, p *[]string, name, shorthand string, value []string, usage string) *pflag.Flag { + PathArrayVarP(f, p, name, shorthand, value, usage) + return f.Lookup(name) +} diff --git a/pkg/cobrautils/flag/replace.go b/pkg/cobrautils/flag/replace.go index c12895b7f5..d68c1e08b8 100644 --- a/pkg/cobrautils/flag/replace.go +++ b/pkg/cobrautils/flag/replace.go @@ -27,15 +27,3 @@ func IntVarPF(f *pflag.FlagSet, p *int, name, shorthand string, value int, usage f.IntVarP(p, name, shorthand, value, usage) return f.Lookup(name) } - -// PathVarPF is like PathVarP, but returns the created flag. -func PathVarPF(f *pflag.FlagSet, p *string, name, shorthand string, value string, usage string) *pflag.Flag { - PathVarP(f, p, name, shorthand, value, usage) - return f.Lookup(name) -} - -// PathArrayVarPF is like PathArrayVarP, but returns the created flag. -func PathArrayVarPF(f *pflag.FlagSet, p *[]string, name, shorthand string, value []string, usage string) *pflag.Flag { - PathArrayVarP(f, p, name, shorthand, value, usage) - return f.Lookup(name) -} From 5141e5bb7ed30a944050bbf69df47db0f986d51f Mon Sep 17 00:00:00 2001 From: Hilmar Falkenberg Date: Fri, 23 Feb 2024 16:07:13 +0100 Subject: [PATCH 11/12] use vfs instead of filepath --- cmds/ocm/commands/ocmcmds/common/inputs/utils.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/cmds/ocm/commands/ocmcmds/common/inputs/utils.go b/cmds/ocm/commands/ocmcmds/common/inputs/utils.go index 18918072cd..973b4424ba 100644 --- a/cmds/ocm/commands/ocmcmds/common/inputs/utils.go +++ b/cmds/ocm/commands/ocmcmds/common/inputs/utils.go @@ -1,15 +1,11 @@ -// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors. -// -// SPDX-License-Identifier: Apache-2.0 - package inputs import ( "fmt" "os" - "github.com/mandelsoft/filepath/pkg/filepath" "github.com/mandelsoft/vfs/pkg/vfs" + "github.com/open-component-model/ocm/pkg/contexts/clictx" "github.com/open-component-model/ocm/pkg/errors" ) @@ -40,7 +36,7 @@ func GetBaseDir(fs vfs.FileSystem, filePath string) (string, error) { return "", fmt.Errorf("unable to read current working directory: %w", err) } } else { - wd = filepath.Dir(filePath) + wd = vfs.Dir(fs, filePath) } return wd, nil } @@ -50,7 +46,7 @@ func GetPath(ctx clictx.Context, path string, inputFilePath string) (string, err if path == "" { return "", fmt.Errorf("path attribute required") } - if filepath.IsAbs(path) { + if vfs.IsAbs(fs, path) { return path, nil } else { wd, err := GetBaseDir(fs, inputFilePath) @@ -58,6 +54,6 @@ func GetPath(ctx clictx.Context, path string, inputFilePath string) (string, err return "", err } - return filepath.Join(wd, path), nil + return vfs.Join(fs, wd, path), nil } } From 51d117b0820051f497d1dd2d2a84b2a62840884e Mon Sep 17 00:00:00 2001 From: Hilmar Falkenberg Date: Fri, 23 Feb 2024 16:23:42 +0100 Subject: [PATCH 12/12] copy array --- pkg/cobrautils/flag/path_array.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pkg/cobrautils/flag/path_array.go b/pkg/cobrautils/flag/path_array.go index 1d9578a4ca..8aaaad3870 100644 --- a/pkg/cobrautils/flag/path_array.go +++ b/pkg/cobrautils/flag/path_array.go @@ -49,9 +49,7 @@ func (s *pathArrayValue) Replace(val []string) error { func (s *pathArrayValue) GetSlice() []string { out := make([]string, len(*s.value)) - for i, d := range *s.value { - out[i] = d - } + copy(out, *s.value) return out } @@ -73,7 +71,7 @@ func pathArrayConv(sval []string) []string { return sval } -// pathStringListConv converts a string containing multiple filepaths seperated by filepath.ListSeparator into a list +// pathStringListConv converts a string containing multiple filepaths separated by filepath.ListSeparator into a list // of filepaths. func pathStringListConv(sval string) []string { values := filepath.SplitList(sval)