Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[poc] do not merge! #374

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 39 additions & 28 deletions commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -442,15 +454,15 @@ 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 == "" {
// then defaults to current directory for name, the name must be lowercase
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
}
Expand All @@ -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")
}
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand Down
5 changes: 2 additions & 3 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
41 changes: 23 additions & 18 deletions objects/fn/fns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down
69 changes: 43 additions & 26 deletions objects/route/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down