-
Notifications
You must be signed in to change notification settings - Fork 24
/
helpers.go
137 lines (116 loc) · 3 KB
/
helpers.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"encoding/json"
"html/template"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/fatih/color"
)
// AdrConfig ADR configuration, loaded and used by each sub-command
type AdrConfig struct {
BaseDir string `json:"base_directory"`
CurrentAdr int `json:"current_id"`
}
// Adr basic structure
type Adr struct {
Number int
Title string
Date string
Status AdrStatus
}
// AdrStatus type
type AdrStatus string
// ADR status enums
const (
PROPOSED AdrStatus = "Proposed"
ACCEPTED AdrStatus = "Accepted"
DEPRECATED AdrStatus = "Deprecated"
SUPERSEDED AdrStatus = "Superseded"
)
var usr, err = user.Current()
var adrConfigFolderName = ".adr"
var adrConfigFileName = "config.json"
var adrConfigTemplateName = "template.md"
var adrConfigFolderPath = filepath.Join(usr.HomeDir, adrConfigFolderName)
var adrConfigFilePath = filepath.Join(adrConfigFolderPath, adrConfigFileName)
var adrTemplateFilePath = filepath.Join(adrConfigFolderPath, adrConfigTemplateName)
var adrDefaultBaseFolder = filepath.Join(usr.HomeDir, "adr")
func initBaseDir(baseDir string) {
if _, err := os.Stat(baseDir); os.IsNotExist(err) {
os.Mkdir(baseDir, 0744)
} else {
color.Red(baseDir + " already exists, skipping folder creation")
}
}
func initConfig(baseDir string) {
if _, err := os.Stat(adrConfigFolderPath); os.IsNotExist(err) {
os.Mkdir(adrConfigFolderPath, 0744)
}
config := AdrConfig{baseDir, 0}
bytes, err := json.MarshalIndent(config, "", " ")
if err != nil {
panic(err)
}
ioutil.WriteFile(adrConfigFilePath, bytes, 0644)
}
func initTemplate() {
body := []byte(`
# {{.Number}}. {{.Title}}
======
Date: {{.Date}}
## Status
======
{{.Status}}
## Context
======
## Decision
======
## Consequences
======
`)
ioutil.WriteFile(adrTemplateFilePath, body, 0644)
}
func updateConfig(config AdrConfig) {
bytes, err := json.MarshalIndent(config, "", " ")
if err != nil {
panic(err)
}
ioutil.WriteFile(adrConfigFilePath, bytes, 0644)
}
func getConfig() AdrConfig {
var currentConfig AdrConfig
bytes, err := ioutil.ReadFile(adrConfigFilePath)
if err != nil {
color.Red("No ADR configuration is found!")
color.HiGreen("Start by initializing ADR configuration, check 'adr init --help' for more help")
os.Exit(1)
}
json.Unmarshal(bytes, ¤tConfig)
return currentConfig
}
func newAdr(config AdrConfig, adrName []string) {
adr := Adr{
Title: strings.Join(adrName, " "),
Date: time.Now().Format("02-01-2006 15:04:05"),
Number: config.CurrentAdr,
Status: PROPOSED,
}
template, err := template.ParseFiles(adrTemplateFilePath)
if err != nil {
panic(err)
}
adrFileName := strconv.Itoa(adr.Number) + "-" + strings.Join(strings.Split(strings.Trim(adr.Title, "\n \t"), " "), "-") + ".md"
adrFullPath := filepath.Join(config.BaseDir, adrFileName)
f, err := os.Create(adrFullPath)
if err != nil {
panic(err)
}
template.Execute(f, adr)
f.Close()
color.Green("ADR number " + strconv.Itoa(adr.Number) + " was successfully written to : " + adrFullPath)
}