Skip to content

Commit

Permalink
DeployConfig: combine all CLI flags related to deploy into a DeployCo…
Browse files Browse the repository at this point in the history
…nfig

 DeployConfig represents a set of CLI args (deploy parameters) that are used for "fn deploy"

 DeployConfig makes method signatures shorter and simpler to read and work with
  • Loading branch information
denismakogon committed Sep 9, 2018
1 parent c6ec944 commit 2a3ba0c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 40 deletions.
6 changes: 4 additions & 2 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (b *buildcmd) build(c *cli.Context) error {
}

buildArgs := c.StringSlice("build-arg")
ff, err = common.BuildFuncV20180708(c.GlobalBool("verbose"), fpath, ff, buildArgs, b.noCache)
ff, err = common.BuildFuncV20180708(buildArgs, c.GlobalBool("verbose"), b.noCache, fpath, ff)
if err != nil {
return err
}
Expand All @@ -97,7 +97,9 @@ func (b *buildcmd) build(c *cli.Context) error {
}

buildArgs := c.StringSlice("build-arg")
ff, err = common.BuildFunc(c.GlobalBool("verbose"), fpath, ff, buildArgs, b.noCache)
verbpse := c.GlobalBool("verbose")

ff, err = common.BuildFunc(buildArgs, verbpse, b.noCache, fpath, ff)
if err != nil {
return err
}
Expand Down
40 changes: 20 additions & 20 deletions commands/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,26 +139,33 @@ func (p *deploycmd) deploy(c *cli.Context) error {
return errors.New("App name must be provided, try `--app APP_NAME`")
}

buildArgs := c.StringSlice("build-arg")
verbose := c.GlobalBool("verbose")
noBump := p.noBump
isLocal := p.local
noCache := p.noCache
deployConfig := &DeployConfig{
c.StringSlice("build-arg"),
c.GlobalBool("verbose"),
p.noBump,
p.local,
p.noCache}

appDir := c.String("dir")

if p.all {
return DeployAll(
p.client, buildArgs,
verbose, noBump,
isLocal, noCache,
appDir, appName, appf)
return DeployAll(p.client, deployConfig, appDir, appName, appf)
}
return p.deploySingle(c, appName, appf)

return p.deploySingle(c, deployConfig, appName, appf)
}

type DeployConfig struct {
BuildArgs []string
Verbose bool
NoBump bool
IsLocal bool
NoCache bool
}

// deploySingle deploys a single function, either the current directory or if in the context
// of an app and user provides relative path as the first arg, it will deploy that function.
func (p *deploycmd) deploySingle(c *cli.Context, appName string, appf *common.AppFile) error {
func (p *deploycmd) deploySingle(c *cli.Context, deployConfig *DeployConfig, appName string, appf *common.AppFile) error {
var dir string
wd := common.GetWd()

Expand All @@ -175,14 +182,7 @@ func (p *deploycmd) deploySingle(c *cli.Context, appName string, appf *common.Ap
dir = filepath.Join(wd, fpath)
}

buildArgs := c.StringSlice("build-arg")
verbose := c.GlobalBool("verbose")
noBump := p.noBump
isLocal := p.local
noCache := p.noCache

return DeploySingle(p.client, p.clientV2, buildArgs, verbose,
noBump, isLocal, noCache, dir, appName, appf)
return DeploySingle(p.client, p.clientV2, deployConfig, dir, appName, appf)
}

func setRootFuncInfo(ff *common.FuncFile, appName string) {
Expand Down
28 changes: 14 additions & 14 deletions commands/deploy_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
v2Client "github.com/fnproject/fn_go/clientv2"
)

func DeploySingle(clientV1 *fnclient.Fn, clientV2 *v2Client.Fn, buildArgs []string, verbose bool, noBump, isLocal, noCache bool, dir, appName string, appf *common.AppFile) error {
func DeploySingle(clientV1 *fnclient.Fn, clientV2 *v2Client.Fn, deployConfig *DeployConfig, dir, appName string, appf *common.AppFile) error {
wd := common.GetWd()

err := os.Chdir(dir)
Expand Down Expand Up @@ -45,8 +45,7 @@ func DeploySingle(clientV1 *fnclient.Fn, clientV2 *v2Client.Fn, buildArgs []stri
}
}

return DeployFuncV20180708(clientV1, clientV2, buildArgs, verbose, noBump,
isLocal, noCache, appName, fpath, ff)
return DeployFuncV20180708(clientV1, clientV2, deployConfig, appName, fpath, ff)
default:
fpath, ff, err := common.FindAndParseFuncfile(dir)
if err != nil {
Expand All @@ -65,11 +64,11 @@ func DeploySingle(clientV1 *fnclient.Fn, clientV2 *v2Client.Fn, buildArgs []stri
}
}

return DeployFunc(clientV1, buildArgs, verbose, noBump, isLocal, noCache, appName, fpath, ff)
return DeployFunc(clientV1, deployConfig, appName, fpath, ff)
}
}

func DeployAll(client *fnclient.Fn, buildArgs []string, verbose bool, noBump, isLocal, noCache bool, appDir, appName string, appf *common.AppFile) error {
func DeployAll(client *fnclient.Fn, deployConfig *DeployConfig, appDir, appName string, appf *common.AppFile) error {
if appf != nil {
err := UpdateAppConfig(client, appf)
if err != nil {
Expand Down Expand Up @@ -111,7 +110,7 @@ func DeployAll(client *fnclient.Fn, buildArgs []string, verbose bool, noBump, is
}
}

err = DeployFunc(client, buildArgs, verbose, noBump, isLocal, noCache, appName, path, ff)
err = DeployFunc(client, deployConfig, appName, path, ff)
if err != nil {
return fmt.Errorf("deploy error on %s: %v", path, err)
}
Expand All @@ -132,7 +131,7 @@ func DeployAll(client *fnclient.Fn, buildArgs []string, verbose bool, noBump, is
// Parse func.yaml file, bump version, build image, push to registry, and
// finally it will update function's route. Optionally,
// the route can be overriden inside the func.yaml file.
func DeployFunc(client *fnclient.Fn, buildArgs []string, verbose bool, noBump, isLocal, noCache bool, appName, funcfilePath string, funcfile *common.FuncFile) error {
func DeployFunc(client *fnclient.Fn, deployConfig *DeployConfig, appName, funcfilePath string, funcfile *common.FuncFile) error {
dir := filepath.Dir(funcfilePath)
// get name from directory if it's not defined
if funcfile.Name == "" {
Expand All @@ -148,7 +147,7 @@ func DeployFunc(client *fnclient.Fn, buildArgs []string, verbose bool, noBump, i
}

var err error
if !noBump {
if !deployConfig.NoBump {
funcfile2, err := common.BumpIt(funcfilePath, common.Patch)
if err != nil {
return err
Expand All @@ -158,13 +157,13 @@ func DeployFunc(client *fnclient.Fn, buildArgs []string, verbose bool, noBump, i
}

// p.noCache
_, err = common.BuildFunc(verbose, funcfilePath, funcfile, buildArgs, noCache)
_, err = common.BuildFunc(deployConfig.BuildArgs, deployConfig.Verbose, deployConfig.NoCache, funcfilePath, funcfile)
if err != nil {
return err
}

// p.local
if !isLocal {
if !deployConfig.IsLocal {
if err := common.DockerPush(funcfile); err != nil {
return err
}
Expand All @@ -173,13 +172,13 @@ func DeployFunc(client *fnclient.Fn, buildArgs []string, verbose bool, noBump, i
return updateRoute(client, appName, funcfile)
}

func DeployFuncV20180708(clientV1 *fnclient.Fn, clientV2 *v2Client.Fn, buildArgs []string, verbose bool, noBump, isLocal, noCache bool, appName, funcfilePath string, funcfile *common.FuncFileV20180708) error {
func DeployFuncV20180708(clientV1 *fnclient.Fn, clientV2 *v2Client.Fn, deployConfig *DeployConfig, appName, funcfilePath string, funcfile *common.FuncFileV20180708) error {
if funcfile.Name == "" {
funcfile.Name = filepath.Base(filepath.Dir(funcfilePath)) // todo: should probably make a copy of ff before changing it
}

var err error
if !noBump {
if !deployConfig.NoBump {
funcfile2, err := common.BumpItV20180708(funcfilePath, common.Patch)
if err != nil {
return err
Expand All @@ -188,12 +187,13 @@ func DeployFuncV20180708(clientV1 *fnclient.Fn, clientV2 *v2Client.Fn, buildArgs
// TODO: this whole funcfile handling needs some love, way too confusing. Only bump makes permanent changes to it.
}

_, err = common.BuildFuncV20180708(verbose, funcfilePath, funcfile, buildArgs, noCache)
_, err = common.BuildFuncV20180708(deployConfig.BuildArgs, deployConfig.Verbose,
deployConfig.NoCache, funcfilePath, funcfile)
if err != nil {
return err
}

if !isLocal {
if !deployConfig.IsLocal {
if err := common.DockerPushV20180708(funcfile); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func GetDir(c *cli.Context) string {
}

// BuildFunc bumps version and builds function.
func BuildFunc(verbose bool, fpath string, funcfile *FuncFile, buildArg []string, noCache bool) (*FuncFile, error) {
func BuildFunc(buildArg []string, verbose, noCache bool, fpath string, funcfile *FuncFile) (*FuncFile, error) {
var err error
if funcfile.Version == "" {
funcfile, err = BumpIt(fpath, Patch)
Expand All @@ -77,7 +77,7 @@ func BuildFunc(verbose bool, fpath string, funcfile *FuncFile, buildArg []string
}

// BuildFunc bumps version and builds function.
func BuildFuncV20180708(verbose bool, fpath string, funcfile *FuncFileV20180708, buildArg []string, noCache bool) (*FuncFileV20180708, error) {
func BuildFuncV20180708(buildArg []string, verbose, noCache bool, fpath string, funcfile *FuncFileV20180708) (*FuncFileV20180708, error) {
var err error

if funcfile.Version == "" {
Expand Down
10 changes: 8 additions & 2 deletions run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ func PreRun(c *cli.Context) (string, *common.FuncFile, []string, error) {
}

buildArgs := c.StringSlice("build-arg")
_, err = common.BuildFunc(c.GlobalBool("verbose"), fpath, ff, buildArgs, c.Bool("no-cache"))
verbose := c.GlobalBool("verbose")
noCache := c.Bool("no-cache")

_, err = common.BuildFunc(buildArgs, verbose, noCache, fpath, ff)
if err != nil {
return fpath, nil, nil, err
}
Expand Down Expand Up @@ -207,7 +210,10 @@ func PreRunV20180708(c *cli.Context) (string, *common.FuncFileV20180708, []strin
}

buildArgs := c.StringSlice("build-arg")
_, err = common.BuildFuncV20180708(c.GlobalBool("verbose"), fpath, ff, buildArgs, c.Bool("no-cache"))
verbose := c.GlobalBool("verbose")
noCache := c.Bool("no-cache")

_, err = common.BuildFuncV20180708(buildArgs, verbose, noCache, fpath, ff)
if err != nil {
return fpath, nil, nil, err
}
Expand Down

0 comments on commit 2a3ba0c

Please sign in to comment.