-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
75 lines (60 loc) · 1.37 KB
/
config.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
75
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"runtime"
"github.com/howeyc/gopass"
)
// Config struct
type Config struct {
ServerAddress string `json:"ServerAddress"`
Username string `json:"Username"`
Password string `json:"Password"`
UploadDir string `json:"UploadDir"`
}
// ReadConfig returns an error if file does not exist
func ReadConfig(file string) error {
configJSON, err := ioutil.ReadFile(file)
if err != nil {
return err
}
err = json.Unmarshal(configJSON, &config)
if err != nil {
return err
}
return nil
}
// WriteConfig writes a config file
func WriteConfig(file string) error {
config = Config{}
fmt.Printf("Webdav server: ")
fmt.Scanln(&config.ServerAddress)
fmt.Printf("Username: ")
fmt.Scanln(&config.Username)
fmt.Printf("Password (not displayed): ")
pwd, err := gopass.GetPasswd()
if err != nil {
return err
}
config.Password = string(pwd)
fmt.Printf("Default upload directory: ")
fmt.Scanln(&config.UploadDir)
configJSON, err := json.MarshalIndent(config, "", "\t")
if err != nil {
return err
}
return ioutil.WriteFile(file, configJSON, 0600)
}
// Home returns the user's home directory
func Home() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}
return os.Getenv("HOME")
}