Skip to content

Commit

Permalink
Showing 4 changed files with 165 additions and 1 deletion.
66 changes: 65 additions & 1 deletion fetch.go
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ package main

import (
"archive/zip"
"encoding/json"
"encoding/xml"
"fmt"
"io"
@@ -25,6 +26,8 @@ Examples
force fetch CustomObject
force fetch Aura
force fetch package MyPackagedApp
options
@@ -36,6 +39,65 @@ Examples
`,
}

func runFetchAura(cmd *Command, args []string) {
force, _ := ActiveForce()

bundles, definitions, err := force.GetAuraBundlesList()
if err != nil {
ErrorAndExit(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
wd, _ := os.Getwd()
root := filepath.Join(wd, "metadata", "aurabundles")

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())
}

var bundleManifest = BundleManifest{}
bundleManifest.Name = value
bundleManifest.Files = []ComponentFile{}

for _, def := range defRecords {
var did = fmt.Sprintf("%s", def["AuraDefinitionBundleId"])
if did == key {
var entity = fmt.Sprintf("%s%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 += ".css"
default:
entity += ".js"
}
var componentFile = ComponentFile{entity, fmt.Sprintf("%s", def["Id"])}
bundleManifest.Files = append(bundleManifest.Files, componentFile)
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.json"), bmBody, 0644)
}
return
}

func runFetch(cmd *Command, args []string) {
wd, _ := os.Getwd()
root := filepath.Join(wd, "metadata")
@@ -49,7 +111,9 @@ func runFetch(cmd *Command, args []string) {
var expandResources bool = false

artifactType := args[0]
if artifactType == "package" {
if strings.ToLower(artifactType) == "aura" {
runFetchAura(cmd, args)
} else if artifactType == "package" {
files, err = force.Metadata.RetrievePackage(args[1])
if err != nil {
ErrorAndExit(err.Error())
46 changes: 46 additions & 0 deletions force.go
Original file line number Diff line number Diff line change
@@ -260,6 +260,25 @@ type JobInfo struct {
ApexProcessingTime int `xml:"apexProcessingTime"`
}

type AuraDefinitionBundleResult struct {
Done bool
Records []ForceRecord
TotalSize int
QueryLocator string
Size int
EntityTypeName string
}

type ComponentFile struct {
FileName string
ComponentId string
}

type BundleManifest struct {
Name string
Files []ComponentFile
}

func NewForce(creds ForceCredentials) (force *Force) {
force = new(Force)
force.Credentials = creds
@@ -356,6 +375,27 @@ func (f *Force) GetCodeCoverage(classId string, className string) (err error) {
return
}

func (f *Force) GetAuraBundlesList() (bundles AuraDefinitionBundleResult, definitions AuraDefinitionBundleResult, err error) {
aurl := fmt.Sprintf("%s/services/data/%s/tooling/query?q=%s", f.Credentials.InstanceUrl, apiVersion,
url.QueryEscape("SELECT Id, DeveloperName, NamespacePrefix, ApiVersion, Description FROM AuraDefinitionBundle"))
body, err := f.httpGet(aurl)
if err != nil {
return
}
json.Unmarshal(body, &bundles)

aurl = fmt.Sprintf("%s/services/data/%s/tooling/query?q=%s", f.Credentials.InstanceUrl, apiVersion,
url.QueryEscape("SELECT Id, Source, AuraDefinitionBundleId, DefType, Format FROM AuraDefinition"))

body, err = f.httpGet(aurl)
if err != nil {
return
}
json.Unmarshal(body, &definitions)

return
}

func (f *Force) ListSobjects() (sobjects []ForceSobject, err error) {
url := fmt.Sprintf("%s/services/data/%s/sobjects", f.Credentials.InstanceUrl, apiVersion)
body, err := f.httpGet(url)
@@ -575,6 +615,12 @@ func (f *Force) RetrieveBulkBatchResults(jobId string, batchId string) (results
return
}

func (f *Force) UpdateAuraComponent(source map[string]string, id string) (err error) {
url := fmt.Sprintf("%s/services/data/%s/tooling/sobjects/AuraDefinition/%s", f.Credentials.InstanceUrl, apiVersion, id)
_, err = f.httpPatch(url, source)
return
}

func (f *Force) UpdateRecord(sobject string, id string, attrs map[string]string) (err error) {
url := fmt.Sprintf("%s/services/data/%s/sobjects/%s/%s", f.Credentials.InstanceUrl, apiVersion, sobject, id)
_, err = f.httpPatch(url, attrs)
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ var commands = []*Command{
cmdUpdate,
cmdHelp,
cmdPush,
cmdPushAura,
cmdPassword,
}

53 changes: 53 additions & 0 deletions pushAura.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
)

var cmdPushAura = &Command{
Run: runPushAura,
Usage: "pushAura",
Short: "TBD",
Long: `
force pushAura fullFilePath entityId
`,
}

func runPushAura(cmd *Command, args []string) {
force, _ := ActiveForce()

fileName := args[0]

//Get the manifest
mbody, _ := readFile(filepath.Join(filepath.Dir(fileName), "manifest.json"))

var manifest BundleManifest
json.Unmarshal([]byte(mbody), &manifest)

for i := range manifest.Files {
component := manifest.Files[i]
if component.FileName == filepath.Base(fileName) {
//Here is where we make the call to send the update
mbody, _ = readFile(fileName)
err := force.UpdateAuraComponent(map[string]string{"source": mbody}, component.ComponentId)
if err != nil {
ErrorAndExit(err.Error())
}
fmt.Printf("Aura definition updated: %s\n", filepath.Base(fileName))
break
}
}
}

func readFile(filename string) (body string, err error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return
}
body = string(data)
return
}

0 comments on commit 274b4ed

Please sign in to comment.