forked from carimura/cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.go
239 lines (206 loc) · 5.43 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
/*
usage: fn init <name>
o If there's a Dockerfile found, this will generate a basic
function file with the image and 'docker' as 'runtime'
like following, for example:
name: hello
version: 0.0.1
runtime: docker
path: /hello
then exit; if 'runtime' is 'docker' in the function file
and no Dockerfile exists, print an error message then exit
o 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 print an error message then exit.
*/
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/fnproject/cli/langs"
"github.com/funcy/functions_go/models"
"github.com/urfave/cli"
)
var (
fileExtToRuntime = map[string]string{
".go": "go",
".js": "node",
".rb": "ruby",
".py": "python",
".php": "php",
".rs": "rust",
".cs": "dotnet",
".fs": "dotnet",
".java": "java",
}
fnInitRuntimes []string
)
func init() {
for rt := range fileExtToRuntime {
fnInitRuntimes = append(fnInitRuntimes, rt)
}
}
type initFnCmd struct {
force bool
funcfile
}
func initFlags(a *initFnCmd) []cli.Flag {
fgs := []cli.Flag{
cli.BoolFlag{
Name: "force",
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,
},
cli.StringFlag{
Name: "cmd",
Usage: "command to run to start this function - equivalent to Dockerfile CMD.",
Destination: &a.Entrypoint,
},
cli.StringFlag{
Name: "version",
Usage: "function version",
Destination: &a.Version,
Value: initialVersion,
},
}
return append(fgs, routeFlags...)
}
func initFn() cli.Command {
a := &initFnCmd{}
// funcfile := &funcfile{}
return cli.Command{
Name: "init",
Usage: "create a local func.yaml file",
Description: "Creates a func.yaml file in the current directory.",
ArgsUsage: "[FUNCTION_NAME]",
Action: a.init,
Flags: initFlags(a),
}
}
func (a *initFnCmd) init(c *cli.Context) error {
rt := &models.Route{}
routeWithFlags(c, rt)
if !a.force {
ff, err := loadFuncfile()
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
}
runtimeSpecified := a.Runtime != ""
if runtimeSpecified && a.Runtime != funcfileDockerRuntime {
err := a.generateBoilerplate()
if err != nil {
return err
}
}
ff := a.funcfile
_, path := appNamePath(ff.ImageName())
ff.Path = path
if err := encodeFuncfileYAML("func.yaml", &ff); err != nil {
return err
}
fmt.Println("func.yaml created")
return nil
}
func (a *initFnCmd) generateBoilerplate() error {
helper := langs.GetLangHelper(a.Runtime)
if helper != nil && helper.HasBoilerplate() {
if err := helper.GenerateBoilerplate(); err != nil {
if err == langs.ErrBoilerplateExists {
return nil
}
return err
}
fmt.Println("function boilerplate generated.")
}
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: %v", err)
}
a.Name = c.Args().First()
// if a.name == "" {
// // return errors.New("please specify a name for your function.\nTry: fn init <FUNCTION_NAME>")
// } else
if a.Name == "" {
// then use current directory for name
a.Name = filepath.Base(pwd)
} else if strings.Contains(a.Name, ":") {
return errors.New("function name cannot contain a colon")
}
//if Dockerfile presents, use 'docker' as 'runtime'
if exists("Dockerfile") {
fmt.Println("Dockerfile found. Using runtime 'docker'")
a.Runtime = funcfileDockerRuntime
return nil
}
if a.Runtime == funcfileDockerRuntime {
return errors.New("function file runtime is 'docker', but no Dockerfile exist !")
}
var rt string
if a.Runtime == "" {
rt, err = detectRuntime(pwd)
if err != nil {
return err
}
a.Runtime = rt
fmt.Printf("Found %v, assuming %v runtime.\n", rt, rt)
} else {
fmt.Println("Runtime:", a.Runtime)
}
helper := langs.GetLangHelper(a.Runtime)
if helper == nil {
fmt.Printf("init does not support the %s runtime, you'll have to create your own Dockerfile for this function", a.Runtime)
}
if a.Entrypoint == "" {
if helper != nil {
a.Entrypoint = helper.Entrypoint()
}
}
if a.Cmd == "" {
if helper != nil {
a.Cmd = helper.Cmd()
}
}
if a.Entrypoint == "" && a.Cmd == "" {
return fmt.Errorf("could not detect entrypoint or cmd for %v, use --entrypoint and/or --cmd to set them explicitly", a.Runtime)
}
return nil
}
func detectRuntime(path string) (runtime string, err error) {
for ext, runtime := range fileExtToRuntime {
filenames := []string{
filepath.Join(path, fmt.Sprintf("func%s", ext)),
filepath.Join(path, fmt.Sprintf("Func%s", ext)),
filepath.Join(path, fmt.Sprintf("src/main%s", ext)), // rust
}
for _, filename := range filenames {
if exists(filename) {
return runtime, nil
}
}
}
return "", fmt.Errorf("no supported files found to guess runtime, please set runtime explicitly with --runtime flag")
}