-
Notifications
You must be signed in to change notification settings - Fork 0
/
combine.go
150 lines (125 loc) · 3.57 KB
/
combine.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
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"encoding/json"
"github.com/spf13/cobra"
)
var (
build string
keep bool
note string
flavor string
archive bool
)
// combineCmd represents the combine command
var combineCmd = &cobra.Command{
Use: "combine",
Short: "Combines existing changelog entries into CHANGELOG.md",
Long: `combines all the .json files in the /changelog/unreleased/ folder
and appends to a master CHANGELOG.md
as well as creating a [build]-CHANGELOG.md. Example usage:
combine -b "v0.1" -n "released to app store"`,
Run: func(cmd *cobra.Command, args []string) {
if _, err := os.Stat(changelogEntriesPath); os.IsNotExist(err) {
fmt.Println(err)
return
}
if _, err := os.Stat(changelogArchivePath); os.IsNotExist(err) {
pathErr := os.MkdirAll(changelogArchivePath, 0777)
if pathErr != nil {
fmt.Println(err)
return
}
}
if len(build) == 0 {
fmt.Println("Build is required see --help for usage")
return
}
if archive {
if _, err := os.Stat(changelogArchivePath + sanitizeDescription(build) + "/"); os.IsNotExist(err) {
pathErr := os.MkdirAll(changelogArchivePath+sanitizeDescription(build)+"/", 0777)
if pathErr != nil {
fmt.Println(err)
return
}
}
}
//Parse files
var changelogFiles []os.FileInfo
var changelogEntries []ChangelogEntry
files, err := ioutil.ReadDir(changelogEntriesPath)
if err != nil {
fmt.Println(err)
return
}
for _, f := range files {
if filepath.Ext(f.Name()) == ".json" {
changelogFiles = append(changelogFiles, f)
contents, err := ioutil.ReadFile(changelogEntriesPath + f.Name())
if err != nil {
fmt.Println(err)
return
}
//archive old changelogFiles
if archive {
err = ioutil.WriteFile(changelogArchivePath+sanitizeDescription(build)+"/"+f.Name(), contents, 0644)
if err != nil {
fmt.Println(err)
}
}
var entry ChangelogEntry
err = json.Unmarshal(contents, &entry)
if err != nil {
fmt.Println(err)
return
}
changelogEntries = append(changelogEntries, entry)
}
}
//Build output
b := &bytes.Buffer{}
if len(flavor) == 0 {
unflavoredContent(b, changelogEntries)
} else {
buildFlavoredContent(strings.ToLower(flavor), b, changelogEntries)
}
//Write files
os.OpenFile(changelogFilePath, os.O_RDONLY|os.O_CREATE, 0666)
fileContent, err := ioutil.ReadFile(changelogFilePath)
if err != nil {
fmt.Println(err)
return
}
ioutil.WriteFile(changelogFilePath, b.Bytes(), 0644)
f, err := os.OpenFile(changelogFilePath, os.O_APPEND|os.O_WRONLY, 0600)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
if _, err = f.Write(fileContent); err != nil {
fmt.Println(err)
return
}
ioutil.WriteFile(changelogArchivePath+build+"-CHANGELOG.md", b.Bytes(), 0644)
//remove old changelogFiles
if !keep {
for _, f := range changelogFiles {
os.Remove(changelogEntriesPath + f.Name())
}
}
},
}
func init() {
RootCmd.AddCommand(combineCmd)
combineCmd.Flags().StringVarP(&build, "build", "b", "", "Required: Build version used as header in .md file")
combineCmd.Flags().StringVarP(¬e, "note", "n", "", "Note about this build")
combineCmd.Flags().BoolVarP(&keep, "keep", "k", false, "Maintains the .json files in the unreleased directory")
combineCmd.Flags().StringVarP(&flavor, "flavor", "f", "", "Sets markdown flavor, can be 'github' or 'gitlab'")
combineCmd.Flags().BoolVarP(&archive, "archive", "a", false, "Archives the .json files to changelogs/released/[build]/")
}