/
bake.go
323 lines (293 loc) · 7.59 KB
/
bake.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// bake - release management tool
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"path"
"runtime"
"github.com/singhsaysdotcom/cobra"
"github.com/singhsaysdotcom/gdrive/gdrive"
"github.com/singhsaysdotcom/shlog"
"google.golang.org/api/drive/v2"
)
var (
// Flags
NAME string // populated via ldflags
VERSION string // populated via ldflags
GOOS = runtime.GOOS
GOARCH = runtime.GOARCH
versionFile string
enable_uploads bool
enable_git_push bool
enable_git_tag bool
enable_git_commit bool
enable_git_tasks bool
logger = shlog.NewLogger()
is_versioned bool
current_version *Version
logFile *os.File
errLogFile *os.File
goog *gdrive.Drive
)
func SetEnv() {
if os.Getenv("GOOS") != "" {
GOOS = os.Getenv("GOOS")
}
if os.Getenv("GOARCH") != "" {
GOARCH = os.Getenv("GOARCH")
}
}
func Build(args []string) bool {
SetEnv()
logger.Message("GOOS")
logger.Status(shlog.Grey, GOOS)
logger.Message("GOARCH")
logger.Status(shlog.Grey, GOARCH)
logger.Message("building %s ...", PkgName(""))
_, err := exec.LookPath("go")
if err != nil {
logger.Err()
return false
}
c := exec.Command("go", "build", "-v", "-ldflags", "-X \"main.VERSION="+current_version.String()+"\"", "-o", PkgName(""))
if len(args) > 0 {
c.Args = append(c.Args, args...)
}
if !CaptureLogs("build", c) {
logger.Err()
return false
} else {
logger.Ok()
return true
}
}
// git commits version file
func CommitVersion() bool {
var ok bool
logger.Message("git commit new version ...")
ok = CaptureLogs("git add", exec.Command("git", "add", versionFile))
if !ok {
logger.Err()
return false
}
ok = CaptureLogs("commitversion", exec.Command("git", "commit", versionFile, "-m", "Built new version "+current_version.String()))
if ok {
logger.Ok()
} else {
logger.Err()
}
return ok
}
// adds a git tag
func TagVersion() bool {
logger.Message("adding git tag ...")
ok := CaptureLogs("git tag", exec.Command("git", "tag", "v"+current_version.String()))
if ok {
logger.Ok()
} else {
logger.Err()
}
return ok
}
// git push to remote
func Push() bool {
logger.Message("git push to remote ...")
changes, err := exec.Command("git", "status", "-s").Output()
if err != nil {
logger.Status(shlog.Orange, "skipped - unknown status")
return false
}
if len(changes) > 0 {
logger.Status(shlog.Orange, "skipped - uncommited changes")
return false
}
remotes, err := exec.Command("git", "remote").Output()
if err != nil || len(remotes) == 0 {
logger.Status(shlog.Orange, "skipped - no remotes")
return false
}
err = exec.Command("git", "push").Run()
if err != nil {
logger.Err()
return false
}
logger.Ok()
return true
}
// Uploads the binary to google drive
func Upload() bool {
var (
err error
)
logger.Message("uploading new binary ...")
goog, err = gdrive.New("", false, false)
if err != nil {
logger.Status(shlog.Orange, "not configured")
return false
}
// TODO: check it file already exists and remove it
// TODO: add support for uploads to folders
filename := PkgName("")
f, err := os.Open(filename)
defer f.Close()
if err != nil {
logger.Err()
return false
}
fileRef := &drive.File{Title: path.Base(filename), MimeType: "application/octet-stream"}
reader := bufio.NewReader(f)
_, err = goog.Files.Insert(fileRef).Media(reader).Do()
if err != nil {
logger.Err()
return false
}
logger.Ok()
return true
}
func BuildCommon(save_version bool, upload bool, args []string) {
var ok bool
if !Build(args) {
return
}
if save_version {
if ok, _ = SaveVersion(&versionFile); !ok {
return
}
}
if IsGitRepo() && enable_git_tasks {
// Skip all git changes if no new version
if !save_version {
logger.Message("git commit new version ...")
logger.Status(shlog.Orange, "skipped - no new version")
logger.Message("adding git tag ...")
logger.Status(shlog.Orange, "skipped - no new version")
logger.Message("git push to remote ...")
logger.Status(shlog.Orange, "skipped - no new version")
} else {
if enable_git_commit && !CommitVersion() {
return
}
if enable_git_tag && !TagVersion() {
return
}
if enable_git_push && !Push() {
return
}
}
}
if enable_uploads && upload {
Upload()
}
logger.Done()
}
// PrintVersion prints the current version of bake.
func PrintVersion(cmd *cobra.Command, args []string) {
logger.Message("Bake Version")
logger.Status(shlog.Green, VERSION)
}
// Build a new major version
func BuildMajor(cmd *cobra.Command, args []string) {
if !is_versioned {
current_version = NewVersion()
logger.Message("new major version")
logger.Status(shlog.Green, current_version.String())
} else {
current_version.IncMajor()
}
BuildCommon(true, true, args)
}
// Build a new minor version
func BuildMinor(cmd *cobra.Command, args []string) {
if !is_versioned {
current_version = NewVersion()
logger.Message("new minor version")
logger.Status(shlog.Green, current_version.String())
} else {
current_version.IncMinor()
}
BuildCommon(true, true, args)
}
// Build at the next build number
func BuildNext(cmd *cobra.Command, args []string) {
if !is_versioned {
current_version = NewVersion()
logger.Message("new build version")
logger.Status(shlog.Green, current_version.String())
} else {
current_version.IncBuild()
}
BuildCommon(true, true, args)
}
// Rebuild at current version
func Rebuild(cmd *cobra.Command, args []string) {
BuildCommon(false, false, args)
}
// Rebuilds at the current version and reuploads to drive
func Reupload(cmd *cobra.Command, args []string) {
BuildCommon(false, true, args)
}
func main() {
rootCmd := &cobra.Command{
Use: "bake",
Short: "minimal build and release tool",
}
rootCmd.PersistentFlags().StringVarP(&versionFile, "version_file", "f", "VERSION", "name of the version file")
rootCmd.PersistentFlags().BoolVar(&enable_uploads, "enable_uploads", true, "enable uploads to Google Drive")
rootCmd.PersistentFlags().BoolVar(&enable_git_tasks, "enable_git_tasks", true, "enable git related tasks")
rootCmd.PersistentFlags().BoolVar(&enable_git_commit, "enable_git_commit", true, "enable git commits for version changes")
rootCmd.PersistentFlags().BoolVar(&enable_git_tag, "enable_git_tag", true, "enable git tagging for version changes.")
rootCmd.PersistentFlags().BoolVar(&enable_git_push, "enable_git_push", true, "enable git push to remotes")
is_versioned, current_version, _ = GetVersion(&versionFile)
// Log files
err := os.Mkdir(".log", 0755)
if err != nil && !os.IsExist(err) {
fmt.Printf("Error creating logs directory\n")
os.Exit(1)
}
logFile, err := os.Create(".log/bake.log")
defer logFile.Close()
if err != nil {
fmt.Printf("Error creating log file\n")
os.Exit(1)
}
errLogFile, err = os.Create(".log/bake.err.log")
defer errLogFile.Close()
if err != nil {
fmt.Printf("Error creating log file\n")
os.Exit(1)
}
versionCmd := &cobra.Command{
Use: "version",
Short: "prints current version of bake itself.",
Run: PrintVersion,
}
majorCmd := &cobra.Command{
Use: "major",
Short: "build a new major version",
Run: BuildMajor,
}
minorCmd := &cobra.Command{
Use: "minor",
Short: "build a new minor version",
Run: BuildMinor,
}
nextCmd := &cobra.Command{
Use: "next",
Short: "build at the next build number",
Run: BuildNext,
}
rebuildCmd := &cobra.Command{
Use: "rebuild",
Short: "rebuilds at the current version",
Run: Rebuild,
}
reuploadCmd := &cobra.Command{
Use: "reupload",
Short: "rebuilds at the current version and reuploads to drive",
Run: Reupload,
}
rootCmd.AddCommand(versionCmd, majorCmd, minorCmd, nextCmd, rebuildCmd, reuploadCmd)
rootCmd.Execute()
}