-
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.
parse and validate some input parameters as filesystem-paths (#669)
## Description <!-- Please do not leave this blank This PR [adds/removes/fixes/replaces] the [feature/bug/etc]. --> Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any dependencies that are required for this change. ## What type of PR is this? (check all applicable) - [ ] 🍕 Feature - [x] 🐛 Bug Fix - [ ] 📝 Documentation Update - [ ] 🎨 Style - [ ] 🧑💻 Code Refactor - [ ] 🔥 Performance Improvements - [ ] ✅ Test - [ ] 🤖 Build - [ ] 🔁 CI - [ ] 📦 Chore (Release) - [ ] ⏩ Revert ## Related Tickets & Documents - Fixes #648 ## Added tests? - [ ] 👍 yes - [ ] 🙅 no, because they aren't needed - [ ] 🙋 no, because I need help - [ ] Separate ticket for tests # (issue/pr) Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration ## Added to documentation? - [ ] 📜 README.md - [ ] 🙅 no documentation needed ## Checklist: - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [ ] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules
- Loading branch information
Showing
12 changed files
with
425 additions
and
51 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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 | ||
} | ||
|
||
// 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) | ||
} |
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,112 @@ | ||
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)) | ||
copy(out, *s.value) | ||
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 separated 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 | ||
} | ||
|
||
// 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) | ||
} |
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,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"})) | ||
}) | ||
|
||
}) |
Oops, something went wrong.