forked from ForceCLI/force
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logins.go
112 lines (98 loc) · 2.34 KB
/
logins.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
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"text/tabwriter"
)
var cmdLogins = &Command{
Run: runLogins,
Usage: "logins",
Short: "List force.com logins used",
Long: `
List force.com accounts
Examples:
force logins
`,
}
func runLogins(cmd *Command, args []string) {
active, _ := ActiveLogin()
accounts, _ := Config.List("accounts")
if len(accounts) == 0 {
fmt.Println("no logins")
} else {
w := new(tabwriter.Writer)
w.Init(os.Stdout, 1, 0, 1, ' ', 0)
for _, account := range accounts {
if !strings.HasPrefix(account, ".") {
var creds ForceCredentials
data, err := Config.Load("accounts", account)
json.Unmarshal([]byte(data), &creds)
if err != nil {
return
}
var banner = fmt.Sprintf("\t%s", creds.InstanceUrl)
if account == active {
account = fmt.Sprintf("\x1b[31;1m%s (active)\x1b[0m", account)
} else {
account = fmt.Sprintf("%s \x1b[31;1m\x1b[0m", account)
}
fmt.Fprintln(w, fmt.Sprintf("%s%s", account, banner))
}
}
fmt.Fprintln(w)
w.Flush()
}
}
func ActiveLogin() (account string, err error) {
account, err = Config.Load("current", "account")
if err != nil {
accounts, _ := Config.List("accounts")
if len(accounts) > 0 {
SetActiveLoginDefault()
}
}
return
}
func ActiveCredentials(requireCredentials bool) (creds ForceCredentials, err error) {
account, err := ActiveLogin()
if requireCredentials && (err != nil || strings.TrimSpace(account) == "") {
ErrorAndExit("Please login before running this command.")
}
data, err := Config.Load("accounts", strings.TrimSpace(account))
if requireCredentials && err != nil {
ErrorAndExit("Failed to load credentials. %v", err)
}
if err == nil {
_ = json.Unmarshal([]byte(data), &creds)
if creds.ApiVersion != "" {
apiVersionNumber = creds.ApiVersion
apiVersion = "v" + apiVersionNumber
}
}
if !requireCredentials && err != nil {
err = nil
}
return
}
func ActiveForce() (force *Force, err error) {
creds, err := ActiveCredentials(true)
if err != nil {
return
}
force = NewForce(&creds)
return
}
func SetActiveLoginDefault() (account string) {
accounts, _ := Config.List("accounts")
if len(accounts) > 0 {
account = accounts[0]
SetActiveLogin(account)
}
return
}
func SetActiveLogin(account string) (err error) {
err = Config.Save("current", "account", account)
return
}