-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmageFile.go
157 lines (128 loc) · 3.92 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
//go:build mage
// +build mage
package main
import (
"fmt"
"os"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
const (
repoURL string = "github.com/falcosecurity/falco-talon"
)
type Lint mg.Namespace
type Build mg.Namespace
type Push mg.Namespace
type Release mg.Namespace
// lint:run runs linter
func (Lint) Run() error {
if err := sh.RunV("golangci-lint", "--version"); err != nil {
return err
}
if err := sh.RunV("golangci-lint", "run", "--timeout", "3m"); err != nil {
return err
}
if err := sh.RunV("go", "mod", "tidy"); err != nil {
return err
}
return nil
}
// lint:fix fixes linting issues
func (Lint) Fix() error {
if err := sh.RunV("golangci-lint", "run", "--fix"); err != nil {
return err
}
return nil
}
// test runs tests
func Test() error {
return sh.RunV("go", "test", "./...", "-race")
}
// run runs the app (with 'auto' as first argument, air is used to auto reload the app at each change)
func Run(autoreload string) error {
if err := sh.RunV("air", "-v"); err != nil {
return err
}
if autoreload == "auto" {
return sh.RunV("air", "server", "-c", "config.yaml", "-r", "rules.yaml")
}
return sh.RunV("go", "run", "./...", "server", "-c", "config.yaml", "-r", "rules.yaml")
}
// build:local builds a binary
func (Build) Local() error {
ldFlags := generateLDFlags()
return sh.RunV("go", "build", "-trimpath", "-ldflags", ldFlags, "-o", "falco-talon", ".")
}
// build:images builds the images and push them the local docker daemon
func (Build) Images() error {
exportLDFlags()
return sh.RunV("ko", "build", "--local", "--bare", "--sbom=none", "--tags", getVersion(), "--tags", getCommit(), "--tags", "latest",
repoURL)
}
// push:images builds the images and push them to the Dockerhub
func (Push) Images() error {
exportLDFlags()
os.Setenv("KO_DOCKER_REPO", "falcosecurity/falco-talon")
return sh.RunV("ko", "build", "--bare", "--sbom=none", "--tags", getVersion(), "--tags", getCommit(), "--tags", "latest",
repoURL)
}
// release:snapshot creates a release with current commit
func (Release) Snapshot() error {
exportLDFlags()
return sh.RunV("goreleaser", "release", "--clean", "--snapshot", "--skip=sbom", "--skip-publish")
}
// release:tag creates a release from current tag
func (Release) Tag() error {
mg.Deps(Test)
exportLDFlags()
return sh.RunV("goreleaser", "release", "--clean", "--skip=sign", "--skip=sbom")
}
// clean cleans temp folders
func Clean() {
files := []string{"falco-talon", "dist"}
for _, file := range files {
sh.Rm(file)
}
}
// exportLDFlags export as env vars the flags for go build
func exportLDFlags() {
os.Setenv("LDFLAGS", generateLDFlags())
}
// getVersion gets a description of the commit, e.g. v0.30.1 (latest) or v0.30.1-32-gfe72ff73 (canary)
func getVersion() string {
version, _ := sh.Output("git", "describe", "--tags", "--match=v*")
if version != "" {
return version
}
gitBranch, _ := sh.Output("git", "branch", "--show-current")
// repo without any tags in it
return gitBranch
}
// getCommit gets the hash of the current commit
func getCommit() string {
commit, _ := sh.Output("git", "rev-parse", "--short", "HEAD")
return commit
}
// getGitState gets the state of the git repository
func getGitState() string {
_, err := sh.Output("git", "diff", "--quiet")
if err != nil {
return "dirty"
}
return "clean"
}
// getBuildDateTime gets the build date and time
func getBuildDateTime() string {
result, _ := sh.Output("git", "log", "-1", "--pretty=%ct")
if result != "" {
sourceDateEpoch := fmt.Sprintf("@%s", result)
date, _ := sh.Output("date", "-u", "-d", sourceDateEpoch, "+%Y-%m-%dT%H:%M:%SZ")
return date
}
date, _ := sh.Output("date", "+%Y-%m-%dT%H:%M:%SZ")
return date
}
func generateLDFlags() string {
pkg := repoURL + "/configuration"
return fmt.Sprintf("-X %[1]s.GitVersion=%[2]s -X %[1]s.GitCommit=%[3]s -X %[1]s.GitTreeState=%[4]s -X %[1]s.BuildDate=%[5]s", pkg, getVersion(), getCommit(), getGitState(), getBuildDateTime())
}