-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
65 lines (53 loc) · 1.52 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
package main
import (
"errors"
"os"
"path/filepath"
"runtime"
"github.com/BurntSushi/toml"
homedir "github.com/mitchellh/go-homedir"
)
// https://benaiah.me/posts/configuring-go-apps-with-toml
var configDirName = "voip-ms-cli"
// GetDefaultConfigDir returns a string containing the path to the config dir.
// On most platforms return the expanded ~/.config, but on linux maybe respect
// XDG_CONFIG_HOME env var.
func getDefaultConfigDir() (string, error) {
var configDirLocation string
homeDir, err := homedir.Dir()
if err != nil {
return "", err
}
xdgconfig := os.Getenv("XDG_CONFIG_HOME")
if xdgconfig != "" && runtime.GOOS == "linux" {
configDirLocation = xdgconfig
} else {
configDirLocation = filepath.Join(homeDir, ".config", configDirName)
}
return configDirLocation, nil
}
// Config specifies our needed config for talking to the voip.ms API
type Config struct {
Credentials credentials
}
// Credentials section of the config
type credentials struct {
Email string
Password string
}
// LoadConfig returns a pointer to a Config
func loadConfig(filename string) (*Config, error) {
if _, err := os.Stat(filename); os.IsNotExist(err) {
return nil, errors.New("Config file " + filename + " does not exist!")
} else if err != nil {
return nil, err
}
var c Config
if _, err := toml.DecodeFile(filename, &c); err != nil {
return nil, err
}
if c.Credentials.Email == "" || c.Credentials.Password == "" {
return nil, errors.New("config is missing credentials.email or password")
}
return &c, nil
}