-
Notifications
You must be signed in to change notification settings - Fork 22
/
steplib_spec.go
153 lines (126 loc) · 4.45 KB
/
steplib_spec.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
package service
import (
"net/http"
"encoding/json"
"fmt"
"github.com/bitrise-io/bitrise-workflow-editor/apiserver/tools"
"github.com/bitrise-io/depman/pathutil"
"github.com/bitrise-io/go-utils/fileutil"
"github.com/bitrise-io/go-utils/log"
stepmanModels "github.com/bitrise-io/stepman/models"
)
// PostSpecRequestBodyModel ...
type PostSpecRequestBodyModel struct {
Libraries []string `json:"libraries,omitempty"`
}
// PostSpecResponseModel ...
type PostSpecResponseModel struct {
LibraryMap map[string]stepmanModels.StepCollectionModel `json:"library_map,omitempty"`
}
func loadSpec(specPth string) (stepmanModels.StepCollectionModel, error) {
if exist, err := pathutil.IsPathExists(specPth); err != nil {
return stepmanModels.StepCollectionModel{}, fmt.Errorf("failed to check if spec exists at: %s, error: %s", specPth, err)
} else if !exist {
return stepmanModels.StepCollectionModel{}, fmt.Errorf("spec not exists at: %s", specPth)
}
specBytes, err := fileutil.ReadBytesFromFile(specPth)
if err != nil {
return stepmanModels.StepCollectionModel{}, fmt.Errorf("failed to read spec at: %s, error: %s", specPth, err)
}
var spec stepmanModels.StepCollectionModel
if err := json.Unmarshal(specBytes, &spec); err != nil {
return stepmanModels.StepCollectionModel{}, fmt.Errorf("failed to serialize spec, error: %s", err)
}
return spec, nil
}
func isLibrarySetup(libraryURI string, libraryInfos []stepmanModels.SteplibInfoModel) bool {
for _, libraryInfo := range libraryInfos {
if libraryInfo.URI == libraryURI {
return true
}
}
return false
}
func libraryInfo(URI string, infos []stepmanModels.SteplibInfoModel) (stepmanModels.SteplibInfoModel, bool) {
for _, info := range infos {
if info.URI == URI {
return info, true
}
}
return stepmanModels.SteplibInfoModel{}, false
}
// PostSpecHandler ...
func PostSpecHandler(w http.ResponseWriter, r *http.Request) {
requestedLibraryURIs := []string{}
if r.Body != nil {
var requestBody PostSpecRequestBodyModel
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&requestBody); err != nil {
log.Errorf("Failed to read request body, error: %s", err)
RespondWithJSONBadRequestErrorMessage(w, "Failed to read request body, error: %s", err)
return
}
requestedLibraryURIs = requestBody.Libraries
}
// Ensure requested libraries are setup locally
libraryInfos, err := tools.StepmanLocalLibraryInfos()
if err != nil {
log.Errorf(err.Error())
RespondWithJSONBadRequestErrorMessage(w, err.Error())
return
}
shouldReloadLocalLibraryInfos := false
for _, libraryURI := range requestedLibraryURIs {
isSetup := isLibrarySetup(libraryURI, libraryInfos)
if !isSetup {
if err := tools.StepmanSetupLibrary(libraryURI); err != nil {
log.Errorf("Failed to setup library (%s), error: %s", libraryURI, err)
RespondWithJSONBadRequestErrorMessage(w, "Failed to setup library (%s), error: %s", libraryURI, err)
return
}
shouldReloadLocalLibraryInfos = true
}
}
if shouldReloadLocalLibraryInfos {
libraryInfos, err = tools.StepmanLocalLibraryInfos()
if err != nil {
log.Errorf(err.Error())
RespondWithJSONBadRequestErrorMessage(w, err.Error())
return
}
}
// --
libraryMap := map[string]stepmanModels.StepCollectionModel{}
if len(requestedLibraryURIs) == 0 {
// If no library URI specified in the request, return all local library
for _, libraryInfo := range libraryInfos {
spec, err := loadSpec(libraryInfo.SpecPath)
if err != nil {
log.Errorf("Failed to load spec of library (%s), error: %s", libraryInfo.URI, err)
RespondWithJSONBadRequestErrorMessage(w, "Failed to load spec of library (%s), error: %s", libraryInfo.URI, err)
return
}
libraryMap[libraryInfo.URI] = spec
}
} else {
for _, libraryURI := range requestedLibraryURIs {
info, found := libraryInfo(libraryURI, libraryInfos)
if !found {
log.Errorf("Failed to find info of library (%s), error: %s", libraryURI, err)
RespondWithJSONBadRequestErrorMessage(w, "Failed to find info of library (%s), error: %s", libraryURI, err)
return
}
spec, err := loadSpec(info.SpecPath)
if err != nil {
log.Errorf("Failed to load spec of library (%s), error: %s", libraryURI, err)
RespondWithJSONBadRequestErrorMessage(w, "Failed to load spec of library (%s), error: %s", libraryURI, err)
return
}
libraryMap[libraryURI] = spec
}
}
response := PostSpecResponseModel{
LibraryMap: libraryMap,
}
RespondWithJSON(w, http.StatusOK, response)
}