/
ey-berks.go
195 lines (177 loc) · 5.51 KB
/
ey-berks.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"github.com/ukitazume/ey-berks/attribute"
"github.com/ukitazume/ey-berks/author"
"github.com/ukitazume/ey-berks/config"
"github.com/ukitazume/ey-berks/gather"
)
const (
Version = "0.0.1"
)
func main() {
os.Exit(Command(os.Args[1:]))
}
func usage() string {
return `Engine Yard Cloud cookbook tool like Berkshelf
Usage:
ey-berks config <path> : make a sample configuration file (default path=$PWD, --config=EyBerks)
ey-berks compile <path> --config=<path to EyBerks> : update cahce, write a main/recipes and gather recipe to the cookbooks directory
ey-berks update-cache : update cache of remote repositories cookbooks
ey-berks create-main-recipe <path> : create main recipes from the configration file
ey-berks copy-recipes <path> : copy recipes from the cache dir to the cookbooks/ directory
ey-berks clear <path> : remove EyBerksfile and cookbooks directory
ey-berks gather-attr <path> --from=</path/cookbooks> : gather attbiutes from cookbook directory
ey-berks apply-attr <path> ---from=<attributes directory> : apply attbiutes for cookbook directory
ey-berks help : show this help
ey-berks version : show the version
`
}
func Command(argv []string) int {
command, args, options := parseArgs(argv)
configOptions := config.DefaultOption()
if options["config"] != "" {
configOptions.ConfigFileName = options["config"]
}
path := "./"
if len(args) > 0 {
path = args[0]
}
switch command {
case "version":
fmt.Printf("ey-berks version is: %s", Version)
case "help":
fmt.Print(usage())
return 0
case "clear":
fmt.Println("remove cookbooks and %s at %s ? [y, yes|n, no]", configOptions.ConfigFileName, path)
if askForConfirmation() {
fmt.Println("removing cookbooks/ and %s", configOptions.ConfigFileName)
removeDirs(
filepath.Join(path, configOptions.ConfigFileName),
filepath.Join(path, configOptions.TargetDirName),
)
fmt.Println("removed")
}
return 0
case "config":
fmt.Printf("Creating a sample configuration file, %s at %s\n", configOptions.ConfigFileName, path)
if config.IsExistConfigFile(path, configOptions) {
fmt.Println("Error: The configration file alrady exists")
return 1
}
if err := config.Create(path, configOptions); err != nil {
fmt.Println(err)
return 1
}
case "update-cache":
fmt.Println("Updatint cookbook caches")
berks := config.Parse(path, configOptions)
if err := gather.Gather(path, berks); err != nil {
fmt.Println(err)
}
case "create-main-recipe":
berks := config.Parse(path, configOptions)
list := author.CreateMainRecipe(berks)
if err := author.CreateFile(path, list); err != nil {
fmt.Printf("error: %v\n", err)
}
case "copy-recipes":
berks := config.Parse(path, configOptions)
if err := gather.Copy(path, berks); err != nil {
fmt.Printf("error: %v\n", err)
}
case "compile":
berks := config.Parse(path, configOptions)
if err := gather.Gather(path, berks); err != nil {
fmt.Println(err)
}
list := author.CreateMainRecipe(berks)
if err := author.CreateFile(path, list); err != nil {
fmt.Printf("error: %v\n", err)
}
if err := gather.Copy(path, berks); err != nil {
fmt.Printf("error: %v\n", err)
}
case "apply-attr":
if _, ok := options["from"]; !ok {
fmt.Println("require --from=directory path")
return 1
}
attrOptions := attribute.DefaultOptions()
if err := attribute.Apply(path, options["from"], attrOptions); err != nil {
fmt.Printf("error: %v\n", err)
}
case "gather-attr":
if _, ok := options["from"]; !ok {
fmt.Println("require --from=directory path")
return 1
}
attrOptions := attribute.DefaultOptions()
if err := attribute.Gather(options["from"], path, attrOptions); err != nil {
fmt.Printf("error: %v\n", err)
}
default:
fmt.Println("The command doesn't exist.Please check ey-berks help.")
return 0
}
return 0
}
func askForConfirmation() bool {
var response string
_, err := fmt.Scanln(&response)
if err != nil {
log.Fatal(err)
}
okayResponses := []string{"y", "Y", "yes", "Yes", "YES"}
nokayResponses := []string{"n", "N", "no", "No", "NO"}
if containsString(okayResponses, response) {
return true
} else if containsString(nokayResponses, response) {
return false
} else {
fmt.Println("Please type yes or no and then press enter:")
return askForConfirmation()
}
}
func removeDirs(pathes ...string) {
for _, path := range pathes {
if err := os.RemoveAll(path); err != nil {
fmt.Printf("error: dont' remove because %v\n", err)
}
}
}
func containsString(slice []string, element string) bool {
return !(posString(slice, element) == -1)
}
func posString(slice []string, element string) int {
for index, elem := range slice {
if elem == element {
return index
}
}
return -1
}
func parseArgs(argv []string) (command string, args []string, options map[string]string) {
options = map[string]string{}
if len(argv) >= 1 {
command = argv[0]
} else {
command = "help"
return
}
reg, _ := regexp.Compile("--([a-z]+)=(.+)")
for _, value := range argv[1:] {
if match := reg.FindStringSubmatch(value); len(match) == 3 {
key := match[1]
options[key] = match[2]
} else {
args = append(args, value)
}
}
return
}