This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
/
create.go
389 lines (342 loc) · 12 KB
/
create.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*******************************************************************************
* Copyright (c) 2019 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package project
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"github.com/eclipse/codewind-installer/pkg/config"
"github.com/eclipse/codewind-installer/pkg/connections"
"github.com/eclipse/codewind-installer/pkg/apiroutes"
"github.com/eclipse/codewind-installer/pkg/utils"
"github.com/urfave/cli"
)
type (
// ValidationResponse represents the response to validating a project on the users filesystem.
ValidationResponse struct {
Status string `json:"status"`
Path string `json:"projectPath"`
Result interface{} `json:"result"`
}
// CWSettings represents the .cw-settings file which is written to a project
CWSettings struct {
ContextRoot string `json:"contextRoot"`
InternalPort string `json:"internalPort"`
HealthCheck string `json:"healthCheck"`
InternalDebugPort *string `json:"internalDebugPort,omitempty"`
IsHTTPS bool `json:"isHttps"`
IgnoredPaths []string `json:"ignoredPaths"`
MavenProfiles []string `json:"mavenProfiles,omitempty"`
MavenProperties []string `json:"mavenProperties,omitempty"`
StatusPingTimeout string `json:"statusPingTimeout"`
}
)
// DownloadTemplate using the url/link provided
func DownloadTemplate(destination, url string, gitCredentials *utils.GitCredentials) (*Result, *ProjectError) {
projErr := checkProjectDirIsEmpty(destination)
if projErr != nil {
return nil, projErr
}
projectDir := path.Base(destination)
// Remove invalid characters from the string we will use
// as the project name in the template.
r := regexp.MustCompile("[^a-zA-Z0-9._-]")
projectName := r.ReplaceAllString(projectDir, "")
if len(projectName) == 0 {
projectName = "PROJ_NAME_PLACEHOLDER"
}
err := utils.DownloadFromURLThenExtract(url, destination, gitCredentials)
if err != nil {
errOp := errOpCreateProject
// if 401 error, use invalid credentials error code
if err.Error() == http.StatusText(http.StatusUnauthorized) {
errOp = errOpInvalidCredentials
}
return nil, &ProjectError{errOp, err, err.Error()}
}
err = utils.ReplaceInFiles(destination, "[PROJ_NAME_PLACEHOLDER]", projectName)
if err != nil {
return nil, &ProjectError{errOpCreateProject, err, err.Error()}
}
response := Result{Status: "success", StatusMessage: "Project downloaded to" + destination}
return &response, nil
}
// checkIsExtension checks if a project is an extension project and run associated commands as necessary
func checkIsExtension(conID, projectPath string, c *cli.Context) (string, error) {
extensions, err := apiroutes.GetExtensions(conID)
if err != nil {
log.Println("There was a problem retrieving extensions data")
return "unknown", err
}
params := make(map[string]string)
commandName := "postProjectValidate"
// determine if type:subtype hint was given
// but only if url was not given
if c.String("u") == "" && c.String("t") != "" {
parts := strings.Split(c.String("t"), ":")
params["$type"] = parts[0]
if len(parts) > 1 {
params["$subtype"] = parts[1]
}
commandName = "postProjectValidateWithType"
}
for _, extension := range extensions {
var isMatch bool
if len(params) > 0 {
// check if extension project type matched the hinted type
isMatch = extension.ProjectType == params["$type"]
} else {
// check if project contains the detection file an extension defines
isMatch = extension.Detection != "" && utils.PathExists(path.Join(projectPath, extension.Detection))
}
if isMatch {
var cmdErr error
// check if there are any commands to run
for _, command := range extension.Commands {
if command.Name == commandName {
cmdErr = utils.RunCommand(projectPath, command, params)
break
}
}
return extension.ProjectType, cmdErr
}
}
return "", nil
}
// ValidateProject returns the language and buildType for a project at given filesystem path,
// and writes a default .cw-settings file to that project
func ValidateProject(c *cli.Context) (*ValidationResponse, *ProjectError) {
projectPath := c.String("path")
conID := strings.TrimSpace(strings.ToLower(c.String("conid")))
projErr := checkProjectPathExists(projectPath)
if projErr != nil {
return nil, projErr
}
validationStatus := "success"
// result could be ProjectType or string, so define as an interface
var validationResult interface{}
language, buildType := determineProjectInfo(projectPath)
validationResult = ProjectType{
Language: language,
BuildType: buildType,
}
extensionType, err := checkIsExtension(conID, projectPath, c)
if extensionType != "" {
if err == nil {
validationResult = ProjectType{
Language: language,
BuildType: extensionType,
}
} else {
validationStatus = "failed"
validationResult = err.Error()
}
}
response := ValidationResponse{
Status: validationStatus,
Path: projectPath,
Result: validationResult,
}
if err != nil {
return nil, &ProjectError{errOpCreateProject, err, err.Error()}
}
writeErr := writeCwSettingsIfNotInProject(conID, projectPath, buildType)
if writeErr != nil {
return nil, writeErr
}
return &response, nil
}
func writeCwSettingsIfNotInProject(conID string, projectPath string, BuildType string) *ProjectError {
pathToCwSettings := path.Join(projectPath, ".cw-settings")
pathToLegacySettings := path.Join(projectPath, ".mc-settings")
connection, conErr := connections.GetConnectionByID(conID)
if conErr != nil {
return &ProjectError{errOpCreateProject, conErr, conErr.Desc}
}
conURL, configErr := config.PFEOriginFromConnection(connection)
if configErr != nil {
return &ProjectError{errOpCreateProject, conErr, configErr.Desc}
}
if _, err := os.Stat(pathToLegacySettings); os.IsExist(err) {
projErr := renameLegacySettings(pathToLegacySettings, pathToCwSettings)
if projErr != nil {
return projErr
}
} else if _, err := os.Stat(pathToCwSettings); os.IsNotExist(err) {
projErr := writeNewCwSettings(&http.Client{}, connection, conURL, pathToCwSettings, BuildType)
if projErr != nil {
return projErr
}
}
return nil
}
// checkProjectDirIsEmpty return a project error if the given local filepath already exists, or is an empty string
func checkProjectDirIsEmpty(projectPath string) *ProjectError {
if projectPath == "" {
err := errors.New(textNoProjectPath)
return &ProjectError{errOpCreateProject, err, err.Error()}
}
// if the project dir already exists, continue if empty and exit if not
if utils.PathExists(projectPath) {
dirIsEmpty, err := utils.DirIsEmpty(projectPath)
if err != nil {
return &ProjectError{errOpCreateProject, err, err.Error()}
}
if !dirIsEmpty {
projErr := errors.New(textProjectPathNonEmpty)
return &ProjectError{errOpCreateProject, projErr, projErr.Error()}
}
}
return nil
}
// checkProjectPathExists returns a project error if the given local filepath does not exist, or is an empty string
func checkProjectPathExists(projectPath string) *ProjectError {
if projectPath == "" {
err := errors.New(textNoProjectPath)
return &ProjectError{errOpCreateProject, err, err.Error()}
}
if !utils.PathExists(projectPath) {
err := errors.New(textProjectPathDoesNotExist)
return &ProjectError{errOpCreateProject, err, err.Error()}
}
return nil
}
// determineProjectInfo returns the language and build-type of a project
func determineProjectInfo(projectPath string) (string, string) {
language, buildType := "unknown", "docker"
if utils.PathExists(path.Join(projectPath, "pom.xml")) {
language = "java"
buildType = determineJavaBuildType(projectPath)
} else if utils.PathExists(path.Join(projectPath, "package.json")) {
language = "javascript"
buildType = "nodejs"
} else if utils.PathExists(path.Join(projectPath, "Package.swift")) {
language = "swift"
buildType = "swift"
} else {
determinedLanguage, err := determineProjectLanguage(projectPath)
if err != nil {
language = "unknown"
}
language = determinedLanguage
buildType = "docker"
}
return language, buildType
}
func determineJavaBuildType(projectPath string) string {
pathToPomXML := path.Join(projectPath, "pom.xml")
pomXMLContents, err := ioutil.ReadFile(pathToPomXML)
// if there is an error reading the pom.xml, we build as docker
if err != nil {
return "docker"
}
pomXMLString := string(pomXMLContents)
if strings.Contains(pomXMLString, "<groupId>org.springframework.boot</groupId>") {
return "spring"
}
pathToDockerfile := path.Join(projectPath, "Dockerfile")
dockerfileContents, err := ioutil.ReadFile(pathToDockerfile)
dockerfileString := string(dockerfileContents)
if strings.Contains(dockerfileString, "FROM websphere-liberty") {
return "liberty"
}
return "docker"
}
func determineProjectLanguage(projectPath string) (string, *ProjectError) {
projectFiles, err := ioutil.ReadDir(projectPath)
if err != nil {
return "", &ProjectError{errOpCreateProject, err, err.Error()}
}
for _, file := range projectFiles {
if !file.IsDir() {
switch filepath.Ext(file.Name()) {
case ".py":
return "python", nil
case ".go":
return "go", nil
default:
continue
}
}
}
return "unknown", nil
}
// RenameLegacySettings renames a .mc-settings file to .cw-settings
func renameLegacySettings(pathToLegacySettings string, pathToCwSettings string) *ProjectError {
err := os.Rename(pathToLegacySettings, pathToCwSettings)
if err != nil {
return &ProjectError{errOpCreateProject, err, err.Error()}
}
return nil
}
// writeNewCwSettings writes a default .cw-settings file to the given path,
// dependant on the build type of the project
func writeNewCwSettings(httpClient utils.HTTPClient, connection *connections.Connection, conURL string, pathToCwSettings string, BuildType string) *ProjectError {
defaultCwSettings, projErr := getDefaultCwSettings(httpClient, connection, conURL, BuildType)
if projErr != nil {
return projErr
}
cwSettings := addNonDefaultFieldsToCwSettings(defaultCwSettings, BuildType)
settings, err := json.MarshalIndent(cwSettings, "", " ")
if err != nil {
return &ProjectError{errOpCreateProject, err, err.Error()}
}
// File permission 0644 grants read and write access to the owner
err = ioutil.WriteFile(pathToCwSettings, settings, 0644)
if err != nil {
return &ProjectError{errOpCreateProject, err, err.Error()}
}
return nil
}
func getDefaultCwSettings(httpClient utils.HTTPClient, connection *connections.Connection, conURL string, BuildType string) (CWSettings, *ProjectError) {
IgnoredPaths, err := apiroutes.GetIgnoredPaths(httpClient, connection, BuildType, conURL)
if err != nil {
// If error getting the default ignoredPaths, set as empty slice
IgnoredPaths = []string{}
}
return CWSettings{
ContextRoot: "",
InternalPort: "",
HealthCheck: "",
IsHTTPS: false,
IgnoredPaths: IgnoredPaths,
StatusPingTimeout: "",
}, nil
}
func addNonDefaultFieldsToCwSettings(cwSettings CWSettings, ProjectType string) CWSettings {
projectTypesWithInternalDebugPort := []string{"liberty", "spring", "nodejs"}
projectTypesWithMavenSettings := []string{"liberty", "spring"}
if stringInSlice(ProjectType, projectTypesWithInternalDebugPort) {
// We use a pointer, as an empty string would be removed due to omitempty on struct
defaultValue := ""
cwSettings.InternalDebugPort = &defaultValue
}
if stringInSlice(ProjectType, projectTypesWithMavenSettings) {
cwSettings.MavenProfiles = []string{""}
cwSettings.MavenProperties = []string{""}
}
return cwSettings
}
func stringInSlice(a string, slice []string) bool {
for _, b := range slice {
if b == a {
return true
}
}
return false
}