-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
builder short name constants etc throughout (#1180)
* chore: shared builder constants etc * pretty-print the shared unknown builder error * update builder impls to use shared defs and validators * error and docs text formatting * include static default short names * comment updates and typos * docs paths * use the constants for the in-package builder defaults * use builders.All but with caviat
- Loading branch information
Showing
19 changed files
with
335 additions
and
241 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
Package builders provides constants for builder implementation short names, | ||
shared error types and convienience functions. | ||
*/ | ||
package builders | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
fn "knative.dev/kn-plugin-func" | ||
) | ||
|
||
const ( | ||
Pack = "pack" | ||
S2I = "s2i" | ||
Default = Pack | ||
) | ||
|
||
// Known builder names with a pretty-printed string representation | ||
type Known []string | ||
|
||
func All() Known { | ||
return Known([]string{Pack, S2I}) | ||
} | ||
|
||
func (k Known) String() string { | ||
var b strings.Builder | ||
for i, v := range k { | ||
if i < len(k)-2 { | ||
b.WriteString(strconv.Quote(v) + ", ") | ||
} else if i < len(k)-1 { | ||
b.WriteString(strconv.Quote(v) + " and ") | ||
} else { | ||
b.WriteString(strconv.Quote(v)) | ||
} | ||
} | ||
return b.String() | ||
} | ||
|
||
// ErrUnknownBuilder may be used by whomever is choosing a concrete | ||
// implementation of a builder to invoke based on potentially invalid input. | ||
type ErrUnknownBuilder struct { | ||
Name string | ||
Known Known | ||
} | ||
|
||
func (e ErrUnknownBuilder) Error() string { | ||
if len(e.Known) == 0 { | ||
return fmt.Sprintf("\"%v\" is not a known builder", e.Name) | ||
} | ||
if len(e.Known) == 1 { | ||
return fmt.Sprintf("\"%v\" is not a known builder. The available builder is %v", e.Name, e.Known) | ||
} | ||
return fmt.Sprintf("\"%v\" is not a known builder. Available builders are %s", e.Name, e.Known) | ||
} | ||
|
||
// ErrRuntimeRequired | ||
type ErrRuntimeRequired struct { | ||
Builder string | ||
} | ||
|
||
func (e ErrRuntimeRequired) Error() string { | ||
return fmt.Sprintf("runtime required to choose a default '%v' builder image", e.Builder) | ||
} | ||
|
||
// ErrNoDefaultImage | ||
type ErrNoDefaultImage struct { | ||
Builder string | ||
Runtime string | ||
} | ||
|
||
func (e ErrNoDefaultImage) Error() string { | ||
return fmt.Sprintf("the '%v' runtime defines no default '%v' builder image", e.Runtime, e.Builder) | ||
} | ||
|
||
// Image is a convenience function for choosing the correct builder image | ||
// given a function, a builder, and defaults grouped by runtime. | ||
// - ErrRuntimeRequired if no runtime was provided on the given function | ||
// - ErrNoDefaultImage if the function has no builder image already defined | ||
// for the given runtieme and there is no default in the provided map. | ||
func Image(f fn.Function, builder string, defaults map[string]string) (string, error) { | ||
v, ok := f.BuilderImages[builder] | ||
if ok { | ||
return v, nil // found value | ||
} | ||
if f.Runtime == "" { | ||
return "", ErrRuntimeRequired{Builder: builder} | ||
} | ||
v, ok = defaults[f.Runtime] | ||
if ok { | ||
return v, nil // Found default | ||
} | ||
return "", ErrNoDefaultImage{Builder: builder, Runtime: f.Runtime} | ||
|
||
} |
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,95 @@ | ||
package builders_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
fn "knative.dev/kn-plugin-func" | ||
"knative.dev/kn-plugin-func/builders" | ||
) | ||
|
||
// TestImage_Named ensures that a builder image is returned when | ||
// it exists on the function for a given builder, no defaults. | ||
func TestImage_Named(t *testing.T) { | ||
f := fn.Function{ | ||
Builder: builders.Pack, | ||
BuilderImages: map[string]string{ | ||
builders.Pack: "example.com/my/builder-image", | ||
}, | ||
} | ||
|
||
builderImage, err := builders.Image(f, builders.Pack, make(map[string]string)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if builderImage != "example.com/my/builder-image" { | ||
t.Fatalf("expected 'example.com/my/builder-image', got '%v'", builderImage) | ||
} | ||
} | ||
|
||
// TestImage_ErrRuntimeRequired ensures that the correct error is thrown when | ||
// the function has no builder image yet defined for the named builder, and | ||
// also no runtime to choose from the defaults. | ||
func TestImage_ErrRuntimeRequired(t *testing.T) { | ||
_, err := builders.Image(fn.Function{}, "", make(map[string]string)) | ||
if err == nil { | ||
t.Fatalf("did not receive expected error") | ||
} | ||
if !errors.Is(err, builders.ErrRuntimeRequired{}) { | ||
t.Fatalf("error is not an 'ErrRuntimeRequired': '%v'", err) | ||
} | ||
} | ||
|
||
// TestImage_ErrNoDefaultImage ensures that when | ||
func TestImage_ErrNoDefaultImage(t *testing.T) { | ||
_, err := builders.Image(fn.Function{Runtime: "go"}, "", make(map[string]string)) | ||
if err == nil { | ||
t.Fatalf("did not receive expected error") | ||
} | ||
if !errors.Is(err, builders.ErrNoDefaultImage{Runtime: "go"}) { | ||
t.Fatalf("did not get 'ErrNoDefaultImage', got '%v'", err) | ||
} | ||
} | ||
|
||
// TestImage_Defaults ensures that, when a default exists in the provided | ||
// map, it is chosen when both runtime is defined on the function and no | ||
// builder image has yet to be defined on the function. | ||
func TestImage_Defaults(t *testing.T) { | ||
defaults := map[string]string{ | ||
"go": "example.com/go/default-builder-image", | ||
} | ||
builderImage, err := builders.Image(fn.Function{Runtime: "go"}, "", defaults) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if builderImage != "example.com/go/default-builder-image" { | ||
t.Fatalf("the default was not chosen") | ||
} | ||
} | ||
|
||
// Test_ErrUnknownBuilder ensures that the error properfly formats. | ||
// This error is used externally by packages which share builders but may | ||
// define their own custom builder, thus actually throwing this error | ||
// is the responsibility of whomever is instantiating builders. | ||
func Test_ErrUnknownBuilder(t *testing.T) { | ||
var tests = []struct { | ||
Known []string | ||
Expected string | ||
}{ | ||
{[]string{}, | ||
`"test" is not a known builder`}, | ||
{[]string{"pack"}, | ||
`"test" is not a known builder. The available builder is "pack"`}, | ||
{[]string{"pack", "s2i"}, | ||
`"test" is not a known builder. Available builders are "pack" and "s2i"`}, | ||
{[]string{"pack", "s2i", "custom"}, | ||
`"test" is not a known builder. Available builders are "pack", "s2i" and "custom"`}, | ||
} | ||
for _, test := range tests { | ||
e := builders.ErrUnknownBuilder{Name: "test", Known: test.Known} | ||
if e.Error() != test.Expected { | ||
t.Fatalf("expected error \"%v\". got \"%v\"", test.Expected, e.Error()) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.