-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathauth.go
53 lines (44 loc) · 878 Bytes
/
auth.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
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
rpc "github.com/whyrusleeping/zmsg/rpc"
)
func init() {
u, p, err := readAuthCreds()
if err != nil {
fmt.Println("Error reading zcash config: ", err)
}
rpc.DefaultClient.User = u
rpc.DefaultClient.Pass = p
}
func readAuthCreds() (string, string, error) {
homedir := os.Getenv("HOME")
confpath := filepath.Join(homedir, ".zcash/zcash.conf")
fi, err := os.Open(confpath)
if err != nil {
return "", "", err
}
defer fi.Close()
var user string
var pass string
scan := bufio.NewScanner(fi)
for scan.Scan() {
parts := strings.SplitN(scan.Text(), "=", 2)
if len(parts) < 2 {
continue
}
key := strings.TrimSpace(parts[0])
val := strings.TrimSpace(parts[1])
switch key {
case "rpcuser":
user = val
case "rpcpassword":
pass = val
}
}
return user, pass, nil
}