/
config.go
98 lines (81 loc) · 2.49 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
/*
* FEIERABEND - A mite integration for software developers
* Copyright (c) 2018 Philipp Mieden <dreadl0ck [at] protonmail [dot] ch>
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package main
import (
"io/ioutil"
"os"
"runtime"
yaml "gopkg.in/yaml.v2"
)
var (
pathSeparator = "/"
pathUserConfig = os.Getenv("HOME") + pathSeparator + configFileName
)
const configFileName = ".feierabend.yml"
// config for a single project
// defines
type projectConfig struct {
CustomerName string `yaml:"customer"`
ProjectName string `yaml:"project"`
}
// config for a user
// contains data used to authenticate to mite
// and optionally a list of global projects to check when executed
type userConfig struct {
Name string `yaml:"name"`
APIKey string `yaml:"apiKey"`
Team string `yaml:"team"`
UserName string `yaml:"userName"`
Projects []string `yaml:"projects"`
}
// parse the user config and return an instance
func parseUserConfig() *userConfig {
// check if we are running on windows at runtime
// if true adjust the config file path accordingly
if runtime.GOOS == "windows" {
pathSeparator = "\\"
pathUserConfig = os.Getenv("HOME") + pathSeparator + configFileName
}
// parse the user config
c, err := ioutil.ReadFile(pathUserConfig)
if err != nil {
exitWith("failed to read user config", err)
}
// unmarshal yaml
var u = new(userConfig)
err = yaml.Unmarshal(c, &u)
if err != nil {
exitWith("failed to unmarshal user config", err)
}
return u
}
// parse the project config and return an instance
func parseProjectConfig(path string) *projectConfig {
// change directory to path
err := os.Chdir(path)
if err != nil {
exitWith("failed to change the current directory to "+path+":", err)
}
// parse the project config file
c, err := ioutil.ReadFile(configFileName)
if err != nil {
exitWith(path+": failed to read project config", err)
}
// unmarshal yaml
var p = new(projectConfig)
err = yaml.Unmarshal(c, &p)
if err != nil {
exitWith(path+": failed to unmarshal project config", err)
}
return p
}