-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
244 lines (212 loc) · 6.29 KB
/
main.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* Copyright (c) 2022, nwillc@gmail.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
package main
import (
"flag"
"fmt"
"github.com/nwillc/genfuncs"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/nwillc/gorelease/gen/version"
"github.com/nwillc/gorelease/setup"
"github.com/nwillc/gorelease/utils"
"golang.org/x/mod/modfile"
"golang.org/x/mod/semver"
)
const v2 = "v2.0.0"
func main() {
flag.Parse()
if *setup.Flags.Version {
fmt.Printf("version %s\n", version.Version)
os.Exit(setup.NormalExit)
}
if *setup.Flags.DryRun {
log.Println("Performing dry run.")
}
// Get the repo
repo := utils.GetRepository("")
w, err := repo.Worktree()
utils.CheckIfError("repository access", err)
// Check that we are ready for release.
if err := repoReady(w); err != nil {
panic(err)
}
// Get new version.
tag := getNewVersion()
/*
* Create the new version file.
*/
createVersionGo(*setup.Flags.Output, tag)
if *setup.Flags.DryRun {
os.Exit(setup.NormalExit)
}
// Git add the .version and version files.
addVersionFiles(w)
/*
* Git commit the files.
*/
_, err = w.Commit(fmt.Sprintf("Updated for release %s", tag), &git.CommitOptions{
Author: utils.NewSignature(),
})
utils.CheckIfError("committing files", err)
/*
* Git create new tag.
*/
setTag(repo, tag).
OnError(func(e error) {
utils.CheckIfError("setting tag", e)
}).
OnSuccess(func(ok bool) {
if !ok {
panic(fmt.Errorf("unable to set tag %s", tag))
}
})
sshKey := utils.PublicKeys()
/*
* Git push the tag
*/
err = repo.Push(&git.PushOptions{
RemoteName: "origin",
RefSpecs: []config.RefSpec{"refs/tags/*:refs/tags/*"},
})
if err != nil && sshKey.Ok() {
err = repo.Push(&git.PushOptions{
RemoteName: "origin",
RefSpecs: []config.RefSpec{"refs/tags/*:refs/tags/*"},
Auth: sshKey.MustGet(),
})
if err != nil {
if *setup.Flags.Verbose {
log.Println(err)
}
log.Printf("Push failed, please: git push origin %s; git push", tag)
}
}
/*
* Push the entire repo
*/
err = repo.Push(&git.PushOptions{})
if err != nil && sshKey.Ok() {
_ = repo.Push(&git.PushOptions{Auth: sshKey.MustGet()})
}
}
func addVersionFiles(w *git.Worktree) {
_, err := w.Add(*setup.Flags.Output)
utils.CheckIfError(fmt.Sprintf("adding %s", *setup.Flags.Output), err)
_, err = w.Add(setup.DotVersionFile)
utils.CheckIfError(fmt.Sprintf("adding %s", setup.DotVersionFile), err)
}
func getNewVersion() string {
content, err := ioutil.ReadFile(setup.DotVersionFile)
utils.CheckIfError("reading .version", err)
versionStr := strings.Replace(string(content), "\n", "", -1)
if !semver.IsValid(versionStr) {
msg := fmt.Sprintf("invalid version %s", versionStr)
log.Println(msg)
panic(fmt.Errorf(msg))
}
tag := semver.Canonical(versionStr)
if semver.Compare(tag, v2) >= 0 {
log.Printf("Warning %s >= %s", tag, v2)
content, err := ioutil.ReadFile(setup.ModuleFile)
utils.CheckIfError("unable to read go.mod", err)
modFile, err := modfile.Parse(setup.ModuleFile, content, nil)
utils.CheckIfError("unable to parse go.mod", err)
if !strings.HasSuffix(modFile.Module.Mod.Path, semver.Major(tag)) {
log.Printf("Major version specified (%s) not found at end of go.mod module %s", semver.Major(tag), modFile.Module.Mod.Path)
os.Exit(setup.VersionConflict)
}
}
return tag
}
func repoReady(w *git.Worktree) error {
status, err := w.Status()
utils.CheckIfError("repository status", err)
if len(status) != 1 {
msg := fmt.Sprintf("incorrrect file commit status, %d files, expecting only %s", len(status), setup.DotVersionFile)
if *setup.Flags.Dirty {
log.Println("dirty override: ", msg)
} else {
return fmt.Errorf(msg)
}
}
vs := status.File(setup.DotVersionFile)
if vs.Staging == '?' && vs.Worktree == '?' {
msg := fmt.Sprintf("%s should be only uncommitted file", setup.DotVersionFile)
if *setup.Flags.Dirty {
log.Println("dirty override: ", msg)
} else {
return fmt.Errorf(msg)
}
}
return nil
}
func createVersionGo(fileName string, tag string) {
licenseStr := ""
for _, licenseFile := range strings.Fields(setup.LicenseFiles) {
contents, err := ioutil.ReadFile(licenseFile)
if err == nil {
licenseStr = "/*\n *" + strings.Replace(string(contents), "\n", "\n *", -1) + "\n */\n"
break
}
}
versionGo := strings.Replace(setup.VersionTemplateStr, "$LICENSE$", licenseStr, 1)
versionGo = strings.Replace(versionGo, "$TAG$", tag, 1)
path := filepath.Dir(fileName)
folderInfo, err := os.Stat(path)
if os.IsNotExist(err) || !folderInfo.IsDir() {
log.Printf("Folder %s does not exist - creating\n", path)
err := os.MkdirAll(path, 0755)
utils.CheckIfError("Unable to create", err)
}
packageName := filepath.Base(path)
if packageName == "." {
packageName = "main"
}
versionGo = strings.Replace(versionGo, "$PACKAGE$", packageName, -1)
f, err := os.Create(fileName)
utils.CheckIfError("creating version.go", err)
defer f.Close()
_, err = f.WriteString(versionGo)
utils.CheckIfError("writing version.go", err)
}
func setTag(r *git.Repository, tag string) *genfuncs.Result[bool] {
if utils.TagExists(r, tag) {
log.Printf("tag %s already exists", tag)
return genfuncs.NewResult(false)
}
log.Println("Set tag ", tag)
h, err := r.Head()
if err != nil {
log.Println("get HEAD error:", err)
return genfuncs.NewError[bool](err)
}
_, err = r.CreateTag(tag, h.Hash(), &git.CreateTagOptions{
Tagger: utils.NewSignature(),
Message: "Release " + tag,
})
if err != nil {
log.Println("create tag error:", err)
return genfuncs.NewError[bool](err)
}
return genfuncs.NewResult(true)
}