Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle errors, add go.mod, add .gitignore #9

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
1 change: 0 additions & 1 deletion commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ func setCommands(app *cli.App) {
Flags: []cli.Flag{},
Action: func(c *cli.Context) error {
currentConfig := getConfig()
currentConfig.CurrentAdr++
updateConfig(currentConfig)
newAdr(currentConfig, c.Args())
return nil
Expand Down
17 changes: 17 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module github.com/marouni/adr

go 1.18

require (
github.com/fatih/color v1.13.0
github.com/urfave/cli v1.22.9
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
)
24 changes: 24 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/urfave/cli v1.22.9 h1:cv3/KhXGBGjEXLC4bH0sLuJ9BewaAbpk5oyMOveu4pw=
github.com/urfave/cli v1.22.9/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
101 changes: 66 additions & 35 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package main

import (
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
Expand All @@ -16,15 +18,14 @@ import (

// AdrConfig ADR configuration, loaded and used by each sub-command
type AdrConfig struct {
BaseDir string `json:"base_directory"`
CurrentAdr int `json:"current_id"`
BaseDir string `json:"base_directory"`
}

// Adr basic structure
type Adr struct {
Number int
Title string
Date string
Date time.Time
Status AdrStatus
}

Expand All @@ -50,54 +51,54 @@ var adrDefaultBaseFolder = filepath.Join(usr.HomeDir, "adr")

func initBaseDir(baseDir string) {
if _, err := os.Stat(baseDir); os.IsNotExist(err) {
os.Mkdir(baseDir, 0744)
err = os.Mkdir(baseDir, 0744)
if err != nil {
panic(err)
}
} 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)
err = os.Mkdir(adrConfigFolderPath, 0744)
if err != nil {
panic(err)
}
}
config := AdrConfig{baseDir, 0}
config := AdrConfig{BaseDir: baseDir}
bytes, err := json.MarshalIndent(config, "", " ")
if err != nil {
panic(err)
}
ioutil.WriteFile(adrConfigFilePath, bytes, 0644)
err = ioutil.WriteFile(adrConfigFilePath, bytes, 0644)
if err != nil {
panic(err)
}
}

func initTemplate() {
body := []byte(`
# {{.Number}}. {{.Title}}
======
Date: {{.Date}}

## Status
======
{{.Status}}

## Context
======

## Decision
======

## Consequences
======

`)
body, err := fs.ReadFile("tpl/doc.tpl.yaml")
if err != nil {
panic(err)
}

ioutil.WriteFile(adrTemplateFilePath, body, 0644)
err = ioutil.WriteFile(adrTemplateFilePath, body, 0644)
if err != nil {
panic(err)
}
}

func updateConfig(config AdrConfig) {
bytes, err := json.MarshalIndent(config, "", " ")
if err != nil {
panic(err)
}
ioutil.WriteFile(adrConfigFilePath, bytes, 0644)
err = ioutil.WriteFile(adrConfigFilePath, bytes, 0644)
if err != nil {
panic(err)
}
}

func getConfig() AdrConfig {
Expand All @@ -110,28 +111,58 @@ func getConfig() AdrConfig {
os.Exit(1)
}

json.Unmarshal(bytes, &currentConfig)
err = json.Unmarshal(bytes, &currentConfig)
if err != nil {
panic(err)
}
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,
Date: time.Now().UTC(),
Number: findLastNumber(config) + 1,
Status: PROPOSED,
}
template, err := template.ParseFiles(adrTemplateFilePath)
tpl, 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"
adrFileName := fmt.Sprintf("%05d", adr.Number) + "-" + strings.ToLower(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()
err = tpl.Execute(f, adr)
if err != nil {
panic(err)
}
_ = f.Close()
color.Green("ADR number " + strconv.Itoa(adr.Number) + " was successfully written to : " + adrFullPath)
}

// findLastNumber returns the highest number that is found in a directory.
// If the directory is empty, 0 is returned.
func findLastNumber(config AdrConfig) int {
var max int
contents, err := os.ReadDir(config.BaseDir)
if err != nil {
panic(err)
}
regex := regexp.MustCompile(`(\d{5,}).*`)
for _, c := range contents {
m := regex.FindSubmatch([]byte(c.Name()))
if m != nil {
n, err := strconv.Atoi(string(m[1]))
if err != nil {
panic(err)
}
if n >= max {
max = n
}
}
}
return max
}
10 changes: 10 additions & 0 deletions helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import "testing"

func TestFindLastNumber(t *testing.T) {
t.Parallel()
cfg := getConfig()
n := findLastNumber(cfg)
_ = n
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func main() {
app := cli.NewApp()
app.Name = "adr"
app.Usage = "Work with Architecture Decision Records (ADRs)"
app.Version = "0.1.0"
app.Version = "0.1.1"

setFlags(app)
setCommands(app)
Expand Down
7 changes: 7 additions & 0 deletions templates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "embed"

// fs embeds the template into the binary.
//go:embed tpl
var fs embed.FS
11 changes: 11 additions & 0 deletions tpl/doc.tpl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
number: {{ .Number }} # Do not change
title: {{ .Title }}
date: {{ .Date.Format "2006-01-02T15:04:05Z07:00" }} # Do not change
status: {{ .Status}} # Should be one of: Proposed, Accepted, Deprecated, Superseded
---
## Context

## Decision

## Consequences