/
project.go
295 lines (245 loc) · 6.82 KB
/
project.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package main
import "github.com/hopkinsth/lambda-phage/Godeps/_workspace/src/github.com/spf13/cobra"
import "github.com/hopkinsth/lambda-phage/Godeps/_workspace/src/gopkg.in/yaml.v2"
import "io/ioutil"
import "github.com/hopkinsth/lambda-phage/Godeps/_workspace/src/github.com/tj/go-debug"
import "os"
import "os/user"
import "fmt"
import "crypto/sha1"
import "regexp"
func init() {
prjCmd := &cobra.Command{
Use: "project [command]",
Short: "does project stuff",
}
//flg := prjCmd.Flags()
prjCmd.AddCommand(&cobra.Command{
Use: "create [name]",
Short: "creates a new project with the specified name & adds the current folder to it",
RunE: createProjectCmd,
})
prjCmd.AddCommand(&cobra.Command{
Use: "add [project]",
Short: "adds the current folder to a project",
RunE: addToProjectCmd,
})
deployPrjCmd := &cobra.Command{
Use: "deploy [project1] [project2 ... projectN])",
Short: "deploys all the functions in the given project name",
RunE: deployProjectCmd,
}
deployPrjCmd.Flags().String("filter", "", "pattern for filtering function names to deploy; only does regex!!")
deployPrjCmd.Flags().Bool("dry-run", false, "print what functions we would deploy rather than actually doing it")
prjCmd.AddCommand(deployPrjCmd)
cmds = append(cmds, prjCmd)
}
// loads a project file or creates a new struct for one
func getProject(pName string) (*Project, error) {
var err error
prjDir, err := createProjectDir()
if err != nil {
return nil, err
}
pFilePath := fmt.Sprintf("%s/%s.yml", prjDir, pName)
pCfg, err := openProject(pFilePath)
if err != nil {
return nil, fmt.Errorf("Error getting project file:\n%s\n", err)
}
pCfg.fName = pFilePath
return pCfg, nil
}
// defines the configuration for a project
type Project struct {
// YAML filename for project
fName string
fromFile bool
Functions map[string]ProjectFunction
}
type ProjectFunction struct {
Id string
Config *Config
Path string
}
// writes this to a yaml file
func (p *Project) writeToFile() error {
return writeToYamlFile(p, p.fName)
}
// adds a lambda function to this project
// or maybe not if the config file path
// is
func (p *Project) addFunction(c *Config) *Project {
debug := debug.Debug("project.addFunction")
var nHash string
if c.Name == nil {
debug("bummer, your lambda function doesn't have a name")
return p
}
nHash = fmt.Sprintf("%x", sha1.Sum([]byte(*c.Name+c.fName)))
p.Functions[*c.Name] = ProjectFunction{
Id: nHash,
Config: c,
Path: c.fName,
}
return p
}
// cobra command for creating project
func createProjectCmd(c *cobra.Command, args []string) error {
if len(args) == 0 || args[0] == "" {
return fmt.Errorf("You didn't give us a project name! Please give us one.")
}
pName := args[0]
pCfg, err := getProject(pName)
if err != nil {
fmt.Printf("Error creating or opening project:\n%s\n", err)
return nil
}
// add the current function to the project
if cfg != nil {
pCfg.addFunction(cfg)
cfg.addProject(pName)
err = cfg.writeToFile(cfg.fName)
if err != nil {
fmt.Printf("Error updating config with project:\n%s\n", err)
return nil
}
}
err = pCfg.writeToFile()
if err != nil {
fmt.Printf("Error saving project:\n%s\n", err)
return nil
}
var action string
switch c.Name() {
case "add":
action = fmt.Sprintf("added %s to", *cfg.Name)
default:
action = "created"
}
fmt.Printf("%s project %s\n", action, pName)
return nil
}
// adds the current config file to the project
// uses createProjectCmd now, but will be separate from that
// so we reserve the right to change it whenever
func addToProjectCmd(c *cobra.Command, args []string) error {
if cfg == nil || (cfg != nil && cfg.Name == nil) {
return fmt.Errorf("No project configuration found! Please run `lambda-phage init` first!")
}
// just use create project for now
return createProjectCmd(c, args)
}
// deploys an optionally-filtered set of lambda functions
// for the project(s) you specify
func deployProjectCmd(c *cobra.Command, args []string) error {
debug := debug.Debug("cmd.deployProjectCmd")
if len(args) == 0 {
fmt.Println("Need to have at least one project name to deploy :( Please type one.")
return nil
}
isDryRun, _ := c.Flags().GetBool("dry-run")
filter, _ := c.Flags().GetString("filter")
reg, err := regexp.Compile(filter)
if err != nil {
return fmt.Errorf("Invalid filter, %s\n", err)
}
for _, prj := range args {
pCfg, err := getProject(prj)
if err != nil {
fmt.Printf("Error loading project %s:\n%s\n", prj, err)
} else if pCfg.fromFile == true {
for _, f := range pCfg.Functions {
cfg, err := loadConfig(f.Path)
if err != nil {
fmt.Printf(
"Error loading config for function %s in project %s:\n%s\n",
f.Config.Name,
prj,
err,
)
continue
}
if reg != nil && !reg.MatchString(*cfg.Name) {
// skip current project if it doesn't match regex
continue
}
if isDryRun {
fmt.Printf(
"Would deploy function %s in project %s\n",
*cfg.Name,
prj,
)
continue
}
// if loading config succeeded, deploy this thing
d := deployer{cfg}
err = d.deploy(c, args)
if err != nil {
fmt.Printf(
"Deploy failed for function %s in project %s:\n%s\n",
f.Config.Name,
prj,
err,
)
}
}
} else {
debug("skipped project %s because it wasn't found", prj)
}
}
return nil
}
// parses or creates a project config file
func openProject(fName string) (*Project, error) {
debug := debug.Debug("core.openProject")
// read the project file
b, err := ioutil.ReadFile(fName)
if err != nil && err == os.ErrNotExist {
return nil, fmt.Errorf("Error reading project file: %s\n", err)
}
var pCfg *Project
if b != nil {
debug("have project data from %s, trying to parse it", fName)
// if we got data from the file, parse it
err = yaml.Unmarshal(b, &pCfg)
if err != nil {
return nil, fmt.Errorf("Error parsing project file %s:\n %s\n", fName, err)
}
pCfg.fromFile = true
} else {
debug("no project found, making empty struct")
pCfg = new(Project)
pCfg.fName = fName
pCfg.Functions = make(map[string]ProjectFunction)
}
return pCfg, nil
}
// creates a directory for storing projects
// returns the path to that directory
// or an error if doing something failed
//
// TODO: wtf will this do on windows?
func createProjectDir() (string, error) {
debug := debug.Debug("project.createProjectDir")
u, err := user.Current()
if err != nil {
return "", err
}
debug("got user info; home directory is %s", u.HomeDir)
cfgDir := u.HomeDir + `/.lambda_phage`
prjDir := cfgDir + "/projects"
f, err := os.Stat(prjDir)
if f != nil && f.IsDir() {
return prjDir, nil
}
err = os.Mkdir(prjDir, 0755)
if err != nil {
return "", err
}
prjDir += "/projects"
err = os.Mkdir(prjDir, 0755)
if err != nil {
return "", err
}
return prjDir, nil
}