/
config.go
126 lines (103 loc) · 2.69 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package client
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/user"
"path"
"path/filepath"
)
// Config holds our client config
var Config map[string]string
// LoadConfig reads or creates our config file at ~/.sendto/config
func LoadConfig() error {
// Create our files folder to store files to send
err := createFolder("files")
if err != nil {
return fmt.Errorf("config: create folder error %s", err)
}
// Create our users folder to store public keys downloaded
err = createFolder("users")
if err != nil {
return fmt.Errorf("config: create folder error %s", err)
}
// Load our config file (or create a new one with one entry - sender identity)
// First check it exists
file, err := ioutil.ReadFile(configFilePath())
// We tolerate failure here as file may not exist
if err == nil {
err = json.Unmarshal(file, &Config)
if err != nil {
return fmt.Errorf("config: json error %s", err)
}
}
// If no config exists create a config and save it
if len(Config) == 0 {
err = setupConfig()
if err != nil {
return fmt.Errorf("config: setup error %s", err)
}
err = SaveConfig()
if err != nil {
return fmt.Errorf("config: save error %s", err)
}
}
return nil
}
// SaveConfig saves our config out to a file at ~/.sendto/config
func SaveConfig() error {
// Write out a json file representing our config map
configJSON, err := json.MarshalIndent(Config, "", "\t")
if err != nil {
return err
}
// Write the config json file
err = ioutil.WriteFile(configFilePath(), configJSON, os.ModePerm)
if err != nil {
return err
}
return nil
}
func setupConfig() error {
Config = make(map[string]string, 0)
// Use the home dir as a default sender name
name := path.Base(homePath())
u, err := user.Current()
// Recover gracefully from lack of user.Current() on other platforms
// only set if this is supported
if err == nil && u.Name != "" {
name = u.Name
}
// Set up a default config, pointing to sendto.click
Config["sender"] = name
Config["keyserver"] = "https://sendto.click/users/%s/key"
Config["server"] = "https://sendto.click"
fmt.Printf("Setting default config:%v\n", Config)
return nil
}
func configPath() string {
return filepath.Join(homePath(), ".sendto")
}
func homePath() string {
home := os.Getenv("HOME")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}
func configFilePath() string {
return filepath.Join(configPath(), "config.json")
}
func createFolder(name string) error {
p := filepath.Join(configPath(), name)
return os.MkdirAll(p, os.ModeDir|os.ModePerm)
}
// fileExists returns true if this file exists
func fileExists(path string) bool {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return false
}
return true
}