forked from ForceCLI/force
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
66 lines (55 loc) · 1.35 KB
/
query.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
package main
import (
"fmt"
"golang.org/x/crypto/ssh/terminal"
"os"
"strings"
)
var cmdQuery = &Command{
Run: runQuery,
Usage: "query <soql statement> [output format]",
Short: "Execute a SOQL statement",
Long: `
Execute a SOQL statement
Examples:
force query "select Id, Name, Account.Name From Contact"
force query "select Id, Name, Account.Name From Contact" --format:csv
force query "select Id, Name From Account Where MailingState IN ('CA', 'NY')"
`,
}
func runQuery(cmd *Command, args []string) {
force, _ := ActiveForce()
if len(args) < 1 {
cmd.printUsage()
} else {
format := "console"
if !terminal.IsTerminal(int(os.Stdout.Fd())) {
format = "csv"
}
var formatArg = ""
var isTooling = false
var formatIndex = 1
if len(args) == 2 {
formatArg = args[len(args)-formatIndex]
} else if len(args) == 3 {
formatIndex = 2
formatArg = args[len(args)-formatIndex]
isTooling = true
}
if strings.Contains(formatArg, "format:") {
args = args[:len(args)-formatIndex]
format = strings.SplitN(formatArg, ":", 2)[1]
}
soql := strings.Join(args, " ")
records, _, err := force.Query(fmt.Sprintf("%s", soql), isTooling)
if err != nil {
ErrorAndExit(err.Error())
} else {
if format == "console" {
DisplayForceRecords(records)
} else {
DisplayForceRecordsf(records.Records, format)
}
}
}
}