Skip to content
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
82 changes: 25 additions & 57 deletions cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

// Command returns a complete command line handler for krf.
func Command() *cobra.Command { //nolint:funlen,maintidx
func Command() *cobra.Command { //nolint:funlen
cmd := &cobra.Command{
Use: "krf [directory|file|-]",
Long: "krf - kubernetes resource filter",
Expand All @@ -50,195 +50,163 @@ func Command() *cobra.Command { //nolint:funlen,maintidx
mf := mflag.NewMatcherFlags(cmd.Flags())

// Define --annotation flag.
mf.StringSliceErrorMatcher(matcher.NewAnnotationMatcher,
mf.StringSliceMatcher(matcher.NewAnnotationMatcher,
"annotation",
nil,
"include resources by annotation")

// Define --not-annotation flag.
mf.StringSliceErrorMatcher(matcher.NewAnnotationMatcher,
mf.StringSliceMatcher(matcher.NewAnnotationMatcher,
"not-annotation",
nil,
"exclude resources by annotation")

// Define --apiversion flag.
mf.StringSliceErrorMatcher(matcher.NewAPIVersionMatcher,
mf.StringSliceMatcher(matcher.NewAPIVersionMatcher,
"apiversion",
nil,
"include resources by api version")

// Define --not-apiversion flag.
mf.StringSliceErrorMatcher(matcher.NewAPIVersionMatcher,
mf.StringSliceMatcher(matcher.NewAPIVersionMatcher,
"not-apiversion",
nil,
"exclude resources by api version")

// Define --cluster-scoped flag.
mf.BoolMatcher(matcher.NewClusterScopedMatcher,
"cluster-scoped",
false,
"include resources that are cluster-scoped")

// Define --contains flag.
mf.StringSliceMatcher(matcher.NewContainsMatcher,
"contains",
nil,
"include resources by substring contents")

// Define --not-contains flag.
mf.StringSliceMatcher(matcher.NewContainsMatcher,
"not-contains",
nil,
"exclude resources by substring contents")

// Define --diff flag.
mf.StringErrorMatcher(matcher.NewDiffMatcher,
mf.StringMatcher(matcher.NewDiffMatcher,
"diff",
"",
"include resources which differ from those in a file")

// Define --not-diff flag.
mf.StringErrorMatcher(matcher.NewDiffMatcher,
mf.StringMatcher(matcher.NewDiffMatcher,
"not-diff",
"",
"exclude resources which differ from those in a file")

// Define --exec flag.
mf.StringErrorMatcher(matcher.NewExecMatcher,
mf.StringMatcher(matcher.NewExecMatcher,
"exec",
"",
"include resources by executing a script")

// Define --not-exec flag.
mf.StringErrorMatcher(matcher.NewExecMatcher,
mf.StringMatcher(matcher.NewExecMatcher,
"not-exec",
"",
"exclude resources by executing a script")

// Define --fieldpath flag.
mf.StringSliceErrorMatcher(matcher.NewFieldPathMatcher,
mf.StringSliceMatcher(matcher.NewFieldPathMatcher,
"fieldpath",
nil,
"include resources by kustomize fieldpath")

// Define --not-fieldpath flag.
mf.StringSliceErrorMatcher(matcher.NewFieldPathMatcher,
mf.StringSliceMatcher(matcher.NewFieldPathMatcher,
"not-fieldpath",
nil,
"exclude resources by kustomize fieldpath")

// Define --jsonpath flag.
mf.StringSliceErrorMatcher(matcher.NewJsonpathMatcher,
mf.StringSliceMatcher(matcher.NewJsonpathMatcher,
"jsonpath",
nil,
"include resources by jsonpath")

// Define --not-jsonpath flag.
mf.StringSliceErrorMatcher(matcher.NewJsonpathMatcher,
mf.StringSliceMatcher(matcher.NewJsonpathMatcher,
"not-jsonpath",
nil,
"exclude resources by jsonpath")

// Define --kind flag.
mf.StringSliceErrorMatcher(matcher.NewKindMatcher,
mf.StringSliceMatcher(matcher.NewKindMatcher,
"kind",
nil,
"include resources by kind")

// Define --not-kind flag.
mf.StringSliceErrorMatcher(matcher.NewKindMatcher,
mf.StringSliceMatcher(matcher.NewKindMatcher,
"not-kind",
nil,
"exclude resources by kind")

// Define --label flag.
mf.StringSliceErrorMatcher(matcher.NewLabelMatcher,
mf.StringSliceMatcher(matcher.NewLabelMatcher,
"label",
nil,
"include resources by label")

// Define --not-label flag.
mf.StringSliceErrorMatcher(matcher.NewLabelMatcher,
mf.StringSliceMatcher(matcher.NewLabelMatcher,
"not-label",
nil,
"exclude resources by label")

// Define --name flag.
mf.StringSliceErrorMatcher(matcher.NewNameMatcher,
mf.StringSliceMatcher(matcher.NewNameMatcher,
"name",
nil,
"include resources by name")

// Define --not-name flag.
mf.StringSliceErrorMatcher(matcher.NewNameMatcher,
mf.StringSliceMatcher(matcher.NewNameMatcher,
"not-name",
nil,
"exclude resources by name")

// Define --namespace flag.
mf.StringSliceErrorMatcher(matcher.NewNamespaceMatcher,
mf.StringSliceMatcher(matcher.NewNamespaceMatcher,
"namespace",
nil,
"include resources by namespace")

// Define --not-namespace flag.
mf.StringSliceErrorMatcher(matcher.NewNamespaceMatcher,
mf.StringSliceMatcher(matcher.NewNamespaceMatcher,
"not-namespace",
nil,
"exclude resources by namespace")

// Define --namespace-scoped flag.
mf.BoolMatcher(matcher.NewNamespaceScopedMatcher,
"namespace-scoped",
false,
"include resources that are namespace-scoped")

// Define --patch flag.
mf.BoolMatcher(matcher.NewPatchMatcher,
"patch",
false,
"include resources from patch files")

// Define --not-patch flag.
mf.BoolMatcher(matcher.NewPatchMatcher,
"not-patch",
false,
"exclude resources from patch files")

// Define --path flag.
mf.StringSliceMatcher(matcher.NewPathMatcher,
"path",
nil,
"include resources by file path")

// Define --not-path flag.
mf.StringSliceMatcher(matcher.NewPathMatcher,
"not-path",
nil,
"exclude resources by file path")

// Define --references flag.
mf.StringSliceErrorMatcher(matcher.NewReferenceMatcher,
mf.StringSliceMatcher(matcher.NewReferenceMatcher,
"references",
nil,
"include resources that reference resource")

// Define --not-references flag.
mf.StringSliceErrorMatcher(matcher.NewReferenceMatcher,
mf.StringSliceMatcher(matcher.NewReferenceMatcher,
"not-references",
nil,
"exclude resources that reference resource")

// Define --selector flag.
mf.StringErrorMatcher(matcher.NewSelectorMatcher,
mf.StringMatcher(matcher.NewSelectorMatcher,
"selector",
"",
"include resources by label selector")

// Define --not-selector flag.
mf.StringErrorMatcher(matcher.NewSelectorMatcher,
mf.StringMatcher(matcher.NewSelectorMatcher,
"not-selector",
"",
"exclude resources by label selector")

// Define --config flag.
Expand Down
24 changes: 8 additions & 16 deletions cmd/mflag/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func (m *FlagSet) Matcher() (matcher.Matcher, error) {

// BoolMatcher creates a named bool flag paired with the given matcher.Matcher
// constructor.
func (m *FlagSet) BoolMatcher(callback func() matcher.Matcher, name string, value bool, usage string) {
result := m.flags.Bool(name, value, usage)
func (m *FlagSet) BoolMatcher(callback func() matcher.Matcher, name string, usage string) {
result := m.flags.Bool(name, false, usage)

fn := func() ([]matcher.Matcher, error) {
if !*result {
Expand All @@ -89,10 +89,10 @@ func (m *FlagSet) BoolMatcher(callback func() matcher.Matcher, name string, valu
m.add(name, fn)
}

// StringErrorMatcher creates a named string flag paired with the given
// StringMatcher creates a named string flag paired with the given
// matcher.Matcher constructor (which can return an error).
func (m *FlagSet) StringErrorMatcher(callback func(string) (matcher.Matcher, error), name string, value string, usage string) {
result := m.flags.String(name, value, usage)
func (m *FlagSet) StringMatcher(callback func(string) (matcher.Matcher, error), name string, usage string) {
result := m.flags.String(name, "", usage)

fn := func() ([]matcher.Matcher, error) {
if *result == "" {
Expand All @@ -110,18 +110,10 @@ func (m *FlagSet) StringErrorMatcher(callback func(string) (matcher.Matcher, err
m.add(name, fn)
}

// StringSliceMatcher creates a named string slice flag paired with the given
// matcher.Matcher constructor.
func (m *FlagSet) StringSliceMatcher(callback func(string) matcher.Matcher, name string, value []string, usage string) {
m.StringSliceErrorMatcher(func(s string) (matcher.Matcher, error) {
return callback(s), nil
}, name, value, usage)
}

// StringSliceErrorMatcher creates a named string slice flag paired with the
// StringSliceMatcher creates a named string slice flag paired with the
// given matcher.Matcher constructor (which can return an error).
func (m *FlagSet) StringSliceErrorMatcher(callback func(string) (matcher.Matcher, error), name string, value []string, usage string) {
results := m.flags.StringSlice(name, value, usage)
func (m *FlagSet) StringSliceMatcher(callback func(string) (matcher.Matcher, error), name string, usage string) {
results := m.flags.StringSlice(name, nil, usage)

fn := func() ([]matcher.Matcher, error) {
if len(*results) == 0 {
Expand Down
4 changes: 2 additions & 2 deletions matcher/matcher-contains.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
// to resources that were originally decoded from JSON files. Additionally,
// these re-marshalled YAML documents no longer contain any of the original
// comments or other formatting.
func NewContainsMatcher(substring string) Matcher {
return containsMatcher{substring: substring}
func NewContainsMatcher(substring string) (Matcher, error) {
return containsMatcher{substring: substring}, nil
}

type containsMatcher struct {
Expand Down
4 changes: 2 additions & 2 deletions matcher/matcher-contains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func TestContainsMatcher(t *testing.T) {
testMatcher(t, []spec{
{
title: "image substring",
matcher: matcher.NewContainsMatcher("mage: gcr.io/goog"),
matcher: must(matcher.NewContainsMatcher("mage: gcr.io/goog")),
matches: []string{"Pod/test-pod"},
},
{
title: "port substring",
matcher: matcher.NewContainsMatcher("ort: 80"),
matcher: must(matcher.NewContainsMatcher("ort: 80")),
matches: []string{
"Deployment/nginx-deployment",
"Service/my-service",
Expand Down
4 changes: 2 additions & 2 deletions matcher/matcher-path.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import (
// For example, a resource decoded from the file
// `kustomize/environments/production/deployment.yaml` would be matched by the
// input `environments/production` or `deployment.yaml`.
func NewPathMatcher(path string) Matcher {
return pathMatcher{path: path}
func NewPathMatcher(path string) (Matcher, error) {
return pathMatcher{path: path}, nil
}

type pathMatcher struct {
Expand Down
4 changes: 2 additions & 2 deletions matcher/matcher-path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestPathMatcher(t *testing.T) {
testMatcher(t, []spec{
{
title: "files under subdir",
matcher: matcher.NewPathMatcher("/subdir/"),
matcher: must(matcher.NewPathMatcher("/subdir/")),
matches: []string{
"ClusterRoleBinding/read-secrets-global",
"Deployment/nginx-deployment",
Expand All @@ -26,7 +26,7 @@ func TestPathMatcher(t *testing.T) {
},
{
title: "files under subsubdir",
matcher: matcher.NewPathMatcher("/subdir/subsubdir"),
matcher: must(matcher.NewPathMatcher("/subdir/subsubdir")),
matches: []string{"ConfigMap/my-configmap"},
},
})
Expand Down