forked from falcosecurity/falco-talon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmageFile.go
123 lines (98 loc) · 2.85 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
//go:build mage
// +build mage
package main
import (
"fmt"
"os"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)
func Lint() error {
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
}
func FixLint() error {
if err := sh.RunV("golangci-lint", "run", "--fix"); err != nil {
return err
}
return nil
}
func Test() error {
return sh.RunV("go", "test", "./...", "-race")
}
func Run() error {
return sh.RunV("go", "run", "./...", "server", "-c", "config.yaml", "-r", "rules.yaml")
}
func BuildLocal() error {
ldFlags := generateLDFlags()
fmt.Println(ldFlags)
return sh.RunV("go", "build", "-trimpath", "-ldflags", ldFlags, "-o", "falco-talon", ".")
}
// BuildImages build images locally and not push
func BuildImages() error {
exportLDFlags()
os.Setenv("KO_DOCKER_REPO", "ko.local/falco-talon")
return sh.RunV("ko", "build", "--bare", "--sbom=none", "--tags", getVersion(), "--tags", getCommit(), "--tags", "latest",
"github.com/Issif/falco-talon")
}
func Build() error {
exportLDFlags()
return sh.RunV("goreleaser", "release", "--clean", "--snapshot", "--skip-sbom", "--skip-publish")
}
func Release() error {
mg.Deps(Test)
exportLDFlags()
return sh.RunV("goreleaser", "release", "--clean", "--skip-sign", "--skip-sbom")
}
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
}
// repo without any tags in it
return "v0.0.0"
}
// 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 := "github.com/Issif/falco-talon/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())
}