-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMagefile.go
162 lines (139 loc) · 3.42 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
159
160
161
162
//go:build mage
// +build mage
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"strings"
"time"
)
const Name = "dnsmasq-web"
var Default = All
// Get the build info from git and add a datetime stamp
func buildInfo() (string, error) {
// Get the git describe output
cmd := exec.Command("git", "describe", "--tags", "--always")
output, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("failed to get build info: %v", err)
}
gitInfo := strings.TrimSpace(string(output))
// Get the current datetime
datetime := time.Now().Format(time.RFC1123)
// Get the build user
user := os.Getenv("USER")
hostname := os.Getenv("HOSTNAME")
// Combine git info and datetime
buildInfo := fmt.Sprintf("%s built %s by %s@%s", gitInfo, datetime, user, hostname)
return buildInfo, nil
}
// Run the go build command
func runBuild(name string, envVars ...string) error {
buildInfo, err := buildInfo()
if err != nil {
return err
}
cmd := exec.Command("go", "build", "-buildmode=pie", "-ldflags",
fmt.Sprintf("-w -s -X 'main.Version=%s'", buildInfo), "-o", name)
if len(envVars) > 0 {
cmd.Env = append(os.Environ(), envVars...)
} else {
cmd.Env = os.Environ()
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
// Build the client POSIX shell environment
func createClientEnv(full, minify bool) error {
scripts := []string{"use.sh", "token.sh", "curl.sh"}
if full {
scripts = append(scripts, "curl_jq.sh", "jq_commands.sh", "reservations.sh")
}
var script bytes.Buffer
script.WriteString("# Dnsmasq Web Client environment")
for _, scriptName := range scripts {
content, err := os.ReadFile("cli/" + scriptName)
if err != nil {
return fmt.Errorf("failed to read %s: %v", scriptName, err)
}
lines := strings.Split(string(content), "\n")
for _, line := range lines {
if !strings.HasPrefix(line, "#!") {
script.WriteString(line + "\n")
}
}
}
cmdArgs := []string{"shfmt", "-p"}
if minify {
cmdArgs = append(cmdArgs, "-mn")
}
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
cmd.Stdin = &script
output, err := cmd.Output()
if err != nil {
return fmt.Errorf("failed to minify script: %v", err)
}
return os.WriteFile(fmt.Sprintf("%s.env", Name), output, 0755)
}
// Build the client environment
func ClientEnv() error {
return createClientEnv(false, true)
}
// Build the full client environment
func FullClientEnv() error {
return createClientEnv(true, false)
}
// Build binaries for pre-selected architectures
func Binaries() error {
for _, combo := range []struct {
CC string
GOARCH string
Name string
}{
{"gcc", "amd64", "dnsmasq-web-amd64"},
{"aarch64-suse-linux-gcc", "arm64", "dnsmasq-web-arm64"},
} {
envVars := []string{
"CC=" + combo.CC,
"CGO_ENABLED=1",
"GOARCH=" + combo.GOARCH,
"GOOS=linux",
}
if err := runBuild(combo.Name, envVars...); err != nil {
return err
}
}
return nil
}
// Build the client environment and the binaries
func All() error {
if err := ClientEnv(); err != nil {
return err
}
return Binaries()
}
func CleanBinaries() error {
for _, output := range []string{
"dnsmasq-web-amd64",
"dnsmasq-web-arm64",
} {
if err := os.RemoveAll(output); err != nil {
return err
}
}
return nil
}
func CleanClientEnv() error {
return os.RemoveAll(fmt.Sprintf("%s.env", Name))
}
func Clean() {
if err := CleanClientEnv(); err != nil {
fmt.Println(err)
}
if err := CleanBinaries(); err != nil {
fmt.Println(err)
}
}