-
Notifications
You must be signed in to change notification settings - Fork 0
/
mage.go
103 lines (88 loc) · 1.93 KB
/
mage.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
//go:build mage
// +build mage
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/magefile/mage/sh"
"github.com/magefile/mage/target"
)
const (
name = "gobl.verifactu"
mainBranch = "main"
)
// Build the binary
func Build() error {
changed, err := target.Dir("./"+name, ".")
if os.IsNotExist(err) || (err == nil && changed) {
return build("build")
}
return nil
}
// Install the binary into your go bin path
func Install() error {
return build("install")
}
func build(action string) error {
args := []string{action}
flags, err := buildFlags()
if err != nil {
return err
}
args = append(args, flags...)
args = append(args, "./cmd/"+name)
return sh.RunV("go", args...)
}
func buildFlags() ([]string, error) {
ldflags := []string{
fmt.Sprintf("-X 'main.date=%s'", date()),
}
if v, err := version(); err != nil {
return nil, err
} else if v != "" {
ldflags = append(ldflags, fmt.Sprintf("-X 'main.version=%s'", v))
}
out := []string{}
if len(ldflags) > 0 {
out = append(out, "-ldflags="+strings.Join(ldflags, " "))
}
return out, nil
}
func version() (string, error) {
vt, err := versionTag()
if err != nil {
return "", err
}
v := []string{vt}
if b, err := branch(); err != nil {
return "", err
} else if b != mainBranch {
v = append(v, b)
}
if uncommittedChanges() {
v = append(v, "uncommitted")
}
return strings.Join(v, "-"), nil
}
func versionTag() (string, error) {
return trimOutput("git", "describe", "--tags") // no "--exact-match"
}
func branch() (string, error) {
return trimOutput("git", "rev-parse", "--abbrev-ref", "HEAD")
}
func uncommittedChanges() bool {
err := sh.Run("git", "diff-index", "--quiet", "HEAD", "--")
return err != nil
}
func date() string {
return time.Now().UTC().Format(time.RFC3339)
}
func trimOutput(cmd string, args ...string) (string, error) {
txt, err := sh.Output(cmd, args...)
if err != nil {
return "", err
}
return strings.TrimSpace(txt), nil
}