-
Notifications
You must be signed in to change notification settings - Fork 1
/
resources.go
131 lines (118 loc) · 3.72 KB
/
resources.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
package girder
import (
"errors"
"fmt"
"net/url"
"strings"
)
func GetOrCreateFolderRecursive(ctx *Context, path string) (GirderID, error) {
parentID := GirderID(ctx.Destination)
parts := strings.Split(path, "/")
for i, part := range parts {
partialPath := strings.Join(parts[0:i+1], "/")
// check map if this dir has already been made (parents)
if val, ok := ctx.ResourceMap[partialPath]; ok {
if !val.SkipSync && val.GirderID != "" {
parentID = val.GirderID
continue
} else if val.SkipSync {
// skip this folder for the same reason its parent was skipped
ctx.ResourceMap[path].GirderType = "folder"
ctx.ResourceMap[path].SkipSync = true
ctx.ResourceMap[path].SkipReason = ctx.ResourceMap[partialPath].SkipReason
ctx.Logger.Warnf("skipping creation of %s since parent failed", path)
return "", errors.New("parent")
}
}
folder := new(GirderObject)
httpErr := new(GirderError)
url := fmt.Sprintf("folder?parentType=folder&reuseExisting=true&name=%s&parentId=%s", url.QueryEscape(part), string(parentID))
_, err := Post(ctx, url, nil, folder, httpErr)
if err != nil {
ctx.Logger.Errorf("problem creating %s, err: %s", partialPath, err)
ctx.ResourceMap[path].GirderType = "folder"
ctx.ResourceMap[path].SkipSync = true
ctx.ResourceMap[path].SkipReason = err.Error()
return "", err
} else if httpErr.Message != "" {
ctx.Logger.Errorf("problem creating %s, err: %s", partialPath, httpErr.Message)
ctx.ResourceMap[path].GirderType = "folder"
ctx.ResourceMap[path].SkipSync = true
ctx.ResourceMap[path].SkipReason = httpErr.Error()
return "", httpErr
}
parentID = folder.ID
ctx.ResourceMap[path].GirderType = "folder"
ctx.ResourceMap[path].GirderID = folder.ID
}
return parentID, nil
}
func GetOrCreateItem(ctx *Context, folderID GirderID, name string) (GirderID, error) {
obj := new(GirderObject)
httpErr := new(GirderError)
_, err := Post(ctx, fmt.Sprintf("item?folderId=%s&name=%s&reuseExisting=true", folderID, url.QueryEscape(name)), nil, obj, httpErr)
if err != nil {
return "", errors.New(fmt.Sprintf("failed to create item %s, err: %s", name, err))
} else if httpErr.Message != "" {
return "", errors.New(fmt.Sprintf("failed to create item %s, err: %s", name, httpErr.Message))
}
return obj.ID, nil
}
func ItemFiles(ctx *Context, itemID GirderID) []GirderFile {
files := new([]GirderFile)
httpErr := new(GirderError)
_, err := Get(ctx, fmt.Sprintf("item/%s/files", itemID), files, httpErr)
if err != nil {
return nil
} else if httpErr.Message != "" {
fmt.Printf(httpErr.Message)
return nil
}
return *files
}
func Folders(ctx *Context, folderID GirderID) []GirderObject {
folders := make([]GirderObject, 0)
httpErr := new(GirderError)
offset := 0
limit := 50
for {
pageFolders := make([]GirderObject, 0)
_, err := Get(ctx, fmt.Sprintf("folder?parentType=folder&parentId=%s&offset=%d&limit=%d", folderID, offset, limit), &pageFolders, httpErr)
if err != nil {
return nil
} else if httpErr.Message != "" {
fmt.Printf(httpErr.Message)
return nil
}
if len(pageFolders) == 0 {
break
} else {
folders = append(folders, pageFolders...)
}
offset += limit
}
return folders
}
func Items(ctx *Context, folderID GirderID) []GirderObject {
items := make([]GirderObject, 0)
httpErr := new(GirderError)
offset := 0
limit := 50
for {
pageItems := make([]GirderObject, 0)
_, err := Get(ctx, fmt.Sprintf("item?folderId=%s&offset=%d&limit=%d", folderID, offset, limit), &pageItems, httpErr)
if err != nil {
return nil
} else if httpErr.Message != "" {
fmt.Printf(httpErr.Message)
return nil
}
if len(pageItems) == 0 {
break
} else {
items = append(items, pageItems...)
}
offset += limit
}
return items
}