forked from rgreinho/tfe-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagefile.go
158 lines (134 loc) · 3.38 KB
/
magefile.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
//go:build mage
// +build mage
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
const (
BuildDir = "dist"
GoArch = "amd64"
Project = "tfe-cli"
)
var (
Default = Setup
Platforms = []string{"linux", "darwin", "windows"}
)
var simpleSemVer = regexp.MustCompile(`-(\d{2}\.\d{2}\.\d{1,2}-[a-z]+)`)
type Coverage mg.Namespace
// Setup the full environment.
func Setup() {
sh.Run("go", "mod", "tidy")
}
// Run the full CI suite.
func Ci() {
sh.RunV("goimports", "-d", ".")
sh.RunV("golint", "./...")
sh.RunV("go", "vet")
mg.Deps(Test)
}
// Remove unwanted files in project (!DESTRUCTIVE!).
func Clean() {
sh.Run("git", "clean", "-ffdx")
sh.Run("git", "reset", "--hard")
}
func Test() {
sh.RunV("go", "test", "-v", "-cover", "-coverprofile=coverage.out", "./...")
}
// View code coverage in text.
func (Coverage) Text() {
sh.RunV("go", "tool", "cover", "-func=coverage.out")
}
// View code coverage in HTML.
func (Coverage) Html() {
sh.Run("go", "tool", "cover", "-html=coverage.out")
}
// Build binaries for the targeted platforms.
func Dist() {
tag := getTag()
currentDir, _ := os.Getwd()
for _, platform := range Platforms {
builder(platform, GoArch, tag, filepath.Join(currentDir, BuildDir, Project), currentDir)
}
}
// Publish a new GitHub release.
func Publish() {
currentDir, _ := os.Getwd()
buildDir := filepath.Join(currentDir, BuildDir)
files := getBuiltArtifacts()
assets := []string{}
// Get the assets to publish.
for _, file := range files {
assets = append(assets, "-a")
assets = append(assets, filepath.Join(buildDir, file.Name()))
}
// Prepare the command to run
if len(assets) == 0 {
fmt.Printf("Nothing to release in %s. Forgot to build?\n", buildDir)
os.Exit(1)
}
// Prepare the release arguments.
keeparelease_args := []string{
"-f",
"Changelog.md",
}
keeparelease_args = append(keeparelease_args, assets...)
sh.RunV("keeparelease", keeparelease_args...)
}
// Release a new version.
func Release() {
mg.Deps(Dist)
mg.Deps(Publish)
}
func builder(platform, arch, tag, out, cwd string) {
cmd := exec.Command(
"go",
"build",
fmt.Sprintf("-ldflags"),
fmt.Sprintf("-X github.com/rgreinho/tfe-cli/cmd.Version=%s", tag),
fmt.Sprintf("-o"),
fmt.Sprintf("%s-%s-%s-%s", out, tag, platform, arch),
)
cmd.Dir = cwd
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, fmt.Sprintf("GOOS=%s", platform))
cmd.Env = append(cmd.Env, fmt.Sprintf("GOARCH=%s", arch))
fmt.Printf("Build command: %s\n", cmd.String())
if stdoutStderr, err := cmd.CombinedOutput(); err != nil {
fmt.Printf("Error while running: %s\n", cmd.String())
fmt.Printf("%s\n", stdoutStderr)
}
}
func getTag() string {
tag := os.Getenv("GITHUB_REF")
if strings.HasPrefix(tag, "refs/tags/") {
return strings.Replace(tag, "refs/tags/", "", 1)
}
// If the tag could not be retrieved from the environment, use Git.
if tag == "" {
t, err := sh.Output("git", "describe")
if err != nil {
fmt.Printf("Cannot retrieve current git tag: %s.\n", err)
os.Exit(1)
}
tag = t
}
return tag
}
func getBuiltArtifacts() []os.FileInfo {
currentDir, _ := os.Getwd()
buildDir := filepath.Join(currentDir, BuildDir)
files, err := ioutil.ReadDir(buildDir)
if err != nil {
fmt.Printf("Cannot find to package in %s. Forgot to build?\n", buildDir)
os.Exit(1)
}
return files
}