forked from ForceCLI/force
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigobject.go
191 lines (167 loc) · 4.7 KB
/
bigobject.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
package main
import (
"bitbucket.org/pkg/inflect"
"fmt"
"os"
"strconv"
"strings"
)
var cmdBigObject = &Command{
Usage: "bigobject",
Short: "Manage big objects",
Long: `
Manage big objects
Usage:
force bigobject list
force bigobject create -n=<name> [-f=<field> ...]
A field is defined as a "+" separated list of attributes
Attributes depend on the type of the field.
Type = text: name, label, length
Type = datetime: name, label
Type = lookup: name, label, referenceTo, relationshipName
Examples:
force bigobject list
force bigobject create -n=MyObject -l="My Object" -p="My Objects" \
-f=name:Field1+label:"Field 1"+type:Text+length:120 \
-f=name:MyDate+type=dateTime
`,
}
type boField []string
func (i *boField) String() string {
return fmt.Sprint(*i)
}
func (i *boField) 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 (
fields boField
deploymentStatus string
objectLabel string
pluralLabel string
)
func init() {
cmdBigObject.Flag.Var(&fields, "field", "names of metadata")
cmdBigObject.Flag.Var(&fields, "f", "names of metadata")
cmdBigObject.Flag.StringVar(&deploymentStatus, "deployment", "Deployed", "deployment status")
cmdBigObject.Flag.StringVar(&deploymentStatus, "d", "Deployed", "deployment status")
cmdBigObject.Flag.StringVar(&objectLabel, "label", "", "big object label")
cmdBigObject.Flag.StringVar(&objectLabel, "l", "", "big object label")
cmdBigObject.Flag.StringVar(&pluralLabel, "plural", "", "big object plural label")
cmdBigObject.Flag.StringVar(&pluralLabel, "p", "", "big object plural label")
cmdBigObject.Run = runBigObject
}
func runBigObject(cmd *Command, args []string) {
if len(args) == 0 {
cmd.printUsage()
} else {
if err := cmd.Flag.Parse(args[1:]); err != nil {
os.Exit(2)
}
switch args[0] {
case "list":
getBigObjectList(args[1:])
case "create":
runBigObjectCreate(args[1:])
default:
ErrorAndExit("no such command: %s", args[0])
}
}
}
func parseField(fielddata string) (result BigObjectField) {
attrs := strings.Split(fielddata, "+")
for _, data := range attrs {
pair := strings.Split(data, ":")
switch strings.ToLower(pair[0]) {
case "fullname", "name":
result.FullName = pair[1]
case "label":
result.Label = pair[1]
case "type":
result.Type = pair[1]
case "referenceto":
result.ReferenceTo = pair[1]
case "relationshipname":
result.RelationshipName = pair[1]
case "length":
var lval int64
lval, err := strconv.ParseInt(pair[1], 10, 0)
if err != nil {
ErrorAndExit(err.Error())
}
result.Length = int(lval)
}
}
result = validateField(result)
return
}
func validateField(originField BigObjectField) (field BigObjectField) {
field = originField
if len(field.Type) == 0 {
ErrorAndExit("You need to indicate the type for field %s", field.FullName)
}
if len(field.Label) == 0 {
field.Label = field.FullName
}
switch strings.ToLower(field.Type) {
case "text":
if field.Length == 0 {
ErrorAndExit("The text field %s is missing the length attribute.", field.FullName)
}
field.ReferenceTo = ""
field.RelationshipName = ""
case "lookup":
if len(field.ReferenceTo) == 0 {
ErrorAndExit("The lookup field %s is missing the referenceTo attribute.", field.FullName)
}
if len(field.RelationshipName) == 0 {
ErrorAndExit("The lookup field %s is missing the relationshipName attribute.")
}
case "datetime":
field.ReferenceTo = ""
field.RelationshipName = ""
field.Length = 0
default:
ErrorAndExit("%s is not a valid field type.\nValid field types are 'text', 'dateTime' and 'lookup'", field.Type)
}
return
}
func getBigObjectList(args []string) (l []ForceSobject) {
force, _ := ActiveForce()
sobjects, err := force.ListSobjects()
if err != nil {
ErrorAndExit(fmt.Sprintf("ERROR: %s\n", err))
}
for _, sobject := range sobjects {
if len(args) == 1 {
if strings.Contains(sobject["name"].(string), args[0]) {
l = append(l, sobject)
}
} else {
l = append(l, sobject)
}
}
return
}
func runBigObjectCreate(args []string) {
var fieldObjects = make([]BigObjectField, len(fields))
for i, field := range fields {
fieldObjects[i] = parseField(field)
}
var object = BigObject{deploymentStatus, objectLabel, pluralLabel, fieldObjects}
if len(object.Label) == 0 {
ErrorAndExit("Please provide a label for your big object using the -l flag.")
}
if len(object.PluralLabel) == 0 {
object.PluralLabel = inflect.Pluralize(object.Label)
}
force, _ := ActiveForce()
if err := force.Metadata.CreateBigObject(object); err != nil {
ErrorAndExit(err.Error())
}
fmt.Println("Big object created")
}