forked from sirnewton01/gojazz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscmcommon.go
More file actions
934 lines (768 loc) · 20.8 KB
/
scmcommon.go
File metadata and controls
934 lines (768 loc) · 20.8 KB
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"sync"
)
const (
numWalkGoroutines = 10
)
type Path []string
// Check if one path is a prefix of another
func (p Path) isPrefixOf(other Path) bool {
if len(p) > len(other) {
return false
}
for idx, seg := range p {
if seg != other[idx] {
return false
}
}
return true
}
// Convert a path to a string array, which also happens to be a Path.
func pathToArray(p string) Path {
segments := make([]string, 0, 1)
dir := p
for {
for strings.HasSuffix(dir, "/") {
dir = dir[:len(dir)-1]
}
var name string
dir, name = path.Split(dir)
if "" == dir {
break
}
segments = append(segments, name)
}
// And now we reverse result...
result := make([]string, len(segments))
for idx, seg := range segments {
result[len(segments)-1-idx] = seg
}
return result
}
func findSandbox(startingPath string) (p string) {
_, err := os.Stat(startingPath)
if err != nil {
return startingPath
}
p = startingPath
p = filepath.Clean(p)
for p != "." && !strings.HasSuffix(p, "/") {
_, err = os.Stat(filepath.Join(p, metadataFileName))
if err == nil {
return p
}
p = filepath.Dir(p)
}
return startingPath
}
func isSandbox(cwd string) bool {
_, err := os.Stat(filepath.Join(cwd, metadataFileName))
return err == nil
}
func FindRepositoryWorkspace(client *Client, ccmBaseUrl, workspaceName string) (string, error) {
// Fetch all of the user's repository workspaces
url := path.Join(ccmBaseUrl, "/service/com.ibm.team.filesystem.service.jazzhub.IOrionFilesystem/pa")
url = strings.Replace(url, ":/", "://", 1)
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
resp, err := client.Do(request)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", errorFromResponse(resp)
}
// The filesystem service renders the list of workspaces as a directory.
// Decode into a file object so that we can get the workspaces, their names and the item ID's
workspaceList := &FileInfo{}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
err = json.Unmarshal(b, workspaceList)
if err != nil {
return "", err
}
// Return the first workspace that matches the name
for _, w := range workspaceList.Children {
if w.Name == workspaceName {
return w.ScmInfo.ItemId, nil
}
}
return "", nil
}
func FindContributorId(client *Client, ccmBaseUrl string) (string, error) {
// Fetch all of the user's repository workspaces with the flow targets
url := path.Join(ccmBaseUrl, "/service/com.ibm.team.repository.common.internal.IContributorRestService/currentContributor")
url = strings.Replace(url, ":/", "://", 1)
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
request.Header.Add("Accept", "text/json")
resp, err := client.Do(request)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", errorFromResponse(resp)
}
contributor := &soapenv{}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
err = json.Unmarshal(b, contributor)
if err != nil {
return "", err
}
contributorId := contributor.Body.Response.ReturnValue.Value.ItemId
return contributorId, nil
}
type soapenv struct {
Body soapbody `json:"soapenv:Body"`
}
type soapbody struct {
Response soapresponse `json:"response"`
}
type soapresponse struct {
ReturnValue soapreturnvalue `json:"returnValue"`
}
type soapreturnvalue struct {
Value soapvalue `json:"value"`
}
type soapvalue struct {
ItemId string `json:"itemId"`
Items []soapitem `json:"items"`
}
type soapitem struct {
Workspace soapworkspace `json:"workspace"`
}
type soapworkspace struct {
Name string `json:"name"`
Flows []soapworkspaceflow `json:"flows"`
ItemId string `json:"itemId"`
}
type soapworkspaceflow struct {
Flags int `json:"flags"`
TargetWorkspace soapworkspace `json:"targetWorkspace"`
}
func FindWorkspaceForStream(client *Client, ccmBaseUrl string, streamId string) (string, error) {
contributorId, err := FindContributorId(client, ccmBaseUrl)
if err != nil {
return "", err
}
url := path.Join(ccmBaseUrl, "/service/com.ibm.team.scm.common.internal.rest.IScmRestService/workspaces?ownerItemId="+contributorId)
url = strings.Replace(url, ":/", "://", 1)
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
request.Header.Add("Accept", "text/json")
resp, err := client.Do(request)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", errorFromResponse(resp)
}
result := &soapenv{}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
err = json.Unmarshal(b, result)
if err != nil {
return "", err
}
for _, item := range result.Body.Response.ReturnValue.Value.Items {
for _, flow := range item.Workspace.Flows {
if flow.Flags&0x1 == 0x1 && flow.TargetWorkspace.ItemId == streamId {
return item.Workspace.ItemId, nil
}
}
}
return "", nil
}
func FindStream(client *Client, ccmBaseUrl, projectName, streamName string) (string, error) {
// Fetch all of the user's repository workspaces
url := path.Join(ccmBaseUrl, "/service/com.ibm.team.filesystem.service.jazzhub.IOrionFilesystem/pa", projectName)
url = strings.Replace(url, ":/", "://", 1)
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
resp, err := client.Do(request)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", errorFromResponse(resp)
}
// The filesystem service renders the list of streams as a directory.
// Decode into a file object so that we can get the stream, its name and the item ID's
streamList := &FileInfo{}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
err = json.Unmarshal(b, streamList)
if err != nil {
return "", err
}
// Return the first stream that matches the name
for _, s := range streamList.Children {
if s.Name == streamName {
return s.ScmInfo.ItemId, nil
}
}
return "", nil
}
func FindComponentIds(client *Client, ccmBaseUrl string, workspaceId string) ([]string, error) {
result := []string{}
components, err := FindComponents(client, ccmBaseUrl, workspaceId)
if err != nil {
return result, err
}
for _, component := range components {
result = append(result, component.ScmInfo.ItemId)
}
return result, nil
}
func FindComponents(client *Client, ccmBaseUrl string, workspaceId string) ([]FileInfo, error) {
if workspaceId == "" {
return []FileInfo{}, errors.New("No workspace ID provided")
}
url := path.Join(ccmBaseUrl, "/service/com.ibm.team.filesystem.service.jazzhub.IOrionFilesystem/pa/_/", workspaceId)
url = strings.Replace(url, ":/", "://", 1)
result := []FileInfo{}
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return result, err
}
resp, err := client.Do(request)
if err != nil {
return result, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return result, errorFromResponse(resp)
}
// The filesystem service renders the workspace as a directory.
// Decode into a file object so that we can get the components
workspace := &FileInfo{}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return result, err
}
err = json.Unmarshal(b, workspace)
if err != nil {
return result, err
}
result = workspace.Children
return result, nil
}
//type CreateWorkspaceResult struct {
// WorkspaceId string `json:"workspaceId"`
//}
//
//func CreateWorkspaceFromStream(client *Client, ccmBaseUrl string, projectName string, userName string, streamId string, name string) (string, error) {
// // TODO it is completely nonsensical that we have to provide the Orion workspace and userName to create a repository workspace
// url := path.Join(jazzHubBaseUrl, "/code/jazz/Workspace/_/file/", userName+"-OrionContent", projectName)
// url = strings.Replace(url, ":/", "://", 1)
//
// fmt.Printf("URL: %v\n", url)
//
// request, err := http.NewRequest("POST", url, strings.NewReader(`{
// "Create": true,
// "repoUrl": "`+ccmBaseUrl+`",
// "name": "`+name+`",
// "description": "Default Workspace",
// "streamId": "`+streamId+`"
// }`))
// if err != nil {
// return "", err
// }
// addOrionHeaders(request)
//
// resp, err := client.Do(request)
// if err != nil {
// return "", err
// }
//
// result := &CreateWorkspaceResult{}
// err = waitForOrionResponse(client, resp, result)
// if err != nil {
// return "", err
// }
//
// return result.WorkspaceId, nil
//}
type File struct {
client *Client
url string
etag string
info FileInfo
reading io.ReadCloser
}
type FileInfo struct {
Name string
Directory bool
Children []FileInfo
ScmInfo ScmInfo `json:"RTCSCM"`
}
type ScmInfo struct {
ComponentId string
ItemId string
StateId string
}
func assembleOFSUrl(ccmBaseUrl, workspaceId, componentId, p string) string {
ofsUrl, err := url.Parse(ccmBaseUrl)
if err != nil {
panic(err)
}
ofsUrl.Path = path.Join(ofsUrl.Path, "/service/com.ibm.team.filesystem.service.jazzhub.IOrionFilesystem/pa/_", workspaceId, componentId, p)
// TODO figure out why this is having a hard time with "+" characters in filenames
result := ofsUrl.String()
// Workaround for weird IBM DOS bug with the OrionFilesystem
if strings.HasSuffix(result, ".jsp") {
result = result + "derp"
}
return result
}
func Open(client *Client, ccmBaseUrl string, workspaceId string, componentId string, p string) (*File, error) {
f := &File{}
f.client = client
f.url = assembleOFSUrl(ccmBaseUrl, workspaceId, componentId, p)
request, err := http.NewRequest("GET", f.url, nil)
if err != nil {
return nil, err
}
// Workaround for weird IBM DOS bug with the OrionFilesystem
if strings.HasSuffix(f.url, ".jspderp") {
request.Header.Add("X-HasUriSuffix", "true")
}
resp, err := client.Do(request)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
b, _ := ioutil.ReadAll(resp.Body)
body := string(b)
// The service returns 500 instead of 404
if resp.StatusCode == 500 && strings.Contains(body, "Failed to resolve path:") {
return nil, &JazzError{Msg: fmt.Sprintf("Not Found: %v", p), StatusCode: 404}
}
return nil, errorFromResponse(resp)
}
info := &FileInfo{}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, info)
if err != nil {
return nil, err
}
f.info = *info
// The etag returned from the server may have the form W/"c <compSyncTime> ...".
// We want the sync time.
etag := resp.Header.Get("ETag")
etagComponents := strings.Split(etag, "\"")
etag = etagComponents[1]
etagComponents = strings.Split(etag, " ")
etag = etagComponents[1]
f.etag = etag
return f, nil
}
func Create(client *Client, ccmBaseUrl string, workspaceId string, componentId, p string) (*File, error) {
f := &File{}
f.client = client
f.url = assembleOFSUrl(ccmBaseUrl, workspaceId, componentId, p)
parentPath := path.Dir(p)
fileName := path.Base(p)
createUrl := assembleOFSUrl(ccmBaseUrl, workspaceId, componentId, parentPath) + "?op=createFile&name=" + fileName
request, err := http.NewRequest("POST", createUrl, nil)
if err != nil {
return nil, err
}
resp, err := client.Do(request)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
b, _ := ioutil.ReadAll(resp.Body)
body := string(b)
// The service returns 500 instead of 404
if resp.StatusCode == 500 && strings.Contains(body, "Failed to resolve path:") {
return nil, &JazzError{Msg: fmt.Sprintf("Not Found: %v", p), StatusCode: 404}
}
return nil, errorFromResponse(resp)
}
info := &FileInfo{}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, info)
if err != nil {
return nil, err
}
f.info = *info
// The etag returned from the server may have the form W/"c <compSyncTime> ...".
// We want the sync time.
etag := resp.Header.Get("ETag")
etagComponents := strings.Split(etag, "\"")
etag = etagComponents[1]
etagComponents = strings.Split(etag, " ")
etag = etagComponents[1]
f.etag = etag
return f, nil
}
func Mkdir(client *Client, ccmBaseUrl string, workspaceId string, componentId, p string) (*File, error) {
f := &File{}
f.client = client
f.url = assembleOFSUrl(ccmBaseUrl, workspaceId, componentId, p)
parentPath := path.Dir(p)
fileName := path.Base(p)
createUrl := assembleOFSUrl(ccmBaseUrl, workspaceId, componentId, parentPath) + "?op=createFolder&name=" + url.QueryEscape(fileName)
request, err := http.NewRequest("POST", createUrl, nil)
if err != nil {
return nil, err
}
resp, err := client.Do(request)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
b, _ := ioutil.ReadAll(resp.Body)
body := string(b)
// The service returns 500 instead of 404
if resp.StatusCode == 500 && strings.Contains(body, "Failed to resolve path:") {
return nil, &JazzError{Msg: fmt.Sprintf("Not Found: %v", p), StatusCode: 404}
}
return nil, errorFromResponse(resp)
}
info := &FileInfo{}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, info)
if err != nil {
return nil, err
}
f.info = *info
// The etag returned from the server may have the form W/"c <compSyncTime> ...".
// We want the sync time.
etag := resp.Header.Get("ETag")
etagComponents := strings.Split(etag, "\"")
etag = etagComponents[1]
etagComponents = strings.Split(etag, " ")
etag = etagComponents[1]
f.etag = etag
return f, nil
}
func MkdirAll(client *Client, ccmBaseUrl string, workspaceId string, componentId, p string) (*File, error) {
// Walk up the tree to find the first directory that exists
p = path.Clean(p)
dir := p
f, err := Open(client, ccmBaseUrl, workspaceId, componentId, dir)
for {
// We found a file that exists
if err == nil || dir == "/" {
break
}
if err != nil {
jazzError, ok := err.(*JazzError)
if !ok {
return nil, err
}
if jazzError.StatusCode != 404 {
return nil, err
}
}
p = path.Dir(p)
f, err = Open(client, ccmBaseUrl, workspaceId, componentId, p)
}
if p == dir {
return f, nil
}
if !f.info.Directory {
return nil, errors.New("Directory or parent directory is actually a file. Cannot MkdirAll for this path.")
}
// We have the last known existing directory, start creating the children underneath
childrenToCreate := strings.Split(p[len(dir):], "/")
childFile := f
for _, child := range childrenToCreate {
dir = path.Join(dir, child)
childFile, err = Mkdir(client, ccmBaseUrl, workspaceId, componentId, dir)
if err != nil {
return nil, err
}
}
return childFile, nil
}
func Remove(client *Client, ccmBaseUrl string, workspaceId string, componentId string, p string) error {
f := &File{}
f.client = client
f.url = assembleOFSUrl(ccmBaseUrl, workspaceId, componentId, p) + "?op=delete"
request, err := http.NewRequest("POST", f.url, nil)
if err != nil {
return err
}
// Workaround for weird IBM DOS bug with the OrionFilesystem
if strings.HasSuffix(f.url, ".jspderp") {
request.Header.Add("X-HasUriSuffix", "true")
}
resp, err := client.Do(request)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
b, _ := ioutil.ReadAll(resp.Body)
body := string(b)
// The service returns 500 instead of 404
if resp.StatusCode == 500 && strings.Contains(body, "Failed to resolve path:") {
return &JazzError{Msg: fmt.Sprintf("Not Found: %v", p), StatusCode: 404}
}
return errorFromResponse(resp)
}
return nil
}
func (f *File) Read(p []byte) (int, error) {
if f.reading == nil {
request, err := http.NewRequest("GET", f.url+"?op=readContent", nil)
if err != nil {
return 0, err
}
// Workaround for weird IBM DOS bug with the OrionFilesystem
if strings.HasSuffix(f.url, ".jspderp") {
request.Header.Add("X-HasUriSuffix", "true")
}
resp, err := f.client.Do(request)
if err != nil {
return 0, err
}
if resp.StatusCode != 200 {
b, _ := ioutil.ReadAll(resp.Body)
body := string(b)
defer resp.Body.Close()
// The service returns 500 instead of 404
if resp.StatusCode == 500 && strings.Contains(body, "Failed to resolve path:") {
return 0, &JazzError{Msg: fmt.Sprintf("Not Found: %v", f.url), StatusCode: 404}
}
return 0, errorFromResponse(resp)
}
f.reading = resp.Body
}
return f.reading.Read(p)
}
func (f *File) Write(contents io.Reader) error {
request, err := http.NewRequest("POST", f.url+"?op=writeContent", contents)
if err != nil {
return err
}
// Workaround for weird IBM DOS bug with the OrionFilesystem
if strings.HasSuffix(f.url, ".jspderp") {
request.Header.Add("X-HasUriSuffix", "true")
}
resp, err := f.client.Do(request)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
b, _ := ioutil.ReadAll(resp.Body)
body := string(b)
// The service returns 500 instead of 404
if resp.StatusCode == 500 && strings.Contains(body, "Failed to resolve path:") {
return &JazzError{Msg: fmt.Sprintf("Not Found: %v", f.url), StatusCode: 404}
}
return errorFromResponse(resp)
}
info := &FileInfo{}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = json.Unmarshal(b, info)
if err != nil {
return err
}
f.info = *info
return nil
}
func (f *File) Close() error {
if f.reading != nil {
toClose := f.reading
f.reading = nil
return toClose.Close()
}
return nil
}
type WalkFunc func(path string, file File) error
type walkData struct {
client *Client
ccmBaseUrl string
workspaceId string
componentId string
wf WalkFunc
startingEtag string
path string
queue chan walkData
workTracker chan bool
}
func Walk(client *Client, ccmBaseUrl string, workspaceId string, componentId string, metadata *metaData, wf WalkFunc) error {
// Walk doesn't callback for the component root
root, err := Open(client, ccmBaseUrl, workspaceId, componentId, "/")
if err != nil {
return err
}
metadata.componentEtag[root.info.ScmInfo.ItemId] = root.etag
// We track the etag through this whole process to make sure that the configuration
// doesn't change in the middle.
startingEtag := root.etag
walkDataQueue := make(chan walkData)
workTracker := make(chan bool)
finished := make(chan bool)
var firstError error = nil
errMutex := &sync.Mutex{}
go func() {
work := 0
for {
workAdded := <-workTracker
if workAdded {
work += 1
} else {
work -= 1
if work == 0 {
// Send everyone (calling goroutine plus all helpers) the signal that they are finished
for i := 0; i < numWalkGoroutines+1; i++ {
finished <- true
}
return
}
}
}
}()
for i := 0; i < numWalkGoroutines; i++ {
go func() {
for {
select {
case data := <-walkDataQueue:
err := internalWalk(data)
workTracker <- false
if err != nil {
errMutex.Lock()
if firstError == nil {
firstError = err
}
errMutex.Unlock()
}
case <-finished:
return
}
}
}()
}
workTracker <- true
for _, childInfo := range root.info.Children {
p := childInfo.Name
childData := walkData{
client: client,
ccmBaseUrl: ccmBaseUrl,
workspaceId: workspaceId,
componentId: componentId,
wf: wf,
startingEtag: startingEtag,
path: p,
queue: walkDataQueue,
workTracker: workTracker,
}
// Try to push this child on the queue, otherwise simply recurse
// if nobody is listening.
workTracker <- true
select {
case walkDataQueue <- childData:
default:
err = internalWalk(childData)
workTracker <- false
if err != nil {
errMutex.Lock()
if firstError == nil {
firstError = err
}
errMutex.Unlock()
break
}
}
}
workTracker <- false
<-finished
errMutex.Lock()
retVal := firstError
errMutex.Unlock()
return retVal
}
func internalWalk(data walkData) error {
f, err := Open(data.client, data.ccmBaseUrl, data.workspaceId, data.componentId, data.path)
if err != nil {
return err
}
if f.etag != data.startingEtag {
return &JazzError{Msg: "Configuration has changed in the middle of walking the remote file tree"}
}
err = data.wf(data.path, *f)
if err != nil {
return err
}
for _, childInfo := range f.info.Children {
p := path.Join(data.path, childInfo.Name)
childData := walkData{
client: data.client,
ccmBaseUrl: data.ccmBaseUrl,
workspaceId: data.workspaceId,
componentId: data.componentId,
wf: data.wf,
startingEtag: data.startingEtag,
path: p,
queue: data.queue,
workTracker: data.workTracker,
}
// Recurse ourselves if nobody else can take the task
data.workTracker <- true
select {
case data.queue <- childData:
default:
err = internalWalk(childData)
data.workTracker <- false
if err != nil {
return err
}
}
}
return nil
}