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

CLI harmonization and api simplification #79

Closed
Closed
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@

target
node_modules
/coverage.out
/bin
28 changes: 28 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#
# golangci-lint
#
# For defaults, see:
# https://github.com/golangci/golangci-lint/blob/master/.golangci.example.yml
#
#
#
linters:
enable:
- unconvert
- prealloc
- bodyclose

issues:
exclude-rules:
- linters:
- staticcheck
# Error Text:
# "SA9004: only the first constant in this group has an explicit type"
# Efect:
# Allows short-hand first constant type declarations:
# Example:
# const (
# Name Type = "value"
# Name2 = "value2"
# )
text: "SA9004:"
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ bin/golangci-lint:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ./bin v1.27.0

check: bin/golangci-lint
./bin/golangci-lint run --enable=unconvert,prealloc,bodyclose
./bin/golangci-lint run

release: build test
go get -u github.com/git-chglog/git-chglog/cmd/git-chglog
Expand All @@ -80,5 +80,5 @@ release: build test
git tag $(VTAG)

clean:
rm -f $(WINDOWS) $(LINUX) $(DARWIN)
-@rm -f coverage.out
rm -f $(BIN) $(WINDOWS) $(LINUX) $(DARWIN)
-rm -f coverage.out
6 changes: 3 additions & 3 deletions appsody/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"path/filepath"
)

// Builder of images from function source using appsody.
// Builder of images from Function source using appsody.
type Builder struct {
// Verbose logging flag.
Verbose bool
Expand All @@ -25,7 +25,7 @@ func NewBuilder(registry, namespace string) *Builder {
namespace: namespace}
}

// Build an image from the function source at path.
// Build an image from the Function source at path.
func (n *Builder) Build(name, runtime, path string) (image string, err error) {
// Check for the appsody binary explicitly so that we can return
// an extra-friendly error message.
Expand Down Expand Up @@ -70,7 +70,7 @@ func (n *Builder) Build(name, runtime, path string) (image string, err error) {
if _, err = os.Stat(cfg); err == nil {
err = os.Remove(cfg)
if err != nil {
fmt.Fprintf(os.Stderr,"unable to remove superfluous appsody config: %v\n", err)
fmt.Fprintf(os.Stderr, "unable to remove superfluous appsody config: %v\n", err)
}
}
return
Expand Down
4 changes: 2 additions & 2 deletions appsody/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var StackShortNames = map[string]string{
"quarkus": "quarkus-ce-functions",
}

// Initializer of functions using the appsody binary.
// Initializer of Functions using the appsody binary.
type Initializer struct {
// Verbose logging flag.
Verbose bool
Expand All @@ -30,7 +30,7 @@ func NewInitializer() *Initializer {
return &Initializer{}
}

// Initialize a new function of the given name, of the given runtime, at the given path.
// Initialize a new Function of the given name, of the given runtime, at the given path.
func (n *Initializer) Initialize(name, runtime, path string) error {
// Check for the appsody binary explicitly so that we can return
// an extra-friendly error message.
Expand Down
2 changes: 1 addition & 1 deletion appsody/initializer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestInitialize(t *testing.T) {
t.Skip()
}

// Create the directory in which the service function will be initialized,
// Create the directory in which the Function will be initialized,
// removing it on test completion.
if err := os.Mkdir("testdata/example.com/www", 0700); err != nil {
t.Fatal(err)
Expand Down
12 changes: 7 additions & 5 deletions appsody/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"fmt"
"os"
"os/exec"

"github.com/boson-project/faas"
)

// Runner of functions using the appsody binary.
// Runner of Functions using the appsody binary.
type Runner struct {
// Verbose logging flag.
Verbose bool
Expand All @@ -18,8 +20,8 @@ func NewRunner() *Runner {
return &Runner{}
}

// Run the function at path
func (n *Runner) Run(path string) error {
// Run the Function at path
func (n *Runner) Run(f faas.Function) error {
// Check for the appsody binary explicitly so that we can return
// an extra-friendly error message.
_, err := exec.LookPath("appsody")
Expand All @@ -30,14 +32,14 @@ func (n *Runner) Run(path string) error {
// Extra arguments to appsody
args := []string{"run"}

// If verbosity is enabled, pass along as an environment variable to the function.
// If verbosity is enabled, pass along as an environment variable to the Function.
if n.Verbose {
args = append(args, []string{"--docker-options", "-e VERBOSE=true"}...)
}

// Set up the command with extra arguments and to run rooted at path
cmd := exec.Command("appsody", args...)
cmd.Dir = path
cmd.Dir = f.Root

// If verbose logging is enabled, echo command
if n.Verbose {
Expand Down
8 changes: 4 additions & 4 deletions appsody/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ func TestRun(t *testing.T) {

// Testdata Function
//
// The directory has been pre-populated with a runnable base function by running
// The directory has been pre-populated with a runnable base Function by running
// init and committing the result, such that this test is not dependent on a
// functioning creation step. This code will need to be updated for any
// backwards incompatible changes to the templated base go func.

// Run the function
// Run the Function

// TODO: in a separate goroutine, submit an HTTP or CloudEvent request to the
// running function, and confirm expected OK response, then close down the
// running function by interrupting/canceling run. This may requre an update
// running Function, and confirm expected OK response, then close down the
// running Function by interrupting/canceling run. This may requre an update
// to the runner to run the command with a cancelable context, and cancel on
// normal signals as well as a signal specific to these tests such as SIGUSR1.

Expand Down
2 changes: 1 addition & 1 deletion appsody/testdata/example.com/runnable/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// Handle a CloudEvent.
// Supported function signatures:
// Supported Function signatures:
// func()
// func() error
// func(context.Context)
Expand Down
46 changes: 22 additions & 24 deletions buildpacks/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import (
"io"
"os"

"github.com/boson-project/faas"
"github.com/buildpacks/pack"
"github.com/buildpacks/pack/logging"

"github.com/boson-project/faas"
)

type Builder struct {
Verbose bool
Tag string
}

func NewBuilder(tag string) *Builder {
return &Builder{Tag: tag}
func NewBuilder() *Builder {
return &Builder{}
}

var runtime2pack = map[string]string{
Expand All @@ -28,44 +28,42 @@ var runtime2pack = map[string]string{
"go": "quay.io/boson/faas-go-builder",
}

func (builder *Builder) Build(path string) (image string, err error) {
f, err := faas.NewFunction(path)
if err != nil {
return
// Build the Function at path.
func (builder *Builder) Build(f faas.Function) (err error) {
// dervive the builder from the specificed runtime
packBuilder, ok := runtime2pack[f.Runtime]
if !ok {
return errors.New(fmt.Sprint("unsupported runtime: ", f.Runtime))
}

runtime := f.Runtime
packBuilder, ok := runtime2pack[runtime]
if !ok {
err = errors.New(fmt.Sprint("unsupported runtime: ", runtime))
return
// Build options for the pack client.
packOpts := pack.BuildOptions{
AppPath: f.Root,
Image: f.Image,
Builder: packBuilder,
}

// log output is either STDOUt or kept in a buffer to be printed on error.
var logWriter io.Writer
if builder.Verbose {
logWriter = os.Stdout
} else {
logWriter = &bytes.Buffer{}
}

logger := logging.New(logWriter)
packClient, err := pack.NewClient(pack.WithLogger(logger))
// Client with a logger which is enabled if in Verbose mode.
packClient, err := pack.NewClient(pack.WithLogger(logging.New(logWriter)))
if err != nil {
return
}

packOpts := pack.BuildOptions{
AppPath: path,
Image: builder.Tag,
Builder: packBuilder,
}

err = packClient.Build(context.Background(), packOpts)
if err != nil {
// Build based using the given builder.
if err = packClient.Build(context.Background(), packOpts); err != nil {
// If the builder was not showing logs, embed the full logs in the error.
if !builder.Verbose {
err = fmt.Errorf("%v\noutput: %s\n", err, logWriter.(*bytes.Buffer).String())
}
}

return builder.Tag, err
return
}
Loading