-
Notifications
You must be signed in to change notification settings - Fork 12
/
share_create.go
276 lines (231 loc) · 7.65 KB
/
share_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
package cli
import (
"fmt"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"unicode/utf8"
"gopkg.in/yaml.v2"
"github.com/bitrise-io/go-utils/command/git"
"github.com/bitrise-io/go-utils/fileutil"
"github.com/bitrise-io/go-utils/log"
"github.com/bitrise-io/go-utils/pathutil"
"github.com/bitrise-io/go-utils/pointers"
"github.com/bitrise-io/go-utils/retry"
"github.com/bitrise-io/goinp/goinp"
"github.com/bitrise-io/stepman/models"
"github.com/bitrise-io/stepman/stepman"
"github.com/urfave/cli"
)
const maxSummaryLength = 100
func getStepIDFromGit(git string) string {
splits := strings.Split(git, "/")
lastPart := splits[len(splits)-1]
splits = strings.Split(lastPart, ".")
return splits[0]
}
func validateTag(tag string) error {
if tag == "" {
return fmt.Errorf("no Step tag specified")
}
parts := strings.Split(tag, ".")
n := len(parts)
if n != 3 {
return fmt.Errorf("invalid semver format %s: %d parts instead of 3", tag, n)
}
for _, part := range parts {
if _, err := strconv.Atoi(part); err != nil {
return fmt.Errorf("invalid semver format %s: %s", tag, err)
}
}
return nil
}
func getDefaultStepGroupSpec() models.StepGroupInfoModel {
return models.StepGroupInfoModel{
Maintainer: "community",
}
}
func createDefaultStepGroupSpec(route stepman.SteplibRoute, id string) error {
marshalled, err := yaml.Marshal(getDefaultStepGroupSpec())
if err != nil {
return err
}
pth := stepman.GetStepGlobalInfoPath(route, id)
return fileutil.WriteBytesToFile(pth, marshalled)
}
func isStepNew(route stepman.SteplibRoute, id string) (bool, error) {
stepRootDir := path.Dir(stepman.GetStepGlobalInfoPath(route, id))
exists, err := pathutil.IsDirExists(stepRootDir)
if err != nil {
return false, err
}
return !exists, nil
}
func create(c *cli.Context) error {
toolMode := c.Bool(ToolMode)
log.Infof("Validating Step share params...")
share, err := ReadShareSteplibFromFile()
if err != nil {
log.Errorf(err.Error())
failf("You need to start sharing with `stepman share start`, you can read instructions with `stepman share`")
}
// Input validation
tag := c.String(TagKey)
if err := validateTag(tag); err != nil {
failf("validate tag: %s", err)
}
gitURI := c.String(GitKey)
if gitURI == "" {
failf("No Step url specified")
}
stepID := c.String(StepIDKey)
if stepID == "" {
stepID = getStepIDFromGit(gitURI)
}
if stepID == "" {
failf("No Step id specified")
}
r := regexp.MustCompile(`[a-z0-9-]+`)
if find := r.FindString(stepID); find != stepID {
failf("StepID doesn't conform to: [a-z0-9-]+")
}
route, found := stepman.ReadRoute(share.Collection)
if !found {
failf("No route found for collectionURI (%s)", share.Collection)
}
stepDirInSteplib := stepman.GetStepCollectionDirPath(route, stepID, tag)
stepYMLPathInSteplib := filepath.Join(stepDirInSteplib, "step.yml")
if exist, err := pathutil.IsPathExists(stepYMLPathInSteplib); err != nil {
failf("Failed to check step.yml path in steplib, err: %s", err)
} else if exist {
log.Printf("Step already exists in path: %s", stepDirInSteplib)
log.Warnf("Sharing requires to work in a clean Step repository.")
if val, err := goinp.AskForBool("Would you like to overwrite the local version of the Step?"); err != nil {
failf("Failed to get bool, err: %s", err)
} else {
if !val {
log.Errorf("Unfortunately, we can't continue with sharing without overwriting the existing step.yml.")
failf("Please finish your changes, run this command again, and allow it to overwrite the existing step.yml!")
}
}
}
log.Donef("all inputs are valid")
// Clone Step to tmp dir
fmt.Println()
log.Infof("Validating the Step...")
tmp, err := pathutil.NormalizedOSTempDirPath("")
if err != nil {
failf("Failed to get temp directory, err: %s", err)
}
log.Printf("cloning Step repo from (%s) with tag (%s) to: %s", gitURI, tag, tmp)
repo, err := git.New(tmp)
if err != nil {
return err
}
if err := retry.Times(2).Wait(3 * time.Second).Try(func(attempt uint) error {
return repo.CloneTagOrBranch(gitURI, tag).Run()
}); err != nil {
failf("Failed to git-clone (url: %s) version (%s), error: %s",
gitURI, tag, err)
}
// Update step.yml
tmpStepYMLPath := filepath.Join(tmp, "step.yml")
bytes, err := fileutil.ReadBytesFromFile(tmpStepYMLPath)
if err != nil {
failf("Failed to read Step from file, err: %s", err)
}
var stepModel models.StepModel
if err := yaml.Unmarshal(bytes, &stepModel); err != nil {
failf("Failed to unmarshal Step, err: %s", err)
}
commit, err := repo.RevParse("HEAD").RunAndReturnTrimmedCombinedOutput()
if err != nil {
failf("Failed to get commit hash, err: %s", err)
}
stepModel.Source = &models.StepSourceModel{
Git: gitURI,
Commit: commit,
}
stepModel.PublishedAt = pointers.NewTimePtr(time.Now())
// Validate step-yml
if err := stepModel.Audit(); err != nil {
failf("Failed to validate Step, err: %s", err)
}
for _, input := range stepModel.Inputs {
key, value, err := input.GetKeyValuePair()
if err != nil {
failf("Failed to get Step input key-value pair, err: %s", err)
}
options, err := input.GetOptions()
if err != nil {
failf("Failed to get Step input (%s) options, err: %s", key, err)
}
if len(options.ValueOptions) > 0 && value == "" {
log.Warnf("Step input with 'value_options' should contain a default value!")
failf("Missing default value for Step input (%s).", key)
}
}
if strings.Contains(*stepModel.Summary, "\n") {
log.Warnf("Step summary should be one line!")
}
if utf8.RuneCountInString(*stepModel.Summary) > maxSummaryLength {
log.Warnf("Step summary should contain maximum (%d) characters, actual: (%d)!", maxSummaryLength, utf8.RuneCountInString(*stepModel.Summary))
}
log.Donef("step is valid")
// Copy step.yml to steplib
fmt.Println()
log.Infof("Integrating the Step into the Steplib...")
isStepNew, err := isStepNew(route, stepID)
if err != nil {
failf("Failed to check if step is new, err: %s", err)
}
share.StepID = stepID
share.StepTag = tag
if err := WriteShareSteplibToFile(share); err != nil {
failf("Failed to save share steplib to file, err: %s", err)
}
log.Printf("step dir in collection: %s", stepDirInSteplib)
if exist, err := pathutil.IsPathExists(stepDirInSteplib); err != nil {
failf("Failed to check path (%s), err: %s", stepDirInSteplib, err)
} else if !exist {
if err := os.MkdirAll(stepDirInSteplib, 0777); err != nil {
failf("Failed to create path (%s), err: %s", stepDirInSteplib, err)
}
}
collectionDir := stepman.GetLibraryBaseDirPath(route)
steplibRepo, err := git.New(collectionDir)
if err != nil {
failf("Failed to init steplib repo: %s", err)
}
if err := steplibRepo.Checkout(share.ShareBranchName()).Run(); err != nil {
if err := steplibRepo.NewBranch(share.ShareBranchName()).Run(); err != nil {
failf("Git failed to create and checkout branch, err: %s", err)
}
}
stepBytes, err := yaml.Marshal(stepModel)
if err != nil {
failf("Failed to marshal Step model, err: %s", err)
}
if err := fileutil.WriteBytesToFile(stepYMLPathInSteplib, stepBytes); err != nil {
failf("Failed to write Step to file, err: %s", err)
}
if isStepNew {
if err := createDefaultStepGroupSpec(route, stepID); err != nil {
failf("Failed to create step group spec for new step: %s", err)
}
}
log.Printf("your Step (%s@%s) has been added to the local StepLib (%s).", share.StepID, share.StepTag, stepDirInSteplib)
// Update spec.json
if err := stepman.ReGenerateLibrarySpec(route); err != nil {
failf("Failed to re-create steplib, err: %s", err)
}
log.Donef("the StepLib changes are now prepared on branch: %s", share.ShareBranchName())
fmt.Println()
log.Printf(GuideTextForShareFinish(toolMode))
fmt.Println()
return nil
}