forked from claudiodangelis/qrcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
75 lines (69 loc) · 1.31 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"
"io/ioutil"
"log"
"os"
"os/user"
"path/filepath"
)
// Config holds the values
type Config struct {
Iface string `json:"interface"`
Port int `json:"port"`
}
func configFile() (string, error) {
currentUser, err := user.Current()
if err != nil {
return "", err
}
return filepath.Join(currentUser.HomeDir, ".qr-filetransfer.json"), nil
}
// Update the configuration file
func (c *Config) Update() error {
debug("Updating config file")
j, err := json.Marshal(c)
if err != nil {
return err
}
file, err := configFile()
if err != nil {
return err
}
err = ioutil.WriteFile(file, j, 0644)
if err != nil {
return err
}
return nil
}
// Delete the configuration file
func (c *Config) Delete() (bool, error) {
file, err := configFile()
if err != nil {
return false, err
}
if _, err := os.Stat(file); os.IsNotExist(err) {
return false, nil
}
if err := os.Remove(file); err != nil {
return false, err
}
return true, nil
}
// LoadConfig from file
func LoadConfig() Config {
var config Config
file, err := configFile()
if err != nil {
return config
}
debug("Current config file is", file)
b, err := ioutil.ReadFile(file)
if err != nil {
return config
}
if err = json.Unmarshal(b, &config); err != nil {
log.Println("WARN:", err)
}
return config
}