-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.go
106 lines (90 loc) · 2.67 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
package config
import (
"io/ioutil"
"os"
"os/user"
"reflect"
"strings"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
// FILENAME define the config file name that needs to be present on the computer for the app to works
const FILENAME = ".asteroid.yaml"
// Config regroup the Wireguard and Client config
type Config struct {
WG Wireguard `yaml:"wireguard"`
ClientConfig ClientConfig `yaml:"client_config_file"`
}
// Wireguard regroup all the field needed for WG to works properly
type Wireguard struct {
SSHKeyName string `yaml:"ssh_key_name"`
WireguardIP string `yaml:"ip"`
SSHPort string `yaml:"ssh_port"`
Username string `yaml:"username"`
HostKey bool `yaml:"verification_host_key"`
WGPort string `yaml:"wg_port"`
}
// ClientConfig regroup the few fields necessarily to generate WG client config
type ClientConfig struct {
Name string `yaml:"name"`
DNS string `yaml:"dns"`
AllowedIPs string `yaml:"allowed_ips"`
}
func isStructNil(config Config) ([]string, bool) {
e := reflect.ValueOf(&config).Elem()
num := e.NumField()
var listMissing []string
var isNil bool
for i := 0; i < num; i++ {
fieldTagTmp := string(e.Type().Field(i).Tag)
fieldTag := strings.Split(fieldTagTmp, "\"")
fieldValue := e.Field(i).Interface()
if fieldValue == "" {
listMissing = append(listMissing, fieldTag[1])
isNil = true
}
}
if isNil {
return listMissing, true
}
return listMissing, false
}
// ReadConfigFile retrieve the asteroid config file and put all fields into Config object
func ReadConfigFile() (Config, error) {
configWG := Config{}
usr, err := user.Current()
if err != nil {
return configWG, errors.Wrap(err, "failed to retrieve the current user")
}
path := usr.HomeDir + "/" + FILENAME
// check if file exists
_, err = os.Stat(path)
// create file if not exists
if os.IsNotExist(err) {
return configWG, errors.Wrap(err, "couldn't create file")
}
if err != nil {
return configWG, errors.Wrap(err, "failed to get file info")
}
// reading file
data, err := ioutil.ReadFile(path)
if err != nil {
return configWG, errors.Wrap(err, "failed to read the file")
}
err = yaml.Unmarshal(data, &configWG)
if err != nil {
return configWG, errors.Wrap(err, "failed to unmarshall the data from the file")
}
listMissing, isNil := isStructNil(configWG)
if isNil {
switch len(listMissing) {
case 0:
return configWG, errors.New("There is an issue with your config file")
case 1:
return configWG, errors.Errorf("The field %s in your config file is missing", listMissing[0])
default:
return configWG, errors.Errorf("The fields %v in your config file are missing\n", listMissing)
}
}
return configWG, nil
}