Skip to content

Commit

Permalink
refactor fn to seperate common and commands (#657) [skip ci]
Browse files Browse the repository at this point in the history
* refactor fn to seperate common and commands

* remove refactored code

* reorganize imports

* move image commands in images folder
  • Loading branch information
c0ze authored Nov 7, 2017
1 parent 1502994 commit e1c0012
Show file tree
Hide file tree
Showing 28 changed files with 322 additions and 310 deletions.
103 changes: 0 additions & 103 deletions fn/bump.go

This file was deleted.

15 changes: 8 additions & 7 deletions fn/apps.go → fn/commands/apps.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
package main
package commands

import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"strings"

"context"
"github.com/iron-io/functions/fn/common"
"github.com/iron-io/functions_go"
fnclient "github.com/iron-io/functions_go/client"
apiapps "github.com/iron-io/functions_go/client/apps"
"github.com/iron-io/functions_go/models"
"github.com/jmoiron/jsonq"
"github.com/urfave/cli"
"strings"
)

type appsCmd struct {
client *fnclient.Functions
}

func apps() cli.Command {
a := appsCmd{client: apiClient()}
func Apps() cli.Command {
a := appsCmd{client: common.ApiClient()}

return cli.Command{
Name: "apps",
Expand Down Expand Up @@ -125,7 +126,7 @@ func (a *appsCmd) list(c *cli.Context) error {
func (a *appsCmd) create(c *cli.Context) error {
body := &models.AppWrapper{App: &models.App{
Name: c.Args().Get(0),
Config: extractEnvConfig(c.StringSlice("config")),
Config: common.ExtractEnvConfig(c.StringSlice("config")),
}}

resp, err := a.client.Apps.PostApps(&apiapps.PostAppsParams{
Expand Down Expand Up @@ -153,7 +154,7 @@ func (a *appsCmd) update(c *cli.Context) error {
appName := c.Args().First()

patchedApp := &functions.App{
Config: extractEnvConfig(c.StringSlice("config")),
Config: common.ExtractEnvConfig(c.StringSlice("config")),
}

err := a.patchApp(appName, patchedApp)
Expand Down
27 changes: 27 additions & 0 deletions fn/commands/images.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package commands

import (
image_commands "github.com/iron-io/functions/fn/commands/images"
"github.com/iron-io/functions_go"
"github.com/urfave/cli"
)

type imagesCmd struct {
*functions.AppsApi
}

func Images() cli.Command {
return cli.Command{
Name: "images",
Usage: "manage function images",
Subcommands: []cli.Command{
image_commands.Build(),
image_commands.Deploy(),
image_commands.Bump(),
Call(),
image_commands.Push(),
image_commands.Run(),
testfn(),
},
}
}
25 changes: 13 additions & 12 deletions fn/build.go → fn/commands/images/build.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,53 @@
package main
package commands

import (
"fmt"
"os"

"github.com/iron-io/functions/fn/common"
"github.com/urfave/cli"
)

func build() cli.Command {
cmd := buildcmd{}
func Build() cli.Command {
cmd := Buildcmd{}
flags := append([]cli.Flag{}, cmd.flags()...)
return cli.Command{
Name: "build",
Usage: "build function version",
Flags: flags,
Action: cmd.build,
Action: cmd.Build,
}
}

type buildcmd struct {
verbose bool
type Buildcmd struct {
Verbose bool
}

func (b *buildcmd) flags() []cli.Flag {
func (b *Buildcmd) flags() []cli.Flag {
return []cli.Flag{
cli.BoolFlag{
Name: "v",
Usage: "verbose mode",
Destination: &b.verbose,
Destination: &b.Verbose,
},
}
}

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

path, err := os.Getwd()
if err != nil {
return err
}
fn, err := findFuncfile(path)
fn, err := common.FindFuncfile(path)
if err != nil {
return err
}

fmt.Fprintln(verbwriter, "building", fn)
ff, err := buildfunc(verbwriter, fn)
ff, err := common.Buildfunc(verbwriter, fn)
if err != nil {
return err
}
Expand Down
70 changes: 70 additions & 0 deletions fn/commands/images/bump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package commands

import (
"fmt"
"github.com/iron-io/functions/fn/common"
"github.com/urfave/cli"
"os"
)

var (
initialVersion = common.INITIAL_VERSION
)

func Bump() cli.Command {
cmd := bumpcmd{}
flags := append([]cli.Flag{}, cmd.flags()...)
return cli.Command{
Name: "bump",
Usage: "bump function version",
Flags: flags,
Action: cmd.bump,
}
}

type bumpcmd struct {
verbose bool
}

func (b *bumpcmd) flags() []cli.Flag {
return []cli.Flag{
cli.BoolFlag{
Name: "v",
Usage: "verbose mode",
Destination: &b.verbose,
},
}
}

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

path, err := os.Getwd()
if err != nil {
return err
}
fn, err := common.FindFuncfile(path)
if err != nil {
return err
}

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

funcfile, err := common.ParseFuncfile(fn)
if err != nil {
return err
}

err = funcfile.Bumpversion()
if err != nil {
return err
}

if err := common.StoreFuncfile(fn, funcfile); err != nil {
return err
}

fmt.Println("Bumped to version", funcfile.Version)
return nil
}
19 changes: 10 additions & 9 deletions fn/deploy.go → fn/commands/images/deploy.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package commands

import (
"errors"
Expand All @@ -9,11 +9,12 @@ import (
"path/filepath"
"time"

"github.com/iron-io/functions/fn/common"
functions "github.com/iron-io/functions_go"
"github.com/urfave/cli"
)

func deploy() cli.Command {
func Deploy() cli.Command {
cmd := deploycmd{
RoutesApi: functions.NewRoutesApi(),
}
Expand Down Expand Up @@ -69,7 +70,7 @@ func (p *deploycmd) flags() []cli.Flag {

func (p *deploycmd) scan(c *cli.Context) error {
p.appName = c.Args().First()
p.verbwriter = verbwriter(p.verbose)
p.verbwriter = common.Verbwriter(p.verbose)

var walked bool

Expand Down Expand Up @@ -114,7 +115,7 @@ func (p *deploycmd) scan(c *cli.Context) error {
func (p *deploycmd) deploy(path string) error {
fmt.Fprintln(p.verbwriter, "deploying", path)

funcfile, err := buildfunc(p.verbwriter, path)
funcfile, err := common.Buildfunc(p.verbwriter, path)
if err != nil {
return err
}
Expand All @@ -123,20 +124,20 @@ func (p *deploycmd) deploy(path string) error {
return nil
}

if err := dockerpush(funcfile); err != nil {
if err := common.Dockerpush(funcfile); err != nil {
return err
}

return p.route(path, funcfile)
}

func (p *deploycmd) route(path string, ff *funcfile) error {
if err := resetBasePath(p.Configuration); err != nil {
func (p *deploycmd) route(path string, ff *common.Funcfile) error {
if err := common.ResetBasePath(p.Configuration); err != nil {
return fmt.Errorf("error setting endpoint: %v", err)
}

if ff.Path == nil {
_, path := appNamePath(ff.FullName())
_, path := common.AppNamePath(ff.FullName())
ff.Path = &path
}

Expand Down Expand Up @@ -201,7 +202,7 @@ func isFuncfile(path string, info os.FileInfo) bool {
}

basefn := filepath.Base(path)
for _, fn := range validfn {
for _, fn := range common.Validfn {
if basefn == fn {
return true
}
Expand Down
Loading

0 comments on commit e1c0012

Please sign in to comment.