-
Notifications
You must be signed in to change notification settings - Fork 2
/
project.go
90 lines (73 loc) · 1.76 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
package stackcli
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"os/user"
)
type Project struct {
HomeDir string
ProjectName string
DirectoryName string
TempDir string
}
func (p *Project) CreateTempDir() {
os.MkdirAll(p.TempDir, 0755)
}
func (p *Project) DownloadAndExtractZip() {
zipFile := fmt.Sprintf("%v/chef.zip", p.TempDir)
downloadFile(zipFile, "https://github.com/the-startup-stack/chef-repo-template/archive/master.zip")
unzip(zipFile, p.TempDir)
}
func (p *Project) CopyFiles() {
newDirName := fmt.Sprintf("%s/%s/", p.TempDir, "chef-repo-template-master")
cpCmd := exec.Command("cp", "-rf", newDirName, p.DirectoryName)
err := cpCmd.Run()
if err != nil {
panic("Could not copy files")
}
}
func (p *Project) CopyAndRenameFiles() {
p.CopyFiles()
p.iterateDir(p.DirectoryName)
}
func (p *Project) iterateDir(startDir string) {
dir := fmt.Sprintf("%s/", startDir)
files, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
}
for _, file := range files {
filename := fmt.Sprintf("%s%s", dir, file.Name())
renamer := NewRenamer(p, filename)
newName := renamer.execute()
if file.IsDir() {
p.iterateDir(newName)
}
}
}
func (p *Project) Create() {
p.CreateProjectDir()
p.CreateTempDir()
p.DownloadAndExtractZip()
p.CopyAndRenameFiles()
}
func (p *Project) CreateProjectDir() {
err := os.MkdirAll(p.DirectoryName, 0755)
if err != nil {
panic("Could not create project directory")
}
}
func NewProject(projectName string, directoryName string) *Project {
usr, err := user.Current()
if err != nil {
panic("Could not get current user")
}
return &Project{
HomeDir: usr.HomeDir,
ProjectName: projectName,
DirectoryName: directoryName,
TempDir: fmt.Sprintf("%v/.the-startup-stack", usr.HomeDir),
}
}