-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmage.go
157 lines (129 loc) · 3.44 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
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 (
"errors"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
)
const (
binaryName = "moody-core"
serviceDirectory = "services"
serviceSourceDirectory = "internal/services"
)
// Build the application into a single binary for the target architecture
func Build() error {
return command("go", "build", "-ldflags", "-s -w", "./cmd/moody-core").Run()
}
// Buildrpi builds the application into a single binary targeting a 32-bit Linux on Raspberry Pi
func Buildrpi() error {
cmd := command("go", "build", "-ldflags", "-s -w", "./cmd/moody-core")
cmd.Env = append(os.Environ(), "GOOS=linux", "GOARCH=arm", "GOARM=7")
return cmd.Run()
}
// Clean removes previously built binaries
func Clean() error {
return os.Remove(binaryName)
}
// Install the application after building it, by placing it in GOPATH/bin
func Install() error {
if err := os.MkdirAll("/etc/moody", 0755); err != nil {
return err
}
if err := os.MkdirAll("/usr/local/lib/moody", 0755); err != nil {
return err
}
if err := copyFile("./config/conf.json", "/etc/moody/conf.json"); err != nil {
return err
}
if err := copyFile("./config/systemd/moody.service", "/etc/systemd/system/moody.service"); err != nil {
return err
}
if err := copyFile(binaryName, "/usr/local/bin/"+binaryName); err != nil {
return err
}
if err := command("systemctl", "enable", "moody").Run(); err != nil {
return err
}
return command("systemctl", "start", "moody").Run()
}
func Uninstall() error {
if err := command("systemctl", "disable", "moody").Run(); err != nil {
return err
}
if err := os.RemoveAll("/etc/moody"); err != nil {
return err
}
if err := os.RemoveAll("/usr/local/lib/moody"); err != nil {
return err
}
if err := os.Remove("/etc/systemd/system/moody.service"); err != nil {
return err
}
return os.Remove("/usr/local/bin/moody-core")
}
// Test the application by running go test in each sub-directory
func Test() error {
return command("go", "test", "./...").Run()
}
// Services build every service found within ./internal/services/
func Services() error {
if err := os.Mkdir(serviceDirectory, 0755); err != nil && !errors.Is(err, os.ErrExist) {
return err
}
err := filepath.WalkDir(serviceSourceDirectory, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() && d.Name() != serviceDirectory {
cmd := command("go", "build", "-ldflags", "-s -w", "-buildmode", "plugin")
cmd.Dir = serviceSourceDirectory + "/" + d.Name()
err := cmd.Run()
if err != nil {
return err
}
targetName := "/" + d.Name() + ".so"
err = moveFile(cmd.Dir+targetName, serviceDirectory+targetName)
if err != nil {
return err
}
}
return nil
})
return err
}
func command(command string, args ...string) *exec.Cmd {
cmd := exec.Command(command, args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd
}
func copyFile(inName string, outName string) error {
dest, err := os.OpenFile(outName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
return err
}
defer dest.Close()
src, err := os.Open(inName)
if err != nil {
return err
}
if _, err = io.Copy(dest, src); err != nil {
_ = os.Remove(outName)
return err
}
src.Close()
return nil
}
func moveFile(inName string, outName string) error {
if err := copyFile(inName, outName); err != nil {
return err
}
if err := os.Remove(inName); err != nil {
return err
}
return nil
}