/
credentials.go
74 lines (60 loc) · 1.35 KB
/
credentials.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
package tesla
import (
"bufio"
"fmt"
"github.com/Unknwon/goconfig"
"github.com/bgentry/speakeasy"
"log"
"os"
)
type Credentials struct {
Username string
Password string
ClientId string
ClientSecret string
}
func GetCredentials(path string) *Credentials {
cred := new(Credentials)
cred.FetchFromEnv(path)
cred.FetchUserName()
cred.FetchPassWord()
return cred
}
func (cred *Credentials) FetchFromEnv(path string) {
// Load config
cfg, err := goconfig.LoadConfigFile(path)
// Parse
clientId, err := cfg.GetValue(goconfig.DEFAULT_SECTION, "id")
clientSecret, err := cfg.GetValue(goconfig.DEFAULT_SECTION, "secret")
// Check for errors
if err != nil {
log.Fatal(err)
os.Exit(1)
}
// Check for empty values
if len(clientId) == 0 || len(clientSecret) == 0 {
log.Fatal("[tesla]: No config found .. ")
os.Exit(1)
}
// Set on Credentials
cred.ClientId = clientId
cred.ClientSecret = clientSecret
// trace
Ok("[OK] Oauth credentials found")
}
func (cred *Credentials) FetchPassWord() {
// Grab password
password, err := speakeasy.Ask("Enter password: ")
if err != nil {
log.Fatal(err)
os.Exit(1)
}
cred.Password = password
}
func (cred *Credentials) FetchUserName() {
//Grab username
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter username: ")
username, _ := reader.ReadString('\n')
cred.Username = username
}