forked from marouni/adr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
homeconfig.go
66 lines (57 loc) · 1.63 KB
/
homeconfig.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
package main
import (
"encoding/json"
"github.com/fatih/color"
"io/ioutil"
"os"
"os/user"
"path/filepath"
)
// AdrConfig Home directory configuration lists directories where ADR Projects has been configured
type AdrConfig struct {
Projects []string `json:"projects"`
}
var usr, _ = user.Current()
var adrHomeConfigFolderName = ".adr"
var adrHomeConfigFolderPath = filepath.Join(usr.HomeDir, adrHomeConfigFolderName)
var adrHomeConfigFilePath = filepath.Join(adrHomeConfigFolderPath, "projects.json")
func initHomeDir() {
if _, err := os.Stat(adrHomeConfigFolderPath); os.IsNotExist(err) {
err := os.Mkdir(adrHomeConfigFolderPath, 0744)
if err != nil {
color.Red("Could not create directory " + adrHomeConfigFolderPath)
}
}
}
func initHomeConfig() (newHomeConfig AdrConfig) {
if _, err := os.Stat(adrHomeConfigFilePath); os.IsNotExist(err) {
color.Green("Creating new home directory configuration")
newHomeConfig = AdrConfig{nil}
writeHomeConfiguration(newHomeConfig)
}
return
}
func getHomeConfig() AdrConfig {
var currentHomeConfig AdrConfig
bytes, err := ioutil.ReadFile(adrHomeConfigFilePath)
if err != nil {
color.Red("No ADR home directory configuration is found!")
color.HiGreen("Start by initializing ADR configuration, check 'adr init --help' for more help")
os.Exit(1)
}
err = json.Unmarshal(bytes, ¤tHomeConfig)
if err != nil {
panic(err)
}
return currentHomeConfig
}
func writeHomeConfiguration(homeConfig AdrConfig) {
bytes, err := json.MarshalIndent(homeConfig, "", " ")
if err != nil {
panic(err)
}
err = ioutil.WriteFile(adrHomeConfigFilePath, bytes, 0644)
if err != nil {
panic(err)
}
}