From 457dc901a05e927181284327685249e42ff5bb30 Mon Sep 17 00:00:00 2001 From: Denis Makogon Date: Thu, 9 Aug 2018 20:52:13 +0300 Subject: [PATCH] [poc] do not merge --- commands/init.go | 67 ++++++++++++++++++++++----------------- common/common.go | 5 ++- objects/fn/fns.go | 41 +++++++++++++----------- objects/route/routes.go | 69 +++++++++++++++++++++++++---------------- 4 files changed, 107 insertions(+), 75 deletions(-) diff --git a/commands/init.go b/commands/init.go index a0585193..01778b0e 100644 --- a/commands/init.go +++ b/commands/init.go @@ -110,6 +110,22 @@ func InitCommand() cli.Command { } } +func pureInit(triggerType, dir string, rt models.Route, ffName, version, entrypoint, runtime, cmd, image, format string, mem uint64, + timeout, idleTimeout int, cfg, ann []string, fn *modelsV2.Fn, ffV2 *common.FuncFileV20180707) error { + //var err error + if triggerType != "" { + var fn modelsV2.Fn + function.FnWithFlags(image, format, mem, timeout, idleTimeout, cfg, ann, &fn) + bindFn(ffV2, &fn) + + return a.initV2(dir, ffName, + version, format, + entrypoint, cmd, + runtime, fn) + } + return nil +} + func (a *initFnCmd) init(c *cli.Context) error { var err error var dir string @@ -121,15 +137,18 @@ func (a *initFnCmd) init(c *cli.Context) error { if a.triggerType != "" { var fn modelsV2.Fn - function.FnWithFlags(c, &fn) - a.bindFn(&fn) + function.FnWithFlagsContext(c, &fn) + bindFn(a.ffV20180707, &fn) - return a.initV2(c, fn) + return a.initV2(dir, c.Args().First(), + c.String("version"), c.String("format"), + c.String("entrypoint"), c.String("cmd"), + c.String("runtime"), fn) } var rt models.Route - route.WithFlags(c, &rt) - a.bindRoute(&rt) + route.WithFlagsContext(c, &rt) + bindRoute(a.ff, &rt) runtime := c.String("runtime") @@ -196,16 +215,10 @@ func (a *initFnCmd) init(c *cli.Context) error { return nil } -func (a *initFnCmd) initV2(c *cli.Context, fn modelsV2.Fn) error { +func (a *initFnCmd) initV2(dir, ffName, version, format, entrypoint, cmd, runtime string, fn modelsV2.Fn) error { var err error - var dir string - - dir = common.GetWd() - if a.wd != "" { - dir = a.wd - } - a.ffV20180707.Name = c.Args().First() + a.ffV20180707.Name = ffName if a.triggerType == "http" { trig := make([]common.Trigger, 1) @@ -217,8 +230,6 @@ func (a *initFnCmd) initV2(c *cli.Context, fn modelsV2.Fn) error { a.ffV20180707.Triggers = trig } - runtime := c.String("runtime") - runtimeSpecified := runtime != "" a.ffV20180707.Schema_version = common.LatestYamlVersion @@ -229,7 +240,7 @@ func (a *initFnCmd) initV2(c *cli.Context, fn modelsV2.Fn) error { } } - path := c.Args().First() + path := ffName if path != "" { fmt.Printf("Creating function at: /%s\n", path) dir = filepath.Join(dir, path) @@ -263,7 +274,10 @@ func (a *initFnCmd) initV2(c *cli.Context, fn modelsV2.Fn) error { return errors.New("Function file already exists, aborting") } } - err = a.BuildFuncFileV20180707(c, dir) // TODO: Return LangHelper here, then don't need to refind the helper in generateBoilerplate() below + err = a.BuildFuncFileV20180707( + version, runtime, + entrypoint, cmd, + format, dir) // TODO: Return LangHelper here, then don't need to refind the helper in generateBoilerplate() below if err != nil { return err } @@ -302,8 +316,7 @@ func (a *initFnCmd) generateBoilerplate(path, runtime string) error { return nil } -func (a *initFnCmd) bindRoute(fn *models.Route) { - ff := a.ff +func bindRoute(ff *common.FuncFile, fn *models.Route) { if fn.Format != "" { ff.Format = fn.Format } @@ -318,8 +331,7 @@ func (a *initFnCmd) bindRoute(fn *models.Route) { } } -func (a *initFnCmd) bindFn(fn *modelsV2.Fn) { - ff := a.ffV20180707 +func bindFn(ff *common.FuncFileV20180707, fn *modelsV2.Fn) { if fn.Format != "" { ff.Format = fn.Format } @@ -442,7 +454,7 @@ func (a *initFnCmd) BuildFuncFile(c *cli.Context, path string) error { return nil } -func (a *initFnCmd) BuildFuncFileV20180707(c *cli.Context, path string) error { +func (a *initFnCmd) BuildFuncFileV20180707(version, runtime, entrypoint, cmd, format string, path string) error { var err error if a.ffV20180707.Name == "" { @@ -450,7 +462,7 @@ func (a *initFnCmd) BuildFuncFileV20180707(c *cli.Context, path string) error { a.ffV20180707.Name = strings.ToLower(filepath.Base(path)) } - a.ffV20180707.Version = c.String("version") + a.ffV20180707.Version = version if err = ValidateFuncName(a.ffV20180707.Name); err != nil { return err } @@ -461,7 +473,6 @@ func (a *initFnCmd) BuildFuncFileV20180707(c *cli.Context, path string) error { a.ff.Runtime = common.FuncfileDockerRuntime return nil } - runtime := c.String("runtime") if runtime == common.FuncfileDockerRuntime { return errors.New("Function file runtime is 'docker', but no Dockerfile exists") } @@ -474,7 +485,7 @@ func (a *initFnCmd) BuildFuncFileV20180707(c *cli.Context, path string) error { } fmt.Printf("Found %v function, assuming %v runtime.\n", helper.Runtime(), helper.Runtime()) //need to default this to default format to be backwards compatible. Might want to just not allow this anymore, fail here. - if c.String("format") == "" { + if format == "" { a.ffV20180707.Format = "default" } } else { @@ -484,7 +495,7 @@ func (a *initFnCmd) BuildFuncFileV20180707(c *cli.Context, path string) error { if helper == nil { fmt.Printf("Init does not support the %s runtime, you'll have to create your own Dockerfile for this function.\n", runtime) } else { - if c.String("entrypoint") == "" { + if entrypoint == "" { a.ffV20180707.Entrypoint, err = helper.Entrypoint() if err != nil { return err @@ -497,11 +508,11 @@ func (a *initFnCmd) BuildFuncFileV20180707(c *cli.Context, path string) error { a.ffV20180707.Runtime = runtime - if c.String("format") == "" { + if format == "" { a.ffV20180707.Format = helper.DefaultFormat() } - if c.String("cmd") == "" { + if cmd == "" { cmd, err := helper.Cmd() if err != nil { return err diff --git a/common/common.go b/common/common.go index b1a215a9..7063ad97 100644 --- a/common/common.go +++ b/common/common.go @@ -528,10 +528,9 @@ func appNamePath(img string) (string, string) { return img[:sep], img[sep : sep+tag] } -// ExtractAnnotations extract annotations from command flags. -func ExtractAnnotations(c *cli.Context) map[string]interface{} { +func ExtractAnnotations(ann []string) map[string]interface{} { annotations := make(map[string]interface{}) - for _, s := range c.StringSlice("annotation") { + for _, s := range ann { parts := strings.Split(s, "=") if len(parts) == 2 { var v interface{} diff --git a/objects/fn/fns.go b/objects/fn/fns.go index 120cd517..86d43077 100644 --- a/objects/fn/fns.go +++ b/objects/fn/fns.go @@ -128,36 +128,41 @@ func (f *fnsCmd) list(c *cli.Context) error { return nil } -// WithFlags returns a function with specified flags -func FnWithFlags(c *cli.Context, fn *models.Fn) { - if i := c.String("image"); i != "" { - fn.Image = i +func FnWithFlags(image, format string, mem uint64, timeout, idleTimeout int, cfg, ann []string, fn *models.Fn) { + if image != "" { + fn.Image = image } - if f := c.String("format"); f != "" { - fn.Format = f + if format != "" { + fn.Format = format } - - if m := c.Uint64("memory"); m > 0 { - fn.Mem = m + if mem > 0 { + fn.Mem = mem } if len(fn.Config) == 0 { - fn.Config = common.ExtractEnvConfig(c.StringSlice("config")) + fn.Config = common.ExtractEnvConfig(cfg) } if len(fn.Annotations) == 0 { - if len(c.StringSlice("annotation")) > 0 { - fn.Annotations = common.ExtractAnnotations(c) + if len(ann) > 0 { + fn.Annotations = common.ExtractAnnotations(ann) } } - if t := c.Int("timeout"); t > 0 { - to := int32(t) + if timeout > 0 { + to := int32(timeout) fn.Timeout = &to } - if t := c.Int("idle-timeout"); t > 0 { - to := int32(t) + if idleTimeout > 0 { + to := int32(idleTimeout) fn.IDLETimeout = &to } } +// WithFlags returns a function with specified flags +func FnWithFlagsContext(c *cli.Context, fn *models.Fn) { + FnWithFlags(c.String("image"), c.String("format"), c.Uint64("memory"), + c.Int("timeout"), c.Int("idle-timeout"), + c.StringSlice("config"), c.StringSlice("annotation"), fn) +} + // WithFuncFile used when creating a function from a funcfile func WithFuncFileV20180707(ff *common.FuncFileV20180707, fn *models.Fn) error { var err error @@ -204,7 +209,7 @@ func (f *fnsCmd) create(c *cli.Context) error { fn.Name = fnName fn.Image = c.Args().Get(2) - FnWithFlags(c, fn) + FnWithFlagsContext(c, fn) if fn.Name == "" { return errors.New("fnName path is missing") @@ -313,7 +318,7 @@ func (f *fnsCmd) update(c *cli.Context) error { return err } - FnWithFlags(c, fn) + FnWithFlagsContext(c, fn) err = PutFn(f.client, fn.ID, fn) if err != nil { diff --git a/objects/route/routes.go b/objects/route/routes.go index 355003a8..391f90f6 100644 --- a/objects/route/routes.go +++ b/objects/route/routes.go @@ -141,63 +141,80 @@ func (r *routesCmd) list(c *cli.Context) error { return nil } +func WithFlagsContext(c *cli.Context, rt *fnmodels.Route) { + image := c.String("image") + format := c.String("format") + fType := c.String("type") + mem := c.Uint64("memory") + cpus := c.String("cpus") + timeout := c.Int("timeout") + idleTimeout := c.Int("idle-timeout") + headers := c.StringSlice("headers") + cfg := c.StringSlice("config") + ann := c.StringSlice("annotation") + WithAttributes(image, format, fType, cpus, mem, timeout, + idleTimeout, headers, cfg, ann, rt) + +} + // WithFlags returns a route with specified flags -func WithFlags(c *cli.Context, rt *fnmodels.Route) { +func WithAttributes(image, format, fType, cpus string, mem uint64, + timeout, idleTimeout int, headers, cfg, ann []string, rt *fnmodels.Route) { if rt.Image == "" { - if i := c.String("image"); i != "" { - rt.Image = i + if image != "" { + rt.Image = image } } if rt.Format == "" { - if f := c.String("format"); f != "" { - rt.Format = f + if format != "" { + rt.Format = format } } if rt.Type == "" { - if t := c.String("type"); t != "" { - rt.Type = t + if fType != "" { + rt.Type = fType } } if rt.Memory == 0 { - if m := c.Uint64("memory"); m > 0 { - rt.Memory = m + if mem > 0 { + rt.Memory = mem } } if rt.Cpus == "" { - if m := c.String("cpus"); m != "" { - rt.Cpus = m + if cpus != "" { + rt.Cpus = cpus } } if rt.Timeout == nil { - if t := c.Int("timeout"); t > 0 { - to := int32(t) + if timeout > 0 { + to := int32(timeout) rt.Timeout = &to } } if rt.IDLETimeout == nil { - if t := c.Int("idle-timeout"); t > 0 { - to := int32(t) + if idleTimeout > 0 { + to := int32(idleTimeout) rt.IDLETimeout = &to } } if len(rt.Headers) == 0 { - if len(c.StringSlice("headers")) > 0 { - headers := map[string][]string{} - for _, header := range c.StringSlice("headers") { + if len(headers) > 0 { + hs := map[string][]string{} + for _, header := range headers { parts := strings.Split(header, "=") - headers[parts[0]] = strings.Split(parts[1], ";") + hs[parts[0]] = strings.Split(parts[1], ";") } - rt.Headers = headers + rt.Headers = hs } } if len(rt.Config) == 0 { - if len(c.StringSlice("config")) > 0 { - rt.Config = common.ExtractEnvConfig(c.StringSlice("config")) + if len(cfg) > 0 { + rt.Config = common.ExtractEnvConfig(cfg) } } if len(rt.Annotations) == 0 { - if len(c.StringSlice("annotation")) > 0 { - rt.Annotations = common.ExtractAnnotations(c) + if len(ann) > 0 { + rt.Annotations = common.ExtractAnnotations(ann) } } } @@ -256,7 +273,7 @@ func (r *routesCmd) create(c *cli.Context) error { rt.Path = route rt.Image = c.Args().Get(2) - WithFlags(c, rt) + WithFlagsContext(c, rt) if rt.Path == "" { return errors.New("Route path is missing") @@ -356,7 +373,7 @@ func (r *routesCmd) update(c *cli.Context) error { rt := &fnmodels.Route{} - WithFlags(c, rt) + WithFlagsContext(c, rt) err := PatchRoute(r.client, appName, route, rt) if err != nil {