forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs_generator.go
92 lines (68 loc) · 2.06 KB
/
fs_generator.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
package releasedir
import (
"fmt"
"os"
gopath "path"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type FSGenerator struct {
dirPath string
fs boshsys.FileSystem
}
func NewFSGenerator(dirPath string, fs boshsys.FileSystem) FSGenerator {
return FSGenerator{dirPath: dirPath, fs: fs}
}
func (g FSGenerator) GenerateJob(name string) error {
jobDirPath := gopath.Join(g.dirPath, "jobs", name)
if g.fs.FileExists(jobDirPath) {
return bosherr.Errorf("Job '%s' at '%s' already exists", name, jobDirPath)
}
err := g.fs.MkdirAll(jobDirPath, os.ModePerm)
if err != nil {
return bosherr.WrapErrorf(err, "Creating job '%s' dir", name)
}
err = g.fs.MkdirAll(gopath.Join(jobDirPath, "templates"), os.ModePerm)
if err != nil {
return bosherr.WrapErrorf(err, "Creating job '%s' templates dir", name)
}
specTpl := fmt.Sprintf(`---
name: %s
templates: {}
packages: []
properties: {}
`, name)
err = g.fs.WriteFileString(gopath.Join(jobDirPath, "spec"), specTpl)
if err != nil {
return bosherr.WrapErrorf(err, "Creating job '%s' spec file", name)
}
err = g.fs.WriteFileString(gopath.Join(jobDirPath, "monit"), "")
if err != nil {
return bosherr.WrapErrorf(err, "Creating job '%s' monit file", name)
}
return nil
}
func (g FSGenerator) GeneratePackage(name string) error {
pkgDirPath := gopath.Join(g.dirPath, "packages", name)
if g.fs.FileExists(pkgDirPath) {
return bosherr.Errorf("Package '%s' at '%s' already exists", name, pkgDirPath)
}
err := g.fs.MkdirAll(pkgDirPath, os.ModePerm)
if err != nil {
return bosherr.WrapErrorf(err, "Creating package '%s' dir", name)
}
specTpl := fmt.Sprintf(`---
name: %s
dependencies: []
files: []
`, name)
err = g.fs.WriteFileString(gopath.Join(pkgDirPath, "spec"), specTpl)
if err != nil {
return bosherr.WrapErrorf(err, "Creating package '%s' spec file", name)
}
err = g.fs.WriteFileString(gopath.Join(pkgDirPath, "packaging"), "set -e\n")
if err != nil {
return bosherr.WrapErrorf(err, "Creating package '%s' packaging file", name)
}
return nil
}