gitstack

grindlemire/gothem-stack code browser

4.3 KB Go 176 lines 2025-01-11 · 7257ca7 raw
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
166
167
168
169
170
171
172
173
174
175
176
package main

import (
	"context"
	"os"
	"time"

	"github.com/grindlemire/gothem-stack/pkg/log"
	"github.com/pkg/errors"
	"go.uber.org/zap"
)

func init() {
	log.InitGlobal()
}

// Config is the identifying information pulled out of the environment to execute
// different mage commands
type Config struct {
	Env  string   `envconfig:"env" default:"local"`
	Args []string `envconfig:"args"    default:""`
}

func Install() (err error) {
	defer func(now time.Time) {
		if r := recover(); r != nil {
			err = errors.Errorf("%s", r)
		}
		finish(now, err)
	}(time.Now())

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// ignore the first two args since they are "mage" and "init"
	return install(WithConfig(ctx, os.Args[2:]...))
}

// Tidy will run go mod tidy
func Tidy() (err error) {
	defer func(now time.Time) {
		if r := recover(); r != nil {
			err = errors.Errorf("%s", r)
		}
		finish(now, err)
	}(time.Now())

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// ignore the first two args since they are "mage" and "tidy"
	return tidy(WithConfig(ctx, os.Args[2:]...))
}

// Build will build a new binary
func Build() (err error) {
	defer func(now time.Time) {
		if r := recover(); r != nil {
			err = errors.Errorf("%s", r)
		}
		finish(now, err)
	}(time.Now())

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// ignore the first two args since they are "mage" and "build"
	return build(WithConfig(ctx, os.Args[2:]...))
}

// Run will run a local dev server and UI
func Run() (err error) {
	defer func(now time.Time) {
		if r := recover(); r != nil {
			err = errors.Errorf("%s", r)
		}
		finish(now, err)
	}(time.Now())

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// ignore the first two args since they are "mage" and "run"
	return run(WithConfig(ctx, os.Args[2:]...))
}

// Templ will run the templ command and generate the go code for the templates
func Templ() (err error) {
	defer func(now time.Time) {
		if r := recover(); r != nil {
			err = errors.Errorf("%s", r)
		}
		finish(now, err)
	}(time.Now())

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// ignore the first two args since they are "mage" and "templ"
	return templ(WithConfig(ctx, os.Args[2:]...))
}

// Deploy will deploy the static site to Firebase hosting
func Deploy() (err error) {
	defer func(now time.Time) {
		if r := recover(); r != nil {
			err = errors.Errorf("%s", r)
		}
		finish(now, err)
	}(time.Now())

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// ignore the first two args since they are "mage" and "deploy"
	return deploy(WithConfig(ctx, os.Args[2:]...))
}

// Auth will authenticate with cloud services (gcloud or firebase)
func Auth() (err error) {
	defer func(now time.Time) {
		if r := recover(); r != nil {
			err = errors.Errorf("%s", r)
		}
		finish(now, err)
	}(time.Now())

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// ignore the first two args since they are "mage" and "auth"
	return auth(WithConfig(ctx, os.Args[2:]...))
}

// Release will deploy a specific version of the service to Cloud Run
func Release() (err error) {
	defer func(now time.Time) {
		if r := recover(); r != nil {
			err = errors.Errorf("%s", r)
		}
		finish(now, err)
	}(time.Now())

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// ignore the first two args since they are "mage" and "release"
	return release(WithConfig(ctx, os.Args[2:]...))
}

// Destroy will delete all remote cloud infrastructure created during deploy
func Destroy() (err error) {
	defer func(now time.Time) {
		if r := recover(); r != nil {
			err = errors.Errorf("%s", r)
		}
		finish(now, err)
	}(time.Now())

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	// ignore the first two args since they are "mage" and "destroy"
	return destroy(WithConfig(ctx, os.Args[2:]...))
}

func finish(start time.Time, err error) {
	zap.S().Infof("elapsed time: %s", time.Since(start))
	// This is a hack to get around the fact that mage treats command line args
	// as other targets. In a run we just want to interpret them as arugments to the binary,
	// not as other targets. So we just short circuit and tell mage to stop
	if err != nil {
		zap.S().Fatal(err)
	}
	os.Exit(0)
}