mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
fnctl: code cleanup (#281)
This commit is contained in:
committed by
Seif Lotfy سيف لطفي
parent
0d71e1e38e
commit
1395393b9b
@@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
@@ -28,18 +27,20 @@ func (b *buildcmd) scan(c *cli.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *buildcmd) walker(path string, info os.FileInfo, err error, w io.Writer) error {
|
func (b *buildcmd) walker(path string, info os.FileInfo, err error) error {
|
||||||
walker(path, info, err, w, b.build)
|
walker(path, info, err, b.build)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// build will take the found valid function and build it
|
// build will take the found valid function and build it
|
||||||
func (b *buildcmd) build(path string) error {
|
func (b *buildcmd) build(path string) error {
|
||||||
fmt.Fprintln(b.verbwriter, "building", path)
|
fmt.Fprintln(b.verbwriter, "building", path)
|
||||||
|
|
||||||
ff, err := b.buildfunc(path)
|
ff, err := b.buildfunc(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Function %v built successfully.\n", ff.FullName())
|
fmt.Printf("Function %v built successfully.\n", ff.FullName())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@@ -35,8 +34,8 @@ func (b *bumpcmd) scan(c *cli.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *bumpcmd) walker(path string, info os.FileInfo, err error, w io.Writer) error {
|
func (b *bumpcmd) walker(path string, info os.FileInfo, err error) error {
|
||||||
walker(path, info, err, w, b.bump)
|
walker(path, info, err, b.bump)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,10 +66,10 @@ func (b *bumpcmd) bump(path string) error {
|
|||||||
|
|
||||||
funcfile.Version = newver.String()
|
funcfile.Version = newver.String()
|
||||||
|
|
||||||
err = storefuncfile(path, funcfile)
|
if err := storefuncfile(path, funcfile); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Bumped to version", funcfile.Version)
|
fmt.Println("Bumped to version", funcfile.Version)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -79,26 +78,6 @@ func cleanImageName(name string) string {
|
|||||||
if i := strings.Index(name, ":"); i != -1 {
|
if i := strings.Index(name, ":"); i != -1 {
|
||||||
name = name[:i]
|
name = name[:i]
|
||||||
}
|
}
|
||||||
|
|
||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
func imageversion(image string) (name, ver string) {
|
|
||||||
tagpos := strings.Index(image, ":")
|
|
||||||
if tagpos == -1 {
|
|
||||||
return image, initialVersion
|
|
||||||
}
|
|
||||||
|
|
||||||
imgname, imgver := image[:tagpos], image[tagpos+1:]
|
|
||||||
|
|
||||||
s, err := storage.NewVersionStorage("local", imgver)
|
|
||||||
if err != nil {
|
|
||||||
return imgname, initialVersion
|
|
||||||
}
|
|
||||||
|
|
||||||
version := bumper.NewSemverBumper(s, "")
|
|
||||||
v, err := version.GetCurrentVersion()
|
|
||||||
if err != nil {
|
|
||||||
return imgname, initialVersion
|
|
||||||
}
|
|
||||||
|
|
||||||
return imgname, v.String()
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,29 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func TestImageversion(t *testing.T) {
|
|
||||||
type args struct {
|
|
||||||
image string
|
|
||||||
}
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
args args
|
|
||||||
wantName string
|
|
||||||
wantVer string
|
|
||||||
}{
|
|
||||||
{"tag absent", args{"owner/imagename"}, "owner/imagename", initialVersion},
|
|
||||||
{"non semver tag", args{"owner/imagename:tag"}, "owner/imagename", "0.0.1"},
|
|
||||||
{"semver tag (M.m.p)", args{"owner/imagename:0.0.1"}, "owner/imagename", "0.0.1"},
|
|
||||||
{"semver tag (vM.m.p)", args{"owner/imagename:v0.0.1"}, "owner/imagename", "0.0.1"},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
gotName, gotVer := imageversion(tt.args.image)
|
|
||||||
if gotName != tt.wantName {
|
|
||||||
t.Errorf("%q. imageversion() gotName = %v, want %v", tt.name, gotName, tt.wantName)
|
|
||||||
}
|
|
||||||
if gotVer != tt.wantVer {
|
|
||||||
t.Errorf("%q. imageversion() gotVer = %v, want %v", tt.name, gotVer, tt.wantVer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"text/tabwriter"
|
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -33,12 +32,9 @@ func isFuncfile(path string, info os.FileInfo) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func walker(path string, info os.FileInfo, err error, w io.Writer, f func(path string) error) {
|
func walker(path string, info os.FileInfo, err error, f func(path string) error) {
|
||||||
fmt.Fprint(w, path, "\t")
|
|
||||||
if err := f(path); err != nil {
|
if err := f(path); err != nil {
|
||||||
fmt.Fprintln(w, err)
|
fmt.Fprintln(os.Stderr, path, err)
|
||||||
} else {
|
|
||||||
// fmt.Fprintln(w, "done")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,7 +74,7 @@ func (c *commoncmd) flags() []cli.Flag {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *commoncmd) scan(walker func(path string, info os.FileInfo, err error, w io.Writer) error) {
|
func (c *commoncmd) scan(walker func(path string, info os.FileInfo, err error) error) {
|
||||||
c.verbwriter = ioutil.Discard
|
c.verbwriter = ioutil.Discard
|
||||||
if c.verbose {
|
if c.verbose {
|
||||||
c.verbwriter = os.Stderr
|
c.verbwriter = os.Stderr
|
||||||
@@ -86,11 +82,7 @@ func (c *commoncmd) scan(walker func(path string, info os.FileInfo, err error, w
|
|||||||
|
|
||||||
var walked bool
|
var walked bool
|
||||||
|
|
||||||
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, ' ', 0)
|
|
||||||
// fmt.Fprint(w, "path", "\t", "result", "\n")
|
|
||||||
|
|
||||||
err := filepath.Walk(c.wd, func(path string, info os.FileInfo, err error) error {
|
err := filepath.Walk(c.wd, func(path string, info os.FileInfo, err error) error {
|
||||||
// fmt.Println("walking", info.Name())
|
|
||||||
if !c.recursively && path != c.wd && info.IsDir() {
|
if !c.recursively && path != c.wd && info.IsDir() {
|
||||||
return filepath.SkipDir
|
return filepath.SkipDir
|
||||||
}
|
}
|
||||||
@@ -103,7 +95,7 @@ func (c *commoncmd) scan(walker func(path string, info os.FileInfo, err error, w
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
e := walker(path, info, err, w)
|
e := walker(path, info, err)
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
os.Chtimes(path, now, now)
|
os.Chtimes(path, now, now)
|
||||||
walked = true
|
walked = true
|
||||||
@@ -117,8 +109,6 @@ func (c *commoncmd) scan(walker func(path string, info os.FileInfo, err error, w
|
|||||||
fmt.Println("No function file found.")
|
fmt.Println("No function file found.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Flush()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Theory of operation: this takes an optimistic approach to detect whether a
|
// Theory of operation: this takes an optimistic approach to detect whether a
|
||||||
@@ -146,6 +136,7 @@ func isstale(path string) bool {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
return err != nil
|
return err != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -98,6 +98,7 @@ func (a *initFnCmd) init(c *cli.Context) error {
|
|||||||
if err := encodeFuncfileYAML("func.yaml", ff); err != nil {
|
if err := encodeFuncfileYAML("func.yaml", ff); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("func.yaml created.")
|
fmt.Println("func.yaml created.")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -149,6 +150,7 @@ func detectRuntime(path string) (runtime string, err error) {
|
|||||||
return runtime, nil
|
return runtime, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", fmt.Errorf("no supported files found to guess runtime, please set runtime explicitly with --runtime flag")
|
return "", fmt.Errorf("no supported files found to guess runtime, please set runtime explicitly with --runtime flag")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,5 +159,6 @@ func detectEntrypoint(runtime string) (string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return helper.Entrypoint(), nil
|
return helper.Entrypoint(), nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,8 +12,6 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
yaml "gopkg.in/yaml.v2"
|
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
"github.com/aws/aws-sdk-go/aws/credentials"
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||||
"github.com/aws/aws-sdk-go/aws/session"
|
"github.com/aws/aws-sdk-go/aws/session"
|
||||||
@@ -21,6 +19,7 @@ import (
|
|||||||
"github.com/docker/docker/pkg/jsonmessage"
|
"github.com/docker/docker/pkg/jsonmessage"
|
||||||
lambdaImpl "github.com/iron-io/lambda/lambda"
|
lambdaImpl "github.com/iron-io/lambda/lambda"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
|
yaml "gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
@@ -50,8 +49,8 @@ func (p *publishcmd) scan(c *cli.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *publishcmd) walker(path string, info os.FileInfo, err error, w io.Writer) error {
|
func (p *publishcmd) walker(path string, info os.FileInfo, err error) error {
|
||||||
walker(path, info, err, w, p.publish)
|
walker(path, info, err, p.publish)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,15 +74,10 @@ func (p *publishcmd) publish(path string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := p.route(path, funcfile); err != nil {
|
return p.route(path, funcfile)
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p publishcmd) dockerpush(ff *funcfile) error {
|
func (p publishcmd) dockerpush(ff *funcfile) error {
|
||||||
fmt.Printf("Pushing function %v to Docker Hub.\n", ff.FullName())
|
|
||||||
cmd := exec.Command("docker", "push", ff.FullName())
|
cmd := exec.Command("docker", "push", ff.FullName())
|
||||||
cmd.Stderr = os.Stderr
|
cmd.Stderr = os.Stderr
|
||||||
cmd.Stdout = os.Stdout
|
cmd.Stdout = os.Stdout
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
functions "github.com/iron-io/functions_go"
|
functions "github.com/iron-io/functions_go"
|
||||||
@@ -35,8 +34,8 @@ func (p *pushcmd) scan(c *cli.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *pushcmd) walker(path string, info os.FileInfo, err error, w io.Writer) error {
|
func (p *pushcmd) walker(path string, info os.FileInfo, err error) error {
|
||||||
walker(path, info, err, w, p.push)
|
walker(path, info, err, p.push)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,5 +54,7 @@ func (p *pushcmd) push(path string) error {
|
|||||||
if err := p.dockerpush(funcfile); err != nil {
|
if err := p.dockerpush(funcfile); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Function %v pushed successfully to Docker Hub.\n", funcfile.FullName())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,9 +40,8 @@ func (r *runCmd) run(c *cli.Context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
if _, ok := err.(*notFoundError); ok {
|
if _, ok := err.(*notFoundError); ok {
|
||||||
return errors.New("error: image name is missing or no function file found")
|
return errors.New("error: image name is missing or no function file found")
|
||||||
} else {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
image = ff.FullName()
|
image = ff.FullName()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user