forked from nkanaev/yarr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
47 lines (40 loc) · 959 Bytes
/
utils.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
package utils
import (
"bufio"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
)
func ParseAuthfile(authfile io.Reader) (username, password string, err error) {
scanner := bufio.NewScanner(authfile)
for scanner.Scan() {
line := scanner.Text()
parts := strings.SplitN(line, ":", 2)
if len(parts) != 2 {
return "", "", fmt.Errorf("wrong syntax (expected `username:password`)")
}
username = parts[0]
password = parts[1]
break
}
return username, password, nil
}
func GetPocketAuth() (key, token string, err error) {
var pocketfile string
configPath, err := os.UserConfigDir()
storagePath := filepath.Join(configPath, "yarr")
pocketfile = filepath.Join(storagePath, "pocket.txt")
f, err := os.Open(pocketfile)
if err != nil {
log.Fatal("Failed to open auth file: ", err)
}
defer f.Close()
key, token, err = ParseAuthfile(f)
if err != nil {
log.Fatal("Failed to parse auth file: ", err)
}
return key, token, err
}