Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): make -t case insensitive #130

Merged
merged 1 commit into from
Jan 6, 2020
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
19 changes: 6 additions & 13 deletions cmd/tk/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
"log"
"os"
"regexp"
"strings"

"github.com/posener/complete"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/grafana/tanka/pkg/cli/cmp"
"github.com/grafana/tanka/pkg/kubernetes/util"
"github.com/grafana/tanka/pkg/tanka"
)

Expand Down Expand Up @@ -136,17 +136,10 @@ func showCmd() *cobra.Command {
return cmd
}

// stringsToRegexps compiles each string to a regular expression
func stringsToRegexps(strs []string) (exps []*regexp.Regexp) {
exps = make([]*regexp.Regexp, 0, len(strs))
for _, raw := range strs {
// surround the regular expression with start and end markers
s := fmt.Sprintf(`^%s$`, raw)
exp, err := regexp.Compile(s)
if err != nil {
log.Fatalf("%s.\nSee https://tanka.dev/targets/#regular-expressions for details on regular expressions.", strings.Title(err.Error()))
}
exps = append(exps, exp)
func stringsToRegexps(exps []string) []*regexp.Regexp {
regexs, err := util.CompileTargetExps(exps)
if err != nil {
log.Fatalln(err)
}
return exps
return regexs
}
21 changes: 16 additions & 5 deletions pkg/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/grafana/tanka/pkg/kubernetes/manifest"
"github.com/grafana/tanka/pkg/kubernetes/util"
"github.com/grafana/tanka/pkg/spec/v1alpha1"
)

Expand All @@ -35,10 +36,10 @@ func TestReconcile(t *testing.T) {
testDataDeep().flat[".app.web.backend.server.nginx.deployment"],
testDataDeep().flat[".app.web.frontend.nodejs.express.service"],
},
targets: []*regexp.Regexp{
regexp.MustCompile("deployment/nginx"),
regexp.MustCompile("service/frontend"),
},
targets: util.MustCompileTargetExps(
`deployment/nginx`,
`service/frontend`,
),
},
{
name: "targets-regex",
Expand All @@ -47,7 +48,17 @@ func TestReconcile(t *testing.T) {
testDataDeep().flat[".app.web.backend.server.nginx.deployment"],
testDataDeep().flat[".app.web.frontend.nodejs.express.deployment"],
},
targets: []*regexp.Regexp{regexp.MustCompile("deployment/.*")},
targets: util.MustCompileTargetExps(`deployment/.*`),
},
{
name: "targets-caseInsensitive",
deep: testDataDeep().deep,
flat: manifest.List{
testDataDeep().flat[".app.web.backend.server.nginx.deployment"],
},
targets: util.MustCompileTargetExps(
`DePlOyMeNt/NgInX`,
),
},
{
name: "force-namespace",
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubernetes/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func Reconcile(raw map[string]interface{}, spec v1alpha1.Spec, targets []*regexp
tmp := funk.Filter(out, func(i interface{}) bool {
p := objectspec(i.(manifest.Manifest))
for _, t := range targets {
if t.MatchString(strings.ToLower(p)) {
if t.MatchString(p) {
return true
}
}
Expand Down
40 changes: 40 additions & 0 deletions pkg/kubernetes/util/targetexp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package util

import (
"fmt"
"regexp"
"strings"
)

type ErrBadTargetExp struct {
inner error
}

func (e ErrBadTargetExp) Error() string {
return fmt.Sprintf("%s.\nSee https://tanka.dev/targets/#regular-expressions for details on regular expressions.", strings.Title(e.inner.Error()))
}

// CompileTargetExps compiles the regular expression for each target
func CompileTargetExps(strs []string) (exps []*regexp.Regexp, err error) {
exps = make([]*regexp.Regexp, 0, len(strs))
for _, raw := range strs {
// surround the regular expression with start and end markers
s := fmt.Sprintf(`(?i)^%s$`, raw)
exp, err := regexp.Compile(s)
if err != nil {
return nil, ErrBadTargetExp{err}
}
exps = append(exps, exp)
}
return exps, nil
}

// MustCompileTargetExps is like CompileTargetExps but panics on error
// Meant for static code
func MustCompileTargetExps(strs ...string) (exps []*regexp.Regexp) {
exps, err := CompileTargetExps(strs)
if err != nil {
panic(err)
}
return exps
}