-
Notifications
You must be signed in to change notification settings - Fork 1
/
load.go
553 lines (468 loc) · 14.6 KB
/
load.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package main
import (
"bufio"
"crypto/sha1"
"encoding/base64"
"flag"
"fmt"
"io"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
const (
numGoRoutines = 10
bufferSize = 1000
)
func loadDefaults() {
fmt.Printf("gojazz load [<project name> [options]]\n")
flag.PrintDefaults()
}
func loadOp() {
var projectName string
streamDef := ""
stream := &streamDef
workspaceDef := false
workspace := &workspaceDef
// Project name provided
if len(os.Args) > 1 && !strings.HasPrefix(os.Args[1], "-") {
projectName = os.Args[1]
os.Args = os.Args[1:]
// Providing a workspace or stream is only valid in the context of a project
stream = flag.String("stream", "", "Alternate stream to load")
workspace = flag.Bool("workspace", false, "Use a repository workspace to check-in changes (requires authentication).")
}
sandboxPath := flag.String("sandbox", "", "Location of the sandbox to load the files")
force := flag.Bool("force", false, "Force the load to overwrite any files. Don't prompt.")
flag.Usage = loadDefaults
flag.Parse()
if *sandboxPath == "" {
path, err := os.Getwd()
if err != nil {
panic(err)
}
path = findSandbox(path)
sandboxPath = &path
}
// Get the existing status of the sandbox, if available
// Back up any changes that are found
status, _ := scmStatus(*sandboxPath, BACKUP)
if status != nil && !status.unchanged() {
fmt.Printf("Here was the status of your sandbox before loading:\n%v", status)
fmt.Printf("Your changes have been backed up to this location: %v\n", status.copyPath)
}
// You don't need credentials to load streams of public projects
userId := ""
password := ""
// If the user specified a workspace or previously loaded a workspace
// then we will need credentials. If they are already logged in then
// use those credentials.
if *workspace || (status != nil && !status.metaData.isstream) || isLoggedIn() {
var err error
userId, password, err = getCredentials()
if err != nil {
panic(err)
}
}
// Assemble a client with the user credentials
client, err := NewClient(userId, password)
if err != nil {
panic(err)
}
fmt.Printf("Loading into %v...\n", *sandboxPath)
var isstream bool
workspaceId := ""
ccmBaseUrl := ""
// This is either a fresh sandbox or project/stream/workspace information was provided
if status == nil || projectName != "" {
if projectName == "" {
fmt.Println("Provide a project to load and try again.")
loadDefaults()
return
}
project, err := client.findProject(projectName)
if err != nil {
panic(err)
}
ccmBaseUrl = project.CcmBaseUrl
// Find a repository workspace with the correct naming convention
// Failing that, create one.
if *workspace {
isstream = false
streamId := ""
// User has provided a stream that they want to work on
if *stream != "" {
// TODO someday we will support the ability to work on different streams
// streamId, err = FindStream(client, ccmBaseUrl, projectName, *stream)
// if err != nil {
// panic(err)
// }
// if streamId == "" {
// panic(errors.New("Stream with name " + *stream + " not found"))
// }
panic(simpleWarning("Sorry, we don't yet support loading repository workspaces from a specific stream. You can only use the default for now."))
} else {
// Otherwise, use a stream that matches the naming convention
streamId, err = FindStream(client, ccmBaseUrl, projectName, projectName+" Stream")
if err != nil {
// TODO perhaps we should prompt the user in this case?
panic(err)
}
if streamId == "" {
panic(simpleWarning("The default stream for the project could not be found. Is it a Git project?"))
}
}
workspaceId, err = FindWorkspaceForStream(client, ccmBaseUrl, streamId)
if err != nil {
panic(err)
}
if workspaceId == "" {
// TODO someday we will be able to create a repository workspace from a stream, for now we use the init project rest call and hope that the workspace is for the stream the user specified
// workspaceId, err = CreateWorkspaceFromStream(client, ccmBaseUrl, projectName, *userId, streamId, projectName+" Stream")
// if err != nil {
// panic(err)
// }
workspaceId, err = initWebIdeProject(client, project, userId)
if err != nil {
panic(err)
}
}
} else {
isstream = true
// User provided the stream name to load
if *stream != "" {
workspaceId, err = FindStream(client, ccmBaseUrl, projectName, *stream)
if err != nil {
panic(err)
}
if workspaceId == "" {
panic(simpleWarning("Stream with name " + *stream + " not found"))
}
} else {
// Use the stream with the form "user | projectName Stream"
workspaceId, err = FindStream(client, ccmBaseUrl, projectName, projectName+" Stream")
if err != nil {
panic(err)
}
if workspaceId == "" {
panic(simpleWarning("No default stream could be found for this project. Is it a Git project?"))
}
}
}
} else {
projectName = status.metaData.projectName
isstream = status.metaData.isstream
workspaceId = status.metaData.workspaceId
ccmBaseUrl = status.metaData.ccmBaseUrl
}
if isstream {
fmt.Printf("Note: Loading from a stream will not allow you to contribute changes. You must load again using the '-workspace=true' option.\n")
}
scmLoad(client, ccmBaseUrl, projectName, workspaceId, isstream, userId, *sandboxPath, status, *force)
fmt.Printf("Load Successful\n")
// If we loaded from a repository workspace then init the web IDE project and
// provide a URL for them to manage their changes
if !isstream {
project, err := client.findProject(projectName)
if err != nil {
panic(err)
}
// Check if the project is already there, don't initialize it again
webIdeProject, err := findWebIdeProject(client, project)
if err != nil {
panic(err)
}
if webIdeProject == "" {
_, err = initWebIdeProject(client, project, userId)
if err != nil {
panic(err)
}
}
fmt.Println("Visit the following link to work with your repository workspace:")
redirect := fmt.Sprintf(jazzHubBaseUrl + "/code/jazzui/changes.html#" + "/code/jazz/Changes/_/file/" + client.GetJazzId() + "-OrionContent/" + projectName)
fmt.Printf("https://login.jazz.net/psso/proxy/jazzlogin?redirect_uri=%v\n", url.QueryEscape(redirect))
}
}
func scmLoad(client *Client, ccmBaseUrl string, projectName string, workspaceId string, stream bool, userId string, sandbox string, status *status, force bool) {
newMetaData := newMetaData()
newMetaData.initConcurrentWrite()
newMetaData.isstream = stream
newMetaData.userId = userId
newMetaData.ccmBaseUrl = ccmBaseUrl
newMetaData.projectName = projectName
newMetaData.workspaceId = workspaceId
if status != nil {
// Delete any files that were added/modified (they should already be backed up)
for addedPath, _ := range status.Added {
err := os.RemoveAll(filepath.Join(sandbox, addedPath))
if err != nil {
panic(err)
}
}
for modPath, _ := range status.Modified {
err := os.RemoveAll(filepath.Join(sandbox, modPath))
if err != nil {
panic(err)
}
}
} else {
// Check if there are any files in the sandbox, fail if there are any
stat, _ := os.Stat(sandbox)
if stat != nil {
s, err := os.Open(sandbox)
if err != nil {
panic(err)
}
children, err := s.Readdirnames(-1)
if err != nil {
panic(err)
}
if len(children) > 0 && !force {
fmt.Println("There are files in the sandbox directory that will be replaced with the remote files.")
fmt.Print("Do you want to proceed? [Y/n]:")
reader := bufio.NewReader(os.Stdin)
answer, _ := reader.ReadString('\n')
answer = strings.TrimSpace(answer)
if strings.ToLower(answer) == "n" {
panic(simpleWarning("Operation Canceled"))
}
}
}
}
// Delete the old metadata
metadataFile := filepath.Join(sandbox, metadataFileName)
os.Remove(metadataFile)
// Find all of the components of the remote workspace and then walk over each one
componentIds, err := FindComponentIds(client, ccmBaseUrl, workspaceId)
if err != nil {
panic(err)
}
// Walk through the remote components creating directories, if necessary and cleaning up any deleted files
for _, componentId := range componentIds {
loadComponent(client, ccmBaseUrl, workspaceId, componentId, sandbox, newMetaData, status)
}
// Do a final pass over the top-level elements in the sandbox
// to remove any that are no longer registered in the metadata.
s, err := os.Open(sandbox)
if err != nil {
panic(err)
}
roots, err := s.Readdirnames(-1)
if err != nil {
panic(err)
}
for _, root := range roots {
rootPath := filepath.Join(sandbox, root)
ignored, err := IsIgnored(rootPath)
if err != nil {
panic(err)
}
if ignored {
continue
}
_, ok := newMetaData.get(rootPath, sandbox)
if !ok {
err = os.RemoveAll(rootPath)
if err != nil {
panic(err)
}
}
}
newMetaData.save(metadataFile)
}
func loadComponent(client *Client, ccmBaseUrl string, workspaceId string, componentId string, sandbox string, newMetaData *metaData, status *status) {
// Optimization: if status is unchanged and the component's ETag is the same
// then we can skip downloading this component
if status != nil && status.unchanged() {
// TODO implement the optimization
}
// Queue of paths to download (empty string means we are done)
downloadQueue := make(chan string, bufferSize)
// Queue of finished messages from the go routines
finished := make(chan bool)
// Load status updates
trackerFinish := make(chan bool)
workTracker := make(chan bool)
workTransfer := make(chan int64)
go func() {
work := 0
worked := 0
transferred := int64(0)
lastStringLength := 0
for {
select {
case moreBytes := <-workTransfer:
transferred += moreBytes
case added := <-workTracker:
if added {
work += 1
} else {
worked += 1
}
// Backspace and space out the last line that was printed
for i := 0; i < lastStringLength; i++ {
fmt.Printf("\b")
}
for i := 0; i < lastStringLength; i++ {
fmt.Printf(" ")
}
for i := 0; i < lastStringLength; i++ {
fmt.Printf("\b")
}
bytesLoaded := ""
// TODO handle Gigabytes?
if transferred > (1024 * 1024) {
bytesLoaded = strconv.FormatInt(transferred/(1024*1024), 10) + "MB"
} else if transferred > 1024 {
bytesLoaded = strconv.FormatInt(transferred/1024, 10) + "KB"
} else {
bytesLoaded = strconv.FormatInt(transferred, 10) + "B"
}
lastStringLength, _ = fmt.Printf("Loaded %v (of %v) files. %v", worked, work, bytesLoaded)
case <-trackerFinish:
return
}
}
}()
// Downloading gorountine
downloadFiles := func() {
for {
select {
case pathToDownload := <-downloadQueue:
if pathToDownload == "" {
// We're done
finished <- true
return
}
workTracker <- true
remoteFile, err := Open(client, ccmBaseUrl, workspaceId, componentId, pathToDownload)
if err != nil {
// We try one more time with a small timeout
<-time.After(10 * time.Millisecond)
remoteFile, err = Open(client, ccmBaseUrl, workspaceId, componentId, pathToDownload)
if err != nil {
panic(err)
}
}
scmInfo := remoteFile.info.ScmInfo
localPath := filepath.Join(sandbox, pathToDownload)
localSandboxPath := filepath.FromSlash(pathToDownload)
// Optimization: State ID is the same as last time and there were no local modifications
if status != nil && !status.Modified[localSandboxPath] && !status.Deleted[localSandboxPath] {
prevMeta, ok := status.metaData.get(localPath, sandbox)
if ok && prevMeta.StateId == scmInfo.StateId {
// Push the old metadata forward for this file
remoteFile.Close()
newMetaData.put(prevMeta, sandbox)
workTracker <- false
continue
}
}
localFile, err := os.Create(filepath.Join(sandbox, pathToDownload))
if err != nil {
panic(err)
}
// Setup the SHA-1 hash of the file contents
hash := sha1.New()
tee := io.MultiWriter(localFile, hash)
numBytes, err := io.Copy(tee, remoteFile)
if err != nil {
panic(err)
}
workTransfer <- numBytes
localFile.Close()
remoteFile.Close()
stat, _ := os.Stat(localPath)
meta := metaObject{
Path: localPath,
ItemId: scmInfo.ItemId,
StateId: scmInfo.StateId,
ComponentId: scmInfo.ComponentId,
LastModified: stat.ModTime().Unix(),
Size: stat.Size(),
Hash: base64.StdEncoding.EncodeToString(hash.Sum(nil)),
}
newMetaData.put(meta, sandbox)
workTracker <- false
}
}
}
for i := 0; i < numGoRoutines; i++ {
go downloadFiles()
}
err := Walk(client, ccmBaseUrl, workspaceId, componentId, func(p string, file File) error {
localPath := filepath.Join(sandbox, p)
if file.info.Directory {
workTracker <- true
// Create if it doesn't already exist
stat, _ := os.Stat(localPath)
if stat == nil {
err := os.MkdirAll(localPath, 0700)
if err != nil {
return err
}
} else if !stat.IsDir() {
// Weird, there's a file with the same name as the directory in the workspace here
os.Remove(localPath)
err := os.MkdirAll(localPath, 0700)
if err != nil {
return err
}
} else {
// Check for any children that aren't on the remote
// Remove the extra ones that aren't being ignored
localDirectory, err := os.Open(localPath)
if err != nil {
return err
}
localChildren, err := localDirectory.Readdirnames(-1)
for _, localChild := range localChildren {
existsOnRemote := false
for _, remoteChild := range file.info.Children {
if remoteChild.Name == localChild {
existsOnRemote = true
break
}
}
if !existsOnRemote {
localChildPath := filepath.Join(localPath, localChild)
ignored, err := IsIgnored(localChildPath)
if err != nil {
return err
}
if !ignored {
err := os.RemoveAll(localChildPath)
if err != nil {
return err
}
}
}
}
}
// Push the new metadata for this directory
scmInfo := file.info.ScmInfo
meta := metaObject{Path: localPath, ItemId: scmInfo.ItemId, StateId: scmInfo.StateId, ComponentId: scmInfo.ComponentId}
newMetaData.put(meta, sandbox)
workTracker <- false
} else {
// Push the file path into the queue for download (unless the file hasn't changed)
downloadQueue <- p
}
return nil
})
// Send the stop signal to all download routines
for i := 0; i < numGoRoutines; i++ {
downloadQueue <- ""
<-finished
}
// Tell the tracker to finish reporting its status
trackerFinish <- true
// Complete the newline for the progress tracker
fmt.Printf("\n")
if err != nil {
panic(err)
}
}