1package releasedir
2
3import (
4	"fmt"
5	"os"
6	"path/filepath"
7
8	bosherr "github.com/cloudfoundry/bosh-utils/errors"
9	boshsys "github.com/cloudfoundry/bosh-utils/system"
10)
11
12type FSGenerator struct {
13	dirPath string
14	fs      boshsys.FileSystem
15}
16
17func NewFSGenerator(dirPath string, fs boshsys.FileSystem) FSGenerator {
18	return FSGenerator{dirPath: dirPath, fs: fs}
19}
20
21func (g FSGenerator) GenerateJob(name string) error {
22	jobDirPath := filepath.Join(g.dirPath, "jobs", name)
23
24	if g.fs.FileExists(jobDirPath) {
25		return bosherr.Errorf("Job '%s' at '%s' already exists", name, jobDirPath)
26	}
27
28	err := g.fs.MkdirAll(jobDirPath, os.ModePerm)
29	if err != nil {
30		return bosherr.WrapErrorf(err, "Creating job '%s' dir", name)
31	}
32
33	err = g.fs.MkdirAll(filepath.Join(jobDirPath, "templates"), os.ModePerm)
34	if err != nil {
35		return bosherr.WrapErrorf(err, "Creating job '%s' templates dir", name)
36	}
37
38	specTpl := fmt.Sprintf(`---
39name: %s
40
41templates: {}
42
43packages: []
44
45properties: {}
46`, name)
47
48	err = g.fs.WriteFileString(filepath.Join(jobDirPath, "spec"), specTpl)
49	if err != nil {
50		return bosherr.WrapErrorf(err, "Creating job '%s' spec file", name)
51	}
52
53	err = g.fs.WriteFileString(filepath.Join(jobDirPath, "monit"), "")
54	if err != nil {
55		return bosherr.WrapErrorf(err, "Creating job '%s' monit file", name)
56	}
57
58	return nil
59}
60
61func (g FSGenerator) GeneratePackage(name string) error {
62	pkgDirPath := filepath.Join(g.dirPath, "packages", name)
63
64	if g.fs.FileExists(pkgDirPath) {
65		return bosherr.Errorf("Package '%s' at '%s' already exists", name, pkgDirPath)
66	}
67
68	err := g.fs.MkdirAll(pkgDirPath, os.ModePerm)
69	if err != nil {
70		return bosherr.WrapErrorf(err, "Creating package '%s' dir", name)
71	}
72
73	specTpl := fmt.Sprintf(`---
74name: %s
75
76dependencies: []
77
78files: []
79`, name)
80
81	err = g.fs.WriteFileString(filepath.Join(pkgDirPath, "spec"), specTpl)
82	if err != nil {
83		return bosherr.WrapErrorf(err, "Creating package '%s' spec file", name)
84	}
85
86	err = g.fs.WriteFileString(filepath.Join(pkgDirPath, "packaging"), "set -e\n")
87	if err != nil {
88		return bosherr.WrapErrorf(err, "Creating package '%s' packaging file", name)
89	}
90
91	return nil
92}
93