forked from ForceCLI/force
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field.go
113 lines (98 loc) · 2.55 KB
/
field.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
package main
import (
"fmt"
"strings"
)
var cmdField = &Command{
Run: runField,
Usage: "field",
Short: "Manage sobject fields",
Long: `
Manage sobject fields
Usage:
force field list <object>
force field create <object> <field>:<type> [<option>:<value>]
force field delete <object> <field>
force field type
force field type <fieldtype>
Examples:
force field list Todo__c
force field create Inspection__c "Final Outcome":picklist picklist:"Pass, Fail, Redo"
force field create Todo__c Due:DateTime required:true
force field delete Todo__c Due
force field type #displays all the supported field types
force field type email #displays the required and optional attributes
`,
}
func runField(cmd *Command, args []string) {
if len(args) == 0 {
cmd.printUsage()
} else {
switch args[0] {
case "list":
runFieldList(args[1:])
case "create", "add":
runFieldCreate(args[1:])
case "delete", "remove":
runFieldDelete(args[1:])
case "type":
if len(args) == 1 {
DisplayFieldTypes()
} else if len(args) == 2 {
DisplayFieldDetails(args[1])
}
default:
ErrorAndExit("no such command: %s", args[0])
}
}
}
func runFieldList(args []string) {
if len(args) != 1 {
ErrorAndExit("must specify object")
}
force, _ := ActiveForce()
sobject, err := force.GetSobject(args[0])
if err != nil {
ErrorAndExit(err.Error())
}
DisplayForceSobject(sobject)
}
func runFieldCreate(args []string) {
if len(args) < 2 {
ErrorAndExit("must specify object and at least one field")
}
force, _ := ActiveForce()
parts := strings.Split(args[1], ":")
if len(parts) != 2 {
ErrorAndExit("must specify name:type for fields")
}
var optionMap = make(map[string]string)
if len(args) > 2 {
for _, value := range args[2:] {
options := strings.Split(value, ":")
if len(options) != 2 {
ErrorAndExit(fmt.Sprintf("Missing value for field attribute %s", value))
}
optionMap[options[0]] = options[1]
}
}
// Validate the options for this field type
newOptions, err := force.Metadata.ValidateFieldOptions(parts[1], optionMap)
if err != nil {
ErrorAndExit(err.Error())
}
if err := force.Metadata.CreateCustomField(args[0], parts[0], parts[1], newOptions); err != nil {
ErrorAndExit(err.Error())
}
fmt.Println("Custom field created")
}
func runFieldDelete(args []string) {
if len(args) < 2 {
ErrorAndExit("must specify object and at least one field")
}
force, _ := ActiveForce()
if err := force.Metadata.DeleteCustomField(args[0], args[1]); err != nil {
ErrorAndExit(err.Error())
}
fmt.Println("Custom field deleted")
}