This repository has been archived by the owner on Dec 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
165 lines (151 loc) · 4.25 KB
/
main.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
163
164
165
package main
import "strings"
import "bufio"
import "os"
import "os/exec"
import "path/filepath"
import log "github.com/sirupsen/logrus"
import "github.com/spf13/viper"
var CACHE = filepath.Join(os.ExpandEnv("$HOME"), ".cache", "boss")
func initConfig() {
viper.AddConfigPath("$HOME/.config")
viper.AddConfigPath("hooks")
viper.SetConfigName("boss")
viper.SetEnvPrefix("boss")
viper.AutomaticEnv()
// Optional settings
viper.SetDefault("build-cache", "$HOME/.cache/hugo")
viper.SetDefault("branch", "master")
viper.SetDefault("clean-destination-dir", true)
viper.SetDefault("minify", true)
viper.SetDefault("gc", true)
if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Error reading config file '%s'", err)
}
log.Infof("Using config '%s'", viper.ConfigFileUsed())
log.Infof("Destination is: %s", viper.Get("destination"))
log.Infof("Build-Cache is: %s", viper.Get("build-cache"))
}
func cache(path string) string {
return filepath.Join(CACHE, path)
}
func isDeployBranch(ref interface{}) bool {
var deployBranch strings.Builder
deployBranch.WriteString("refs/heads")
deployBranch.WriteString(viper.GetString("branch"))
return ref == deployBranch.String()
}
func receive() string {
var new string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
text := scanner.Text()
values := strings.Fields(text)
length := len(values)
if length < 3 {
log.Fatalf("Expected 3 parameters, received %d from input '%s'", length, text)
}
fields := log.Fields{
"old": values[0][:7],
"new": values[1][:7],
"ref": values[2],
}
logger := log.WithFields(fields)
logger.Info("payload received")
if isDeployBranch(fields["ref"]) {
logger.Infof("Deployment branch '%s' does not match. Skipping...", fields["ref"])
continue
}
new = values[1]
}
return new
}
func hugoArgs(path string) []string {
buildCache := os.ExpandEnv(viper.GetString("build-cache"))
cacheDir := filepath.Join(buildCache, filepath.Base(path))
destination := os.ExpandEnv(viper.GetString("destination"))
args := []string{
"--source", path,
"--cacheDir", cacheDir,
"--destination", destination,
}
if viper.GetBool("minify") {
args = append(args, "--minify")
}
if viper.GetBool("clean-destination-dir") {
args = append(args, "--cleanDestinationDir")
}
if viper.GetBool("gc") {
args = append(args, "--gc")
}
return args
}
func build(path string) {
args := hugoArgs(path)
cmd := exec.Command("hugo", args...)
output, err := cmd.CombinedOutput()
slicedout := strings.Split(strings.TrimSpace(string(output[:])), "\n")
fields := log.Fields{
"cmd": "hugo",
"src": filepath.Base(path),
"dst": viper.GetString("destination"),
}
logger := log.WithFields(fields)
for _, line := range slicedout {
logger.Info(line)
}
if err != nil {
logger.Fatal(err)
}
}
func cleanEnvironment() {
envvars := []string{"GIT_DIR", "GIT_WORK_TREE", "GIT_QUARANTINE_PATH"}
for _, v := range envvars {
fields := log.Fields{"env": v}
logger := log.WithFields(fields)
logger.Debug("Unsetting environment variable")
if err := os.Unsetenv(v); err != nil {
logger.Errorf("Could not unset environment variable: %s", err)
}
}
}
// TODO: Need to move anything *git* related into its own module
func checkout(revision string) string {
log.Debugf("creating directory '%s' in worktree cache", revision)
worktree := filepath.Join(CACHE, revision)
os.RemoveAll(worktree)
if err := os.MkdirAll(worktree, os.ModePerm); err != nil {
log.Fatalf("could not create '%s'", worktree, err)
}
cleanEnvironment()
args := []string{
"worktree",
"add",
worktree,
revision,
}
cmd := exec.Command("git", args...)
output, err := cmd.CombinedOutput()
slicedout := strings.Split(strings.TrimSpace(string(output[:])), "\n")
fields := log.Fields{"git": "worktree-add", "revision": revision[:7]}
logger := log.WithFields(fields)
for _, line := range slicedout {
logger.Info(line)
}
if err != nil {
log.Fatalf("could not create worktree for '%s': %s", revision, err)
}
return worktree
}
func main() {
formatter := &log.TextFormatter{
DisableTimestamp: true,
ForceColors: true, // We're only ever used as a git push
}
log.SetFormatter(formatter)
initConfig()
// This is where the magic happens :D
revision := receive()
worktree := checkout(revision)
build(worktree)
}