/
config.go
60 lines (52 loc) · 1.11 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
package main
import (
toml "github.com/BurntSushi/toml"
xdg "github.com/adrg/xdg"
"os"
"path"
debug "runtime/debug"
)
type Enviorment struct {
SshCommand string
StaticHosts []string
}
type Config struct {
Enviroments map[string]Enviorment
}
func LoadConfig() (Config, error) {
println("loading config")
var configFileName = "oporto.toml"
var configFolder = path.Join(xdg.Home, ".config")
var configPath = path.Join(configFolder, configFileName)
var config Config
_, err := toml.DecodeFile(configPath, &config)
if err == nil {
return config, err
}
if os.IsNotExist(err) {
err = os.MkdirAll(configFolder, 600)
if err != nil {
return config, err
}
file, err := os.Create(configPath)
if err != nil {
return config, err
}
file.Close()
println("created config file")
return LoadConfig()
}
return config, err
}
// var conf Config
// _, err := toml.Decode(tomlData, &conf)
var Commit = func() string {
if info, ok := debug.ReadBuildInfo(); ok {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" {
return setting.Value[0:6]
}
}
}
return "unknown"
}()