This repository has been archived by the owner on Jan 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.go
66 lines (62 loc) · 1.52 KB
/
check.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
package ydisk
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"strings"
"github.com/slytomcat/llog"
)
func notExists(path string) bool {
_, err := os.Stat(path)
if err != nil {
return os.IsNotExist(err)
}
return false
}
// checkDaemon checks that yandex-disk daemon is installed.
// It reads the provided daemon configuration file and checks existence of synchronized folder
// and authorization file ('passwd' file). If one of them is not exists then checkDaemon exits
// from program.
// It returns the user catalogue that is synchronized by daemon in case of success check.
func checkDaemon(conf string) (string, string, error) {
exe, err := exec.LookPath("yandex-disk")
if err != nil {
msg := "Yandex.Disk CLI utility is not installed. Install it first"
llog.Error(msg)
return "", "", fmt.Errorf(msg)
}
f, err := os.Open(conf)
if err != nil {
llog.Error("Daemon configuration file opening error:", err)
return "", "", err
}
defer f.Close()
reader := bufio.NewReader(f)
var line, dir, auth string
for {
line, err = reader.ReadString('\n')
if err != nil {
break
}
if strings.HasPrefix(line, "dir") {
dir = line[5 : len(line)-2]
}
if strings.HasPrefix(line, "auth") {
auth = line[6 : len(line)-2]
}
if dir != "" && auth != "" {
break
}
}
if err != nil && err != io.EOF {
return "", "", err
}
if notExists(dir) || notExists(auth) {
msg := "Daemon is not configured. First run: `yandex-disk setup`"
llog.Error(msg)
return "", "", fmt.Errorf("%s", msg)
}
return exe, dir, nil
}