-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
55 lines (47 loc) · 1.24 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
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"gopkg.in/yaml.v3"
)
type config struct {
BagPath string `yaml:"bag_path"`
Mountpoint string `yaml:"mountpoint"`
MountTimeout int `yaml:"mount_timeout"`
Debug bool `yaml:"debug"`
PrivateRSAKey string `yaml:"private_rsa_key"`
PublicRSAKey string `yaml:"public_rsa_key"`
}
func NewConfig(path string) (config, error) {
c := defaults()
data, err := ioutil.ReadFile(path)
if err != nil {
return c, fmt.Errorf("could not read file %s: %s", path, err.Error())
}
err = yaml.Unmarshal(data, &c)
if err != nil {
return c, fmt.Errorf("could not read data from config file %s: %s", path, err.Error())
}
c.BagPath = tidyPath(c.BagPath)
c.Mountpoint = tidyPath(c.Mountpoint)
c.PrivateRSAKey = tidyPath(c.PrivateRSAKey)
c.PublicRSAKey = tidyPath(c.PublicRSAKey)
return c, nil
}
func defaults() config {
return config{
BagPath: os.ExpandEnv("$HOME/.scumbag/"),
Mountpoint: os.ExpandEnv("$HOME/.scum/"),
MountTimeout: 120,
Debug: false,
PrivateRSAKey: "$HOME/.ssh/id_rsa",
PublicRSAKey: "$HOME/.ssh/id_rsa.pub",
}
}
func tidyPath(path string) string {
path = strings.ReplaceAll(path, "~", "$HOME")
path = os.ExpandEnv(path)
return path
}