github.com/haagen/force@v0.19.6-0.20140911230915-22addd930b34/logins.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "os" 7 "text/tabwriter" 8 ) 9 10 var cmdLogins = &Command{ 11 Run: runLogins, 12 Usage: "logins", 13 Short: "List force.com logins used", 14 Long: ` 15 List force.com accounts 16 17 Examples: 18 19 force logins 20 `, 21 } 22 23 func runLogins(cmd *Command, args []string) { 24 active, _ := ActiveLogin() 25 accounts, _ := Config.List("accounts") 26 if len(accounts) == 0 { 27 fmt.Println("no logins") 28 } else { 29 w := new(tabwriter.Writer) 30 w.Init(os.Stdout, 1, 0, 1, ' ', 0) 31 32 for _, account := range accounts { 33 var creds ForceCredentials 34 data, err := Config.Load("accounts", account) 35 json.Unmarshal([]byte(data), &creds) 36 37 if err != nil { 38 return 39 } 40 41 var banner = fmt.Sprintf("\t%s", creds.InstanceUrl) 42 if account == active { 43 account = fmt.Sprintf("%s \x1b[31;1m(active)\x1b[0m", account) 44 } else { 45 account = fmt.Sprintf("%s \x1b[31;1m\x1b[0m", account) 46 } 47 fmt.Fprintln(w, fmt.Sprintf("%s%s", account, banner)) 48 } 49 fmt.Fprintln(w) 50 w.Flush() 51 } 52 53 } 54 55 func ActiveLogin() (account string, err error) { 56 account, err = Config.Load("current", "account") 57 if err != nil { 58 accounts, _ := Config.List("accounts") 59 if len(accounts) > 0 { 60 SetActiveLoginDefault() 61 } else { 62 account, err = ForceLoginAndSave(EndpointProduction) 63 SetActiveLogin(account) 64 } 65 } 66 return 67 } 68 69 func ActiveCredentials() (creds ForceCredentials, err error) { 70 account, err := ActiveLogin() 71 if err != nil { 72 return 73 } 74 data, err := Config.Load("accounts", account) 75 json.Unmarshal([]byte(data), &creds) 76 return 77 } 78 79 func ActiveForce() (force *Force, err error) { 80 creds, err := ActiveCredentials() 81 if err != nil { 82 return 83 } 84 force = NewForce(creds) 85 return 86 } 87 88 func SetActiveLoginDefault() (account string) { 89 accounts, _ := Config.List("accounts") 90 if len(accounts) > 0 { 91 account = accounts[0] 92 SetActiveLogin(account) 93 } 94 return 95 } 96 97 func SetActiveLogin(account string) (err error) { 98 err = Config.Save("current", "account", account) 99 return 100 }