diff --git a/template/template.go b/template/template.go index 17d808029..52f50089f 100644 --- a/template/template.go +++ b/template/template.go @@ -125,6 +125,31 @@ func (t *Template) Validate() error { return err } +// Skip says whether or not to skip the build with the given name. +func (o *OnlyExcept) Skip(n string) bool { + if len(o.Only) > 0 { + for _, v := range o.Only { + if v == n { + return false + } + } + + return true + } + + if len(o.Except) > 0 { + for _, v := range o.Except { + if v == n { + return true + } + } + + return false + } + + return false +} + // Validate validates that the OnlyExcept settings are correct for a thing. func (o *OnlyExcept) Validate(t *Template) error { if len(o.Only) > 0 && len(o.Except) > 0 { diff --git a/template/template_test.go b/template/template_test.go index d14682728..6fa39ab88 100644 --- a/template/template_test.go +++ b/template/template_test.go @@ -92,3 +92,46 @@ func TestTemplateValidate(t *testing.T) { } } } + +func TestOnlyExceptSkip(t *testing.T) { + cases := []struct { + Only, Except []string + Input string + Result bool + }{ + { + []string{"foo"}, + nil, + "foo", + false, + }, + + { + nil, + []string{"foo"}, + "foo", + true, + }, + + { + nil, + nil, + "foo", + false, + }, + } + + for _, tc := range cases { + oe := &OnlyExcept{ + Only: tc.Only, + Except: tc.Except, + } + + actual := oe.Skip(tc.Input) + if actual != tc.Result { + t.Fatalf( + "bad: %#v\n\n%#v\n\n%#v\n\n%#v", + actual, tc.Only, tc.Except, tc.Input) + } + } +}