Skip to content

Commit

Permalink
Got latest
Browse files Browse the repository at this point in the history
  • Loading branch information
treeder committed Aug 23, 2017
1 parent 1424fd1 commit d593afc
Show file tree
Hide file tree
Showing 18 changed files with 252 additions and 235 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ all: vendor build
build:
go build -o fn

install:
go build -o ${GOPATH}/bin/fn

docker:
docker build -t fnproject/fn:latest .

Expand All @@ -20,4 +23,6 @@ release:
GOOS=linux go build -o fn_linux
GOOS=darwin go build -o fn_mac
GOOS=windows go build -o fn.exe
docker run --rm -v ${PWD}:/go/src/github.com/fnproject/cli -w /go/src/github.com/fnproject/cli funcy/go:dev go build -o fn_alpine
docker run --rm -v ${PWD}:/go/src/github.com/fnproject/fn/cli -w /go/src/github.com/fnproject/fn/cli funcy/go:dev go build -o fn_alpine

.PHONY: install
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ if you are using Node, put the code that you want to execute in the file `func.j
Run:

```sh
fn init <DOCKER_HUB_USERNAME>/<FUNCTION_NAME>
fn init [<FUNCTION_NAME>]
```

If you want to override the convention with configuration, you can do that as well using:

```sh
fn init [--runtime node] [--entrypoint "node hello.js"] <DOCKER_HUB_USERNAME>/<FUNCTION_NAME>
fn init [--runtime node] [--entrypoint "node hello.js"] [<FUNCTION_NAME>]
```

Or, if you want full control, just make a Dockerfile. If `init` finds a Dockerfile, it will use that instead of runtime and entrypoint.
Expand Down
49 changes: 26 additions & 23 deletions apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,17 @@ func (a *appsCmd) list(c *cli.Context) error {
})

if err != nil {
// fmt.Println("err type:", reflect.TypeOf(err))
switch err.(type) {
switch e := err.(type) {
case *apiapps.GetAppsAppNotFound:
return fmt.Errorf("error: %v", err.(*apiapps.GetAppsAppNotFound).Payload.Error.Message)
return fmt.Errorf("error: %v", e.Payload.Error.Message)
case *apiapps.GetAppsAppDefault:
return fmt.Errorf("unexpected error: %v", err.(*apiapps.GetAppsAppDefault).Payload.Error.Message)
return fmt.Errorf("unexpected error: %v", e.Payload.Error.Message)
case *apiapps.GetAppsDefault:
// this is the one getting called, not sure what the one above is?
return fmt.Errorf("unexpected error: %v", err.(*apiapps.GetAppsDefault).Payload.Error.Message)
return fmt.Errorf("unexpected error: %v", e.Payload.Error.Message)
default:
return fmt.Errorf("unexpected error: %v", err)
}
return fmt.Errorf("unexpected error: %v", err)
}

if len(resp.Payload.Apps) == 0 {
Expand All @@ -139,15 +139,16 @@ func (a *appsCmd) create(c *cli.Context) error {
})

if err != nil {
switch err.(type) {
switch e := err.(type) {
case *apiapps.PostAppsBadRequest:
return fmt.Errorf("error: %v", err.(*apiapps.PostAppsBadRequest).Payload.Error.Message)
return fmt.Errorf("error: %v", e.Payload.Error.Message)
case *apiapps.PostAppsConflict:
return fmt.Errorf("error: %v", err.(*apiapps.PostAppsConflict).Payload.Error.Message)
return fmt.Errorf("error: %v", e.Payload.Error.Message)
case *apiapps.PostAppsDefault:
return fmt.Errorf("unexpected error: %v", err.(*apiapps.PostAppsDefault).Payload.Error.Message)
return fmt.Errorf("unexpected error: %v", e.Payload.Error.Message)
default:
return fmt.Errorf("unexpected error: %v", err)
}
return fmt.Errorf("unexpected error: %v", err)
}

fmt.Println("Successfully created app: ", resp.Payload.App.Name)
Expand Down Expand Up @@ -215,15 +216,16 @@ func (a *appsCmd) patchApp(appName string, app *models.App) error {
})

if err != nil {
switch err.(type) {
switch e := err.(type) {
case *apiapps.PatchAppsAppBadRequest:
return errors.New(err.(*apiapps.PatchAppsAppBadRequest).Payload.Error.Message)
return errors.New(e.Payload.Error.Message)
case *apiapps.PatchAppsAppNotFound:
return errors.New(err.(*apiapps.PatchAppsAppNotFound).Payload.Error.Message)
return errors.New(e.Payload.Error.Message)
case *apiapps.PatchAppsAppDefault:
return errors.New(err.(*apiapps.PatchAppsAppDefault).Payload.Error.Message)
return errors.New(e.Payload.Error.Message)
default:
return fmt.Errorf("unexpected error: %v", err)
}
return fmt.Errorf("unexpected error: %v", err)
}

return nil
Expand All @@ -243,13 +245,14 @@ func (a *appsCmd) inspect(c *cli.Context) error {
})

if err != nil {
switch err.(type) {
switch e := err.(type) {
case *apiapps.GetAppsAppNotFound:
return fmt.Errorf("error: %v", err.(*apiapps.GetAppsAppNotFound).Payload.Error.Message)
return fmt.Errorf("error: %v", e.Payload.Error.Message)
case *apiapps.GetAppsAppDefault:
return fmt.Errorf("unexpected error: %v", err.(*apiapps.GetAppsAppDefault).Payload.Error.Message)
return fmt.Errorf("unexpected error: %v", e.Payload.Error.Message)
default:
return fmt.Errorf("unexpected error: %v", err)
}
return fmt.Errorf("unexpected error: %v", err)
}

enc := json.NewEncoder(os.Stdout)
Expand Down Expand Up @@ -294,11 +297,11 @@ func (a *appsCmd) delete(c *cli.Context) error {
})

if err != nil {
switch err.(type) {
switch e := err.(type) {
case *apiapps.DeleteAppsAppNotFound:
return errors.New(err.(*apiapps.DeleteAppsAppNotFound).Payload.Error.Message)
return errors.New(e.Payload.Error.Message)
case *apiapps.DeleteAppsAppDefault:
return errors.New(err.(*apiapps.DeleteAppsAppDefault).Payload.Error.Message)
return errors.New(e.Payload.Error.Message)
}
return fmt.Errorf("unexpected error: %v", err)
}
Expand Down
6 changes: 2 additions & 4 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ func (b *buildcmd) flags() []cli.Flag {

// build will take the found valid function and build it
func (b *buildcmd) build(c *cli.Context) error {
verbwriter := verbwriter(b.verbose)

path, err := os.Getwd()
if err != nil {
return err
Expand All @@ -51,11 +49,11 @@ func (b *buildcmd) build(c *cli.Context) error {
return err
}

ff, err := buildfunc(verbwriter, fn, b.noCache)
ff, err := buildfunc(fn, b.noCache)
if err != nil {
return err
}

fmt.Printf("Function %v built successfully.\n", ff.FullName())
fmt.Printf("Function %v built successfully.\n", ff.ImageName())
return nil
}
3 changes: 1 addition & 2 deletions bump.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func (b *bumpcmd) flags() []cli.Flag {

// bump will take the found valid function and bump its version
func (b *bumpcmd) bump(c *cli.Context) error {
verbwriter := verbwriter(b.verbose)

path, err := os.Getwd()
if err != nil {
Expand All @@ -52,7 +51,7 @@ func (b *bumpcmd) bump(c *cli.Context) error {
return err
}

fmt.Fprintln(verbwriter, "bumping version for", fn)
fmt.Println("bumping version for", fn)

funcfile, err := parsefuncfile(fn)
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ func (call *callsCmd) get(ctx *cli.Context) error {
}
resp, err := call.client.Call.GetAppsAppCallsCall(&params)
if err != nil {
switch err.(type) {
switch e := err.(type) {
case *apicall.GetAppsAppCallsCallNotFound:
return fmt.Errorf("error: %v", err.(*apicall.GetAppsAppCallsCallNotFound).Payload.Error.Message)
return fmt.Errorf("error: %v", e.Payload.Error.Message)
default:
return fmt.Errorf("unexpected error: %v", err)
}
return fmt.Errorf("unexpected error: %v", err)

}
printCalls([]*models.Call{resp.Payload.Call})
return nil
Expand All @@ -87,12 +87,12 @@ func (call *callsCmd) list(ctx *cli.Context) error {
}
resp, err := call.client.Call.GetAppsAppCalls(&params)
if err != nil {
switch err.(type) {
switch e := err.(type) {
case *apicall.GetCallsCallNotFound:
return fmt.Errorf("error: %v", err.(*apicall.GetCallsCallNotFound).Payload.Error.Message)
return fmt.Errorf("error: %v", e.Payload.Error.Message)
default:
return fmt.Errorf("unexpected error: %v", err)
}
return fmt.Errorf("unexpected error: %v", err)

}
printCalls(resp.Payload.Calls)
return nil
Expand Down
8 changes: 6 additions & 2 deletions client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
"github.com/go-openapi/strfmt"
)

const (
envFnToken = "FN_TOKEN"
)

func Host() string {
apiURL := os.Getenv("API_URL")
if apiURL == "" {
Expand All @@ -26,8 +30,8 @@ func Host() string {

func APIClient() *fnclient.Functions {
transport := httptransport.New(Host(), "/v1", []string{"http"})
if os.Getenv("FN_TOKEN") != "" {
transport.DefaultAuthentication = httptransport.BearerToken(os.Getenv("FN_TOKEN"))
if os.Getenv(envFnToken) != "" {
transport.DefaultAuthentication = httptransport.BearerToken(os.Getenv(envFnToken))
}

// create the API client, with the transport
Expand Down
62 changes: 41 additions & 21 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"os/signal"
Expand All @@ -15,25 +16,29 @@ import (
"unicode"

"github.com/coreos/go-semver/semver"

"github.com/fnproject/cli/langs"
)

const (
functionsDockerImage = "fnproject/functions"
minRequiredDockerVersion = "17.5.0"
envFnRegistry = "FN_REGISTRY"
)

func verbwriter(verbose bool) io.Writer {
// this is too limiting, removes all logs which isn't what we want
// verbwriter := ioutil.Discard
// if verbose {
verbwriter := os.Stderr
// }
return verbwriter
type HasRegistry interface {
Registry() string
}

func buildfunc(verbwriter io.Writer, fn string, noCache bool) (*funcfile, error) {
func setRegistryEnv(hr HasRegistry) {
if hr.Registry() != "" {
err := os.Setenv(envFnRegistry, hr.Registry())
if err != nil {
log.Fatalf("Couldn't set %s env var: %v\n", envFnRegistry, err)
}
}
}

func buildfunc(fn string, noCache bool) (*funcfile, error) {
funcfile, err := parsefuncfile(fn)
if err != nil {
return nil, err
Expand All @@ -53,23 +58,21 @@ func buildfunc(verbwriter io.Writer, fn string, noCache bool) (*funcfile, error)
}
}

if err := localbuild(verbwriter, fn, funcfile.Build); err != nil {
if err := localbuild(fn, funcfile.Build); err != nil {
return nil, err
}

if err := dockerbuild(verbwriter, fn, funcfile, noCache); err != nil {
if err := dockerbuild(fn, funcfile, noCache); err != nil {
return nil, err
}

return funcfile, nil
}

func localbuild(verbwriter io.Writer, path string, steps []string) error {
func localbuild(path string, steps []string) error {
for _, cmd := range steps {
exe := exec.Command("/bin/sh", "-c", cmd)
exe.Dir = filepath.Dir(path)
exe.Stderr = verbwriter
exe.Stdout = verbwriter
if err := exe.Run(); err != nil {
return fmt.Errorf("error running command %v (%v)", cmd, err)
}
Expand All @@ -78,7 +81,7 @@ func localbuild(verbwriter io.Writer, path string, steps []string) error {
return nil
}

func dockerbuild(verbwriter io.Writer, path string, ff *funcfile, noCache bool) error {
func dockerbuild(path string, ff *funcfile, noCache bool) error {
err := dockerVersionCheck()
if err != nil {
return err
Expand All @@ -89,9 +92,9 @@ func dockerbuild(verbwriter io.Writer, path string, ff *funcfile, noCache bool)
var helper langs.LangHelper
dockerfile := filepath.Join(dir, "Dockerfile")
if !exists(dockerfile) {
helper = langs.GetLangHelper(*ff.Runtime)
helper = langs.GetLangHelper(ff.Runtime)
if helper == nil {
return fmt.Errorf("Cannot build, no language helper found for %v", *ff.Runtime)
return fmt.Errorf("Cannot build, no language helper found for %v", ff.Runtime)
}
dockerfile, err = writeTmpDockerfile(helper, dir, ff)
if err != nil {
Expand All @@ -106,7 +109,7 @@ func dockerbuild(verbwriter io.Writer, path string, ff *funcfile, noCache bool)
}
}

fmt.Printf("Building image %v\n", ff.FullName())
fmt.Printf("Building image %v\n", ff.ImageName())

cancel := make(chan os.Signal, 3)
signal.Notify(cancel, os.Interrupt) // and others perhaps
Expand All @@ -117,7 +120,7 @@ func dockerbuild(verbwriter io.Writer, path string, ff *funcfile, noCache bool)
go func(done chan<- error) {
args := []string{
"build",
"-t", ff.FullName(),
"-t", ff.ImageName(),
"-f", dockerfile,
}
if noCache {
Expand Down Expand Up @@ -261,8 +264,12 @@ func extractEnvConfig(configs []string) map[string]string {
}

func dockerpush(ff *funcfile) error {
fmt.Println("Pushing to docker registry...")
cmd := exec.Command("docker", "push", ff.FullName())
err := validImageName(ff.ImageName())
if err != nil {
return err
}
fmt.Printf("Pushing %v to docker registry...", ff.ImageName())
cmd := exec.Command("docker", "push", ff.ImageName())
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
Expand All @@ -271,6 +278,19 @@ func dockerpush(ff *funcfile) error {
return nil
}

func validImageName(n string) error {
// must have at least owner name and a tag
split := strings.Split(n, ":")
if len(split) < 2 {
return errors.New("image name must have a tag")
}
split2 := strings.Split(split[0], "/")
if len(split2) < 2 {
return errors.New("image name must have an owner and name, eg: username/myfunc. Be sure to set FN_REGISTRY env var or pass in --registry.")
}
return nil
}

func appNamePath(img string) (string, string) {
sep := strings.Index(img, "/")
if sep < 0 {
Expand Down
Loading

0 comments on commit d593afc

Please sign in to comment.