forked from iron-io/functions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
165 lines (138 loc) · 3.79 KB
/
init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
/*
usage: fnctl init <name>
If there's a Dockerfile found, this will generate the basic file with just the image name. exit
It will then try to decipher the runtime based on the files in the current directory, if it can't figure it out, it will ask.
It will then take a best guess for what the entrypoint will be based on the language, it it can't guess, it will ask.
*/
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/iron-io/functions/fnctl/langs"
"github.com/urfave/cli"
)
var (
fileExtToRuntime = map[string]string{
".go": "go",
".js": "node",
".rb": "ruby",
}
fnInitRuntimes []string
)
func init() {
for rt := range fileExtToRuntime {
fnInitRuntimes = append(fnInitRuntimes, rt)
}
}
type initFnCmd struct {
name string
force bool
runtime *string
entrypoint *string
}
func initFn() cli.Command {
a := initFnCmd{}
return cli.Command{
Name: "init",
Usage: "create a local func.yaml file",
Description: "Creates a func.yaml file in the current directory. ",
ArgsUsage: "<DOCKERHUB_USERNAME/FUNCTION_NAME>",
Action: a.init,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "force, f",
Usage: "overwrite existing func.yaml",
Destination: &a.force,
},
cli.StringFlag{
Name: "runtime",
Usage: "choose an existing runtime - " + strings.Join(fnInitRuntimes, ", "),
Destination: a.runtime,
},
cli.StringFlag{
Name: "entrypoint",
Usage: "entrypoint is the command to run to start this function - equivalent to Dockerfile ENTRYPOINT.",
Destination: a.entrypoint,
},
},
}
}
func (a *initFnCmd) init(c *cli.Context) error {
if !a.force {
ff, err := findFuncfile()
if _, ok := err.(*notFoundError); !ok && err != nil {
return err
}
if ff != nil {
return errors.New("function file already exists")
}
}
err := a.buildFuncFile(c)
if err != nil {
return err
}
ff := &funcfile{
Name: a.name,
Runtime: a.runtime,
Version: initialVersion,
Entrypoint: a.entrypoint,
}
if err := encodeFuncfileYAML("func.yaml", ff); err != nil {
return err
}
fmt.Println("func.yaml created.")
return nil
}
func (a *initFnCmd) buildFuncFile(c *cli.Context) error {
pwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("error detecting current working directory: %s\n", err)
}
a.name = c.Args().First()
if a.name == "" || strings.Contains(a.name, ":") {
return errors.New("Please specify a name for your function in the following format <DOCKERHUB_USERNAME>/<FUNCTION_NAME>")
}
if exists("Dockerfile") {
fmt.Println("Dockerfile found, will use that to build.")
return nil
}
var rt string
if a.runtime == nil || *a.runtime == "" {
rt, err = detectRuntime(pwd)
if err != nil {
return err
}
a.runtime = &rt
fmt.Printf("assuming %v runtime\n", rt)
}
if _, ok := acceptableFnRuntimes[*a.runtime]; !ok {
return fmt.Errorf("init does not support the %s runtime, you'll have to create your own Dockerfile for this function", *a.runtime)
}
if a.entrypoint == nil || *a.entrypoint == "" {
ep, err := detectEntrypoint(*a.runtime)
if err != nil {
return fmt.Errorf("could not detect entrypoint for %v, use --entrypoint to add it explicitly. %v", *a.runtime, err)
}
a.entrypoint = &ep
}
return nil
}
func detectRuntime(path string) (runtime string, err error) {
for ext, runtime := range fileExtToRuntime {
fn := filepath.Join(path, fmt.Sprintf("func%s", ext))
if exists(fn) {
return runtime, nil
}
}
return "", fmt.Errorf("no supported files found to guess runtime, please set runtime explicitly with --runtime flag")
}
func detectEntrypoint(runtime string) (string, error) {
helper, err := langs.GetLangHelper(runtime)
if err != nil {
return "", err
}
return helper.Entrypoint(), nil
}