-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4867a2d
commit ef035eb
Showing
21 changed files
with
205 additions
and
82 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
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
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,35 @@ | ||
package flagsets | ||
|
||
type FlagSetMatcher struct { | ||
strict bool | ||
sets map[string]struct{} | ||
} | ||
|
||
func NewMatcher(strict bool, fetched []string) FlagSetMatcher { | ||
out := FlagSetMatcher{ | ||
strict: strict, | ||
sets: make(map[string]struct{}, len(fetched)), | ||
} | ||
|
||
for idx := range fetched { | ||
out.sets[fetched[idx]] = struct{}{} | ||
} | ||
|
||
return out | ||
} | ||
|
||
func (f *FlagSetMatcher) Sanitize(input []string) []string { | ||
if !f.strict || len(input) == 0 { | ||
return input | ||
} | ||
|
||
for idx := range input { | ||
if _, ok := f.sets[input[idx]]; !ok { | ||
if idx+1 < len(input) { | ||
input[idx] = input[len(input)-1] | ||
} | ||
input = input[:len(input)-1] | ||
} | ||
} | ||
return input | ||
} |
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,21 @@ | ||
package flagsets | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFlagSetsMatcher(t *testing.T) { | ||
|
||
m := NewMatcher(false, []string{"s1", "s2", "s3"}) | ||
assert.Equal(t, []string{"s1", "s2", "s3"}, m.Sanitize([]string{"s1", "s2", "s3"})) | ||
assert.Equal(t, []string{"s1", "s2"}, m.Sanitize([]string{"s1", "s2"})) | ||
assert.Equal(t, []string{"s4"}, m.Sanitize([]string{"s4"})) | ||
|
||
m = NewMatcher(true, []string{"s1", "s2", "s3"}) | ||
assert.Equal(t, []string{"s1", "s2", "s3"}, m.Sanitize([]string{"s1", "s2", "s3"})) | ||
assert.Equal(t, []string{"s1", "s2"}, m.Sanitize([]string{"s1", "s2"})) | ||
assert.Equal(t, []string{"s1", "s2"}, m.Sanitize([]string{"s1", "s2", "s7"})) | ||
assert.Equal(t, []string{}, m.Sanitize([]string{"s4"})) | ||
} |
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.