forked from ForceCLI/force
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.go
344 lines (309 loc) · 9.53 KB
/
fetch.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
package main
import (
"archive/zip"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
var cmdFetch = &Command{
Usage: "fetch -t ApexClass",
Short: "Export specified artifact(s) to a local directory",
Long: `
-t, -type # type of metadata to retrieve
-n, -name # name of specific metadata to retrieve (must be used with -type)
-d, -directory # override the default target directory
-u, -unpack # unpack any zipped static resources (ignored if type is not StaticResource)
-p, -preserve # preserve the zip file
Export specified artifact(s) to a local directory. Use "package" type to retrieve an unmanaged package.
Examples
force fetch -t=CustomObject n=Book__c n=Author__c
force fetch -t Aura -n MyComponent -d /Users/me/Documents/Project/home
`,
}
type metaName []string
func (i *metaName) String() string {
return fmt.Sprint(*i)
}
func (i *metaName) Set(value string) error {
// That would permit usages such as
// -deltaT 10s -deltaT 15s
for _, name := range strings.Split(value, ",") {
*i = append(*i, name)
}
return nil
}
var (
metadataType string
targetDirectory string
unpack bool
metadataName metaName
makefile bool
preserveZip bool
mdbase string
)
func init() {
cmdFetch.Flag.Var(&metadataName, "name", "names of metadata")
cmdFetch.Flag.Var(&metadataName, "n", "names of metadata")
cmdFetch.Flag.StringVar(&metadataType, "t", "", "Type of metadata to fetch")
cmdFetch.Flag.StringVar(&metadataType, "type", "", "Type of metadata to fetch")
cmdFetch.Flag.StringVar(&targetDirectory, "d", "", "Use to specify the root directory of your project")
cmdFetch.Flag.StringVar(&targetDirectory, "directory", "", "Use to specify the root directory of your project")
cmdFetch.Flag.BoolVar(&unpack, "u", false, "Unpack any static resources")
cmdFetch.Flag.BoolVar(&unpack, "unpack", false, "Unpack any static resources")
cmdFetch.Flag.BoolVar(&preserveZip, "p", false, "keep zip file on disk")
cmdFetch.Flag.BoolVar(&preserveZip, "preserve", false, "keep zip file on disk")
cmdFetch.Run = runFetch
makefile = true
}
func runFetchAura2(cmd *Command, entityname string) {
force, _ := ActiveForce()
var bundles AuraDefinitionBundleResult
var definitions AuraDefinitionBundleResult
var err error
if entityname == "" {
bundles, definitions, err = force.GetAuraBundles()
if err != nil {
ErrorAndExit(err.Error())
}
} else {
bundles, definitions, err = force.GetAuraBundle(entityname)
if err != nil {
ErrorAndExit(err.Error())
}
}
_, err = persistBundles(bundles, definitions)
return
}
func FetchManifest(entityname string) (manifest BundleManifest) {
force, _ := ActiveForce()
var bundles AuraDefinitionBundleResult
var definitions AuraDefinitionBundleResult
var err error
if entityname == "" {
bundles, definitions, err = force.GetAuraBundles()
if err != nil {
ErrorAndExit(err.Error())
}
} else {
bundles, definitions, err = force.GetAuraBundle(entityname)
if err != nil {
ErrorAndExit(err.Error())
}
}
makefile = false
_, err = persistBundles(bundles, definitions)
return
}
func persistBundles(bundles AuraDefinitionBundleResult, definitions AuraDefinitionBundleResult) (bundleManifest BundleManifest, err error) {
var bundleMap = make(map[string]string)
var bundleRecords = bundles.Records
for _, bundle := range bundleRecords {
id := fmt.Sprintf("%s", bundle["Id"])
bundleMap[id] = fmt.Sprintf("%s", bundle["DeveloperName"])
}
var defRecords = definitions.Records
root, err := GetSourceDir()
if mdbase == "aura" {
root = filepath.Join(targetDirectory, root, "aura")
} else {
root = filepath.Join(targetDirectory, root, mdbase, "aura")
}
if err := os.MkdirAll(root, 0755); err != nil {
ErrorAndExit(err.Error())
}
for key, value := range bundleMap {
if err := os.MkdirAll(filepath.Join(root, value), 0755); err != nil {
ErrorAndExit(err.Error())
}
bundleManifest = BundleManifest{}
bundleManifest.Name = value
bundleManifest.Files = []ComponentFile{}
bundleManifest.Id = key
for _, def := range defRecords {
var did = fmt.Sprintf("%s", def["AuraDefinitionBundleId"])
if did == key {
var naming = strings.Title(strings.ToLower(fmt.Sprintf("%s", def["DefType"])))
var entity = fmt.Sprintf("%s", value) //, strings.Title(strings.ToLower(fmt.Sprintf("%s", def["DefType"]))))
switch fmt.Sprintf("%s", def["DefType"]) {
case "COMPONENT":
entity += ".cmp"
case "APPLICATION":
entity += ".app"
case "EVENT":
entity += ".evt"
case "STYLE":
entity += fmt.Sprintf("%s.css", naming)
case "DOCUMENTATION":
entity += ".auradoc"
case "SVG":
entity += ".svg"
case "DESIGN":
entity += ".design"
default:
entity += fmt.Sprintf("%s.js", naming)
}
var componentFile = ComponentFile{filepath.Join(root, value, entity), fmt.Sprintf("%s", def["Id"])}
bundleManifest.Files = append(bundleManifest.Files, componentFile)
if makefile {
ioutil.WriteFile(filepath.Join(root, value, entity), []byte(fmt.Sprintf("%s", def["Source"])), 0644)
}
}
}
bmBody, _ := json.Marshal(bundleManifest)
ioutil.WriteFile(filepath.Join(root, value, ".manifest"), bmBody, 0644)
}
return
}
func runFetch(cmd *Command, args []string) {
if metadataType == "" {
ErrorAndExit("must specify object type and/or object name")
}
force, _ := ActiveForce()
var files ForceMetadataFiles
var err error
var expandResources bool = unpack
if strings.ToLower(metadataType) == "aura" {
if len(metadataName) > 0 {
for names := range metadataName {
runFetchAura2(cmd, metadataName[names])
}
} else {
runFetchAura2(cmd, "")
}
} else if strings.ToLower(metadataType) == "package" {
if len(metadataName) > 0 {
for names := range metadataName {
files, err = force.Metadata.RetrievePackage(metadataName[names])
if err != nil {
ErrorAndExit(err.Error())
}
if preserveZip == true {
os.Rename("inbound.zip", fmt.Sprintf("%s.zip", metadataName[names]))
}
}
}
} else {
query := ForceMetadataQuery{}
if len(metadataName) > 0 {
mq := ForceMetadataQueryElement{metadataType, metadataName}
query = append(query, mq)
} else {
mq := ForceMetadataQueryElement{metadataType, []string{"*"}}
query = append(query, mq)
}
files, err = force.Metadata.Retrieve(query)
if err != nil {
ErrorAndExit(err.Error())
}
}
var resourcesMap map[string]string
resourcesMap = make(map[string]string)
root, err := GetSourceDir()
existingPackage, _ := pathExists(filepath.Join(root, "package.xml"))
if len(files) == 1 {
ErrorAndExit("Could not find any objects for " + metadataType + ". (Is the metadata type correct?)")
}
for name, data := range files {
if !existingPackage || name != "package.xml" {
file := filepath.Join(root, name)
dir := filepath.Dir(file)
if err := os.MkdirAll(dir, 0755); err != nil {
ErrorAndExit(err.Error())
}
if err := ioutil.WriteFile(filepath.Join(root, name), data, 0644); err != nil {
ErrorAndExit(err.Error())
}
var isResource = false
if strings.ToLower(metadataType) == "staticresource" {
isResource = true
} else if strings.HasSuffix(file, ".resource-meta.xml") {
isResource = true
}
//Handle expanding static resources into a "bundle" folder
if isResource && expandResources {
if string(os.PathSeparator) != "/" {
name = strings.Replace(name, "/", string(os.PathSeparator), -1)
}
pathParts := strings.Split(name, string(os.PathSeparator))
resourceName := pathParts[cap(pathParts)-1]
resourceExt := strings.Split(resourceName, ".")[1]
resourceName = strings.Split(resourceName, ".")[0]
if resourceExt == "resource-meta" {
//Check the xml to determine the mime type of the resource
// We are looking for application/zip
var meta struct {
CacheControl string `xml:"cacheControl"`
ContentType string `xml:"contentType"`
}
if err = xml.Unmarshal([]byte(data), &meta); err != nil {
//return
}
if meta.ContentType == "application/zip" {
// this is the meat for a zip file, so add the map
resourcesMap[resourceName] = filepath.Join(filepath.Dir(file), resourceName+".resource")
}
}
}
}
}
// Now we need to see if we have any zips to expand
if expandResources && len(resourcesMap) > 0 {
for _, value := range resourcesMap {
//resourcefile := filepath.Join(root, "staticresources", value)
resourcefile := value
dest := strings.Split(value, ".")[0]
if err := os.MkdirAll(dest, 0755); err != nil {
ErrorAndExit(err.Error())
}
r, err := zip.OpenReader(resourcefile)
if err != nil {
log.Fatal(err)
}
defer r.Close()
for _, f := range r.File {
rc, err := f.Open()
if err != nil {
fmt.Println(err)
}
defer rc.Close()
path := dest
if !strings.HasPrefix(f.Name, "__") {
if f.FileInfo().IsDir() {
path = filepath.Join(dest, f.Name)
os.MkdirAll(path, f.Mode())
} else {
os.MkdirAll(filepath.Join(dest, filepath.Dir(f.Name)), 0777)
zf, err := os.OpenFile(
filepath.Join(dest, f.Name), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
fmt.Println("OpenFile: ", err)
}
_, err = io.Copy(zf, rc)
if err != nil {
fmt.Println(err)
zf.Close()
}
zf.Close()
}
}
}
}
}
fmt.Printf("Exported to %s\n", root)
}
func pathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}