Skip to content

Commit e1c0012

Browse files
authored
refactor fn to seperate common and commands (#657) [skip ci]
* refactor fn to seperate common and commands * remove refactored code * reorganize imports * move image commands in images folder
1 parent 1502994 commit e1c0012

28 files changed

+322
-310
lines changed

fn/bump.go

Lines changed: 0 additions & 103 deletions
This file was deleted.

fn/apps.go renamed to fn/commands/apps.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
package main
1+
package commands
22

33
import (
4+
"context"
45
"encoding/json"
56
"errors"
67
"fmt"
78
"os"
9+
"strings"
810

9-
"context"
11+
"github.com/iron-io/functions/fn/common"
1012
"github.com/iron-io/functions_go"
1113
fnclient "github.com/iron-io/functions_go/client"
1214
apiapps "github.com/iron-io/functions_go/client/apps"
1315
"github.com/iron-io/functions_go/models"
1416
"github.com/jmoiron/jsonq"
1517
"github.com/urfave/cli"
16-
"strings"
1718
)
1819

1920
type appsCmd struct {
2021
client *fnclient.Functions
2122
}
2223

23-
func apps() cli.Command {
24-
a := appsCmd{client: apiClient()}
24+
func Apps() cli.Command {
25+
a := appsCmd{client: common.ApiClient()}
2526

2627
return cli.Command{
2728
Name: "apps",
@@ -125,7 +126,7 @@ func (a *appsCmd) list(c *cli.Context) error {
125126
func (a *appsCmd) create(c *cli.Context) error {
126127
body := &models.AppWrapper{App: &models.App{
127128
Name: c.Args().Get(0),
128-
Config: extractEnvConfig(c.StringSlice("config")),
129+
Config: common.ExtractEnvConfig(c.StringSlice("config")),
129130
}}
130131

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

155156
patchedApp := &functions.App{
156-
Config: extractEnvConfig(c.StringSlice("config")),
157+
Config: common.ExtractEnvConfig(c.StringSlice("config")),
157158
}
158159

159160
err := a.patchApp(appName, patchedApp)

fn/commands/images.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package commands
2+
3+
import (
4+
image_commands "github.com/iron-io/functions/fn/commands/images"
5+
"github.com/iron-io/functions_go"
6+
"github.com/urfave/cli"
7+
)
8+
9+
type imagesCmd struct {
10+
*functions.AppsApi
11+
}
12+
13+
func Images() cli.Command {
14+
return cli.Command{
15+
Name: "images",
16+
Usage: "manage function images",
17+
Subcommands: []cli.Command{
18+
image_commands.Build(),
19+
image_commands.Deploy(),
20+
image_commands.Bump(),
21+
Call(),
22+
image_commands.Push(),
23+
image_commands.Run(),
24+
testfn(),
25+
},
26+
}
27+
}

fn/build.go renamed to fn/commands/images/build.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,53 @@
1-
package main
1+
package commands
22

33
import (
44
"fmt"
55
"os"
66

7+
"github.com/iron-io/functions/fn/common"
78
"github.com/urfave/cli"
89
)
910

10-
func build() cli.Command {
11-
cmd := buildcmd{}
11+
func Build() cli.Command {
12+
cmd := Buildcmd{}
1213
flags := append([]cli.Flag{}, cmd.flags()...)
1314
return cli.Command{
1415
Name: "build",
1516
Usage: "build function version",
1617
Flags: flags,
17-
Action: cmd.build,
18+
Action: cmd.Build,
1819
}
1920
}
2021

21-
type buildcmd struct {
22-
verbose bool
22+
type Buildcmd struct {
23+
Verbose bool
2324
}
2425

25-
func (b *buildcmd) flags() []cli.Flag {
26+
func (b *Buildcmd) flags() []cli.Flag {
2627
return []cli.Flag{
2728
cli.BoolFlag{
2829
Name: "v",
2930
Usage: "verbose mode",
30-
Destination: &b.verbose,
31+
Destination: &b.Verbose,
3132
},
3233
}
3334
}
3435

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

3940
path, err := os.Getwd()
4041
if err != nil {
4142
return err
4243
}
43-
fn, err := findFuncfile(path)
44+
fn, err := common.FindFuncfile(path)
4445
if err != nil {
4546
return err
4647
}
4748

4849
fmt.Fprintln(verbwriter, "building", fn)
49-
ff, err := buildfunc(verbwriter, fn)
50+
ff, err := common.Buildfunc(verbwriter, fn)
5051
if err != nil {
5152
return err
5253
}

fn/commands/images/bump.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"github.com/iron-io/functions/fn/common"
6+
"github.com/urfave/cli"
7+
"os"
8+
)
9+
10+
var (
11+
initialVersion = common.INITIAL_VERSION
12+
)
13+
14+
func Bump() cli.Command {
15+
cmd := bumpcmd{}
16+
flags := append([]cli.Flag{}, cmd.flags()...)
17+
return cli.Command{
18+
Name: "bump",
19+
Usage: "bump function version",
20+
Flags: flags,
21+
Action: cmd.bump,
22+
}
23+
}
24+
25+
type bumpcmd struct {
26+
verbose bool
27+
}
28+
29+
func (b *bumpcmd) flags() []cli.Flag {
30+
return []cli.Flag{
31+
cli.BoolFlag{
32+
Name: "v",
33+
Usage: "verbose mode",
34+
Destination: &b.verbose,
35+
},
36+
}
37+
}
38+
39+
// bump will take the found valid function and bump its version
40+
func (b *bumpcmd) bump(c *cli.Context) error {
41+
verbwriter := common.Verbwriter(b.verbose)
42+
43+
path, err := os.Getwd()
44+
if err != nil {
45+
return err
46+
}
47+
fn, err := common.FindFuncfile(path)
48+
if err != nil {
49+
return err
50+
}
51+
52+
fmt.Fprintln(verbwriter, "bumping version for", fn)
53+
54+
funcfile, err := common.ParseFuncfile(fn)
55+
if err != nil {
56+
return err
57+
}
58+
59+
err = funcfile.Bumpversion()
60+
if err != nil {
61+
return err
62+
}
63+
64+
if err := common.StoreFuncfile(fn, funcfile); err != nil {
65+
return err
66+
}
67+
68+
fmt.Println("Bumped to version", funcfile.Version)
69+
return nil
70+
}

fn/deploy.go renamed to fn/commands/images/deploy.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package commands
22

33
import (
44
"errors"
@@ -9,11 +9,12 @@ import (
99
"path/filepath"
1010
"time"
1111

12+
"github.com/iron-io/functions/fn/common"
1213
functions "github.com/iron-io/functions_go"
1314
"github.com/urfave/cli"
1415
)
1516

16-
func deploy() cli.Command {
17+
func Deploy() cli.Command {
1718
cmd := deploycmd{
1819
RoutesApi: functions.NewRoutesApi(),
1920
}
@@ -69,7 +70,7 @@ func (p *deploycmd) flags() []cli.Flag {
6970

7071
func (p *deploycmd) scan(c *cli.Context) error {
7172
p.appName = c.Args().First()
72-
p.verbwriter = verbwriter(p.verbose)
73+
p.verbwriter = common.Verbwriter(p.verbose)
7374

7475
var walked bool
7576

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

117-
funcfile, err := buildfunc(p.verbwriter, path)
118+
funcfile, err := common.Buildfunc(p.verbwriter, path)
118119
if err != nil {
119120
return err
120121
}
@@ -123,20 +124,20 @@ func (p *deploycmd) deploy(path string) error {
123124
return nil
124125
}
125126

126-
if err := dockerpush(funcfile); err != nil {
127+
if err := common.Dockerpush(funcfile); err != nil {
127128
return err
128129
}
129130

130131
return p.route(path, funcfile)
131132
}
132133

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

138139
if ff.Path == nil {
139-
_, path := appNamePath(ff.FullName())
140+
_, path := common.AppNamePath(ff.FullName())
140141
ff.Path = &path
141142
}
142143

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

203204
basefn := filepath.Base(path)
204-
for _, fn := range validfn {
205+
for _, fn := range common.Validfn {
205206
if basefn == fn {
206207
return true
207208
}

0 commit comments

Comments
 (0)