forked from cloud66-oss/starter
/
main.go
242 lines (201 loc) · 5.73 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strings"
"github.com/bugsnag/bugsnag-go"
"github.com/cloud66/starter/common"
)
type downloadFile struct {
Url string `json:"url"`
Name string `json:"name"`
}
type templateDefinition struct {
Version string `json:"version"`
Dockerfiles []downloadFile `json:"dockerfiles"`
ServiceYmls []downloadFile `json:"service-ymls"`
}
var (
flagPath string
flagNoPrompt bool
flagEnvironment string
flagTemplates string
flagBranch string
VERSION string = "dev"
BUILD_DATE string = ""
serviceYAMLTemplateDir string
dockerfileTemplateDir string
)
const (
templatePath = "https://raw.githubusercontent.com/cloud66/starter/{{.branch}}/templates/templates.json"
)
func init() {
bugsnag.Configure(bugsnag.Configuration{
APIKey: "916591d12b54e689edde67e641c5843d",
AppVersion: VERSION,
})
flag.StringVar(&flagPath, "p", "", "project path")
flag.BoolVar(&flagNoPrompt, "y", false, "do not prompt user")
flag.StringVar(&flagEnvironment, "e", "production", "set project environment")
flag.StringVar(&flagTemplates, "templates", "", "location of the templates directory")
flag.StringVar(&flagBranch, "branch", "master", "template branch in github")
}
// downloading templates from github and putting them into homedir
func getTempaltes(tempDir string) error {
common.PrintlnL0("Checking templates in %s", tempDir)
var tv templateDefinition
err := fetchJSON(strings.Replace(templatePath, "{{.branch}}", flagBranch, -1), nil, &tv)
if err != nil {
return err
}
// is there a local copy?
if _, err := os.Stat(filepath.Join(tempDir, "templates.json")); os.IsNotExist(err) {
// no file. downloading
common.PrintlnL1("No local templates found. Downloading now.")
err := os.MkdirAll(tempDir, 0777)
if err != nil {
return err
}
err = downloadTemplates(tempDir, tv)
if err != nil {
return err
}
}
// load the local json
templatesLocal, err := ioutil.ReadFile(filepath.Join(tempDir, "templates.json"))
if err != nil {
return err
}
var localTv templateDefinition
err = json.Unmarshal(templatesLocal, &localTv)
if err != nil {
return err
}
// compare
if localTv.Version != tv.Version {
common.PrintlnL2("Newer templates found. Downloading them now")
// they are different, we need to download the new ones
err = downloadTemplates(tempDir, tv)
if err != nil {
return err
}
} else {
common.PrintlnL1("Local templates are up to date")
}
return nil
}
func downloadTemplates(tempDir string, td templateDefinition) error {
err := downloadSingleFile(tempDir, downloadFile{Url: strings.Replace(templatePath, "{{.branch}}", flagBranch, -1), Name: "templates.json"})
if err != nil {
return err
}
for _, temp := range td.Dockerfiles {
err := downloadSingleFile(tempDir, temp)
if err != nil {
return err
}
}
for _, temp := range td.ServiceYmls {
err := downloadSingleFile(tempDir, temp)
if err != nil {
return err
}
}
return nil
}
func downloadSingleFile(tempDir string, temp downloadFile) error {
r, err := fetch(strings.Replace(temp.Url, "{{.branch}}", flagBranch, -1), nil)
if err != nil {
return err
}
defer r.Close()
output, err := os.Create(filepath.Join(tempDir, temp.Name))
if err != nil {
return err
}
defer output.Close()
_, err = io.Copy(output, r)
if err != nil {
return err
}
return nil
}
func main() {
args := os.Args[1:]
if len(args) > 0 && args[0] == "help" {
flag.PrintDefaults()
return
}
if len(args) > 0 && (args[0] == "version" || args[0] == "v") {
fmt.Printf("Cloud 66 Starter (%s)\n", VERSION)
fmt.Println("Copyright 2015 Cloud66 Inc.")
return
}
if len(args) > 0 && args[0] == "version" {
common.PrintlnTitle("Starter version: %s (%s)", VERSION, BUILD_DATE)
return
}
flag.Parse()
common.PrintlnTitle("Cloud 66 Starter ~ (c) 2015 Cloud 66")
if flagPath == "" {
pwd, err := os.Getwd()
if err != nil {
common.PrintlnError("Unable to detect current directory path due to %s", err.Error())
}
flagPath = pwd
}
// if templateFolder is specified we're going to use that otherwise download
if flagTemplates == "" {
usr, _ := user.Current()
homeDir := usr.HomeDir
flagTemplates = filepath.Join(homeDir, ".starter")
err := getTempaltes(flagTemplates)
if err != nil {
common.PrintlnError("Failed to download latest templates due to %s", err.Error())
os.Exit(1)
}
dockerfileTemplateDir = flagTemplates
serviceYAMLTemplateDir = flagTemplates
} else {
common.PrintlnTitle("Using local templates at %s", flagTemplates)
flagTemplates, err := filepath.Abs(flagTemplates)
if err != nil {
common.PrintlnError("Failed to use %s for templates due to %s", flagTemplates, err.Error())
os.Exit(1)
}
dockerfileTemplateDir = flagTemplates
serviceYAMLTemplateDir = flagTemplates
}
common.PrintlnTitle("Detecting framework for the project at %s", flagPath)
pack, err := Detect(flagPath)
if err != nil {
common.PrintlnError("Failed to detect framework due to: %s", err.Error())
return
}
err = pack.Analyze(flagPath, flagEnvironment, !flagNoPrompt)
if err != nil {
common.PrintlnError("Failed to analyze the project due to: %s", err.Error())
return
}
err = pack.WriteDockerfile(dockerfileTemplateDir, flagPath, !flagNoPrompt)
if err != nil {
common.PrintlnError("Failed to write Dockerfile due to: %s", err.Error())
}
err = pack.WriteServiceYAML(serviceYAMLTemplateDir, flagPath, !flagNoPrompt)
if err != nil {
common.PrintlnError("Failed to write service.yml due to: %s", err.Error())
}
if len(pack.GetMessages()) > 0 {
common.PrintlnWarning("Warnings:")
for _, warning := range pack.GetMessages() {
common.PrintlnWarning(" * " + warning)
}
}
common.PrintlnTitle("Done")
}