forked from marouni/adr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adr.go
123 lines (105 loc) · 2.64 KB
/
adr.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
package main
import (
"bufio"
"github.com/fatih/color"
"html/template"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
)
// Adr basic structure
type Adr struct {
Number int
Title string
Date string
Status AdrStatus
Filename string
}
// AdrStatus type
type AdrStatus string
// ADR status enums
const (
PROPOSED AdrStatus = "Proposed"
ACCEPTED AdrStatus = "Accepted"
DEPRECATED AdrStatus = "Deprecated"
SUPERSEDED AdrStatus = "Superseded"
)
const (
layoutISO = "2006-01-02"
)
func newAdr(projectPath string, config AdrProjectConfig, adrName []string) (adrFullPath string) {
adr := Adr{
Title: strings.Join(adrName, " "),
Date: time.Now().Format(layoutISO),
Number: config.CurrentAdr,
Status: PROPOSED,
}
useTemplate, err := template.ParseFiles(projectTemplatePath(projectPath))
if err != nil {
panic(err)
}
adrFileName := strconv.Itoa(adr.Number) + "-" + strings.Join(strings.Split(strings.Trim(strings.ToLower(adr.Title), "\n \t"), " "), "-") + ".md"
adrFullPath = filepath.Join(projectPath, config.BaseDir, adrFileName)
f, err := os.Create(adrFullPath)
if err != nil {
panic(err)
}
err = useTemplate.Execute(f, adr)
if err != nil {
color.Red("Problems writing ADR on " + adrFullPath)
panic(err)
}
_ = f.Close()
color.Green("ADR number " + strconv.Itoa(adr.Number) + " was successfully written to : " + adrFullPath)
return
}
func visitAdrFile(adrs *[]Adr) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
var adr Adr
firstLine := ""
status := ""
if err != nil {
log.Fatal(err)
}
if info.IsDir() {
return nil
}
file, err := os.OpenFile(path, os.O_RDONLY, os.ModePerm)
defer file.Close()
sc := bufio.NewScanner(file)
for sc.Scan() {
line := sc.Text()
matchTitle, _ := regexp.MatchString("# \\d+\\.", line)
if firstLine == "" && matchTitle {
firstLine = line
adr.Title = strings.Replace(firstLine, "# ", "", 1)
}
matchStatus, _ := regexp.MatchString(string("^"+PROPOSED+"|^"+ACCEPTED+"|^"+DEPRECATED+"|^"+SUPERSEDED), line)
if status == "" && matchStatus {
status = line
adr.Status = AdrStatus(status)
}
if strings.HasPrefix(line, "Date: ") {
date := strings.Replace(line, "Date: ", "", 1)
adr.Date = date
}
}
adr.Filename = path
*adrs = append(*adrs, adr)
return nil
}
}
func getAdrs(projectPath string, currentConfig AdrProjectConfig) []Adr {
var adrs []Adr
adrDir := filepath.Join(projectPath, currentConfig.BaseDir)
err := filepath.Walk(adrDir, visitAdrFile(&adrs))
if err != nil {
color.Red("Could not open directory of ADRs: " + adrDir)
panic(err)
}
return adrs
}