-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.go
752 lines (708 loc) · 20.6 KB
/
runner.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
package nktest
import (
"bytes"
"context"
"crypto/md5"
"database/sql"
_ "embed"
"errors"
"fmt"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"text/template"
"time"
_ "github.com/lib/pq"
"github.com/yookoala/realpath"
)
// Runner is a nakama test runner.
type Runner struct {
// wd is the working directory.
wd string
// dir is the base directory.
dir string
// name is the app name.
name string
// podName is the pod name.
podName string
// volumeDir is the directory for container volumes.
volumeDir string
// buildConfigs are the module build configs.
buildConfigs []BuildConfig
// podId is the created pod id.
podId string
// podContainerId is the pod infrastructure container id.
podContainerId string
// nakamaContainerId is the nakama container id.
nakamaContainerId string
// postgresContainerId is the postgres container id.
postgresContainerId string
// postgresLocal is the local postgres address.
postgresLocal string
// postgresRemote is the remote postgres address.
postgresRemote string
// grpcLocal is the local grpc address.
grpcLocal string
// grpcRemote is the remote grpc address.
grpcRemote string
// httpLocal is the local http address.
httpLocal string
// httpRemote is the remote http address.
httpRemote string
// consoleLocal is the local console address.
consoleLocal string
// consoleRemote is the remote console address.
consoleRemote string
// runEnv is the run env.
runEnv map[string]string
}
// NewRunner creates a new nakama test runner.
func NewRunner(opts ...Option) *Runner {
r := new(Runner)
for _, o := range opts {
o(r)
}
return r
}
// init initializes the environment for running nakama-pluginbuilder and nakama
// images.
func (r *Runner) init(ctx context.Context) error {
var err error
// get working directory
if r.wd, err = os.Getwd(); err != nil {
return fmt.Errorf("unable to get working directory: %w", err)
}
if r.wd, err = realpath.Realpath(r.wd); err != nil {
return fmt.Errorf("unable to determine real path for %s: %w", r.dir, err)
}
// use working directory as base if not set
if r.dir == "" {
r.dir = r.wd
}
// setup working directory
if r.dir, err = realpath.Realpath(r.dir); err != nil {
return fmt.Errorf("unable to determine real path for %s: %w", r.dir, err)
}
// setup name
if r.name == "" {
r.name = filepath.Base(r.dir)
}
// setup pod name
if r.podName == "" {
r.podName = ShortId(fmt.Sprintf("%x", md5.Sum([]byte(r.dir))))
}
// set volume directory
if r.volumeDir == "" {
r.volumeDir = filepath.Join(r.dir, ".cache")
if err := os.MkdirAll(r.volumeDir, 0o755); err != nil {
return fmt.Errorf("unable to create volume dir %s: %w", r.volumeDir, err)
}
}
// check that volume dir is subdir of working dir
if err := IsSubDir(r.dir, r.volumeDir); err != nil {
return fmt.Errorf("%s must be subdir of %s: %w", r.volumeDir, r.dir, err)
}
// ensure postgres and nakama volume directories exist
for _, s := range []string{"nakama", "postgres"} {
d := filepath.Join(r.volumeDir, s)
if err := os.MkdirAll(d, 0o755); err != nil {
return fmt.Errorf("unable to create cache dir %s: %w", d, err)
}
}
// load config template
tpl, err := template.New("").Parse(ConfigTemplate(ctx))
if err != nil {
return fmt.Errorf("unable to compile config template: %w", err)
}
// exec config template
buf := new(bytes.Buffer)
if err := tpl.Execute(buf, map[string]interface{}{
"name": r.name,
}); err != nil {
return fmt.Errorf("unable to execute template: %w", err)
}
// write config template
configFilename := ConfigFilename(ctx)
if err := os.WriteFile(filepath.Join(r.volumeDir, "nakama", configFilename), buf.Bytes(), 0o644); err != nil {
return fmt.Errorf("unable to write %s: %w", configFilename, err)
}
return nil
}
// Run handles building the nakama plugin and starting the postgres and
// nakama server containers.
func (r *Runner) Run(ctx context.Context) error {
// setup project working directory
if err := r.init(ctx); err != nil {
return err
}
// cleanup environment
if err := PodmanPodKill(ctx, r.podName); err != nil {
return err
}
// images
postgresImageId, pluginbuilderImageId, nakamaImageId := PostgresImageId(ctx), PluginbuilderImageId(ctx), NakamaImageId(ctx)
// versions
postgresVersion := PostgresVersion(ctx)
nakamaVersion, err := NakamaVersion(ctx)
if err != nil {
return err
}
Trace(ctx).Str("version", nakamaVersion).Msg("nakama")
qualifiedPostgresId := QualifiedId(postgresImageId + ":" + postgresVersion)
qualifiedPluginbuilderId := QualifiedId(pluginbuilderImageId + ":" + nakamaVersion)
qualifiedNakamaId := QualifiedId(nakamaImageId + ":" + nakamaVersion)
// retrieve images
if err := PodmanPullImages(
ctx,
qualifiedPostgresId,
qualifiedPluginbuilderId,
qualifiedNakamaId,
); err != nil {
return fmt.Errorf("unable to retrieve images: %w", err)
}
// build modules
if err := r.BuildModules(ctx, qualifiedPluginbuilderId); err != nil {
return fmt.Errorf("unable to build modules: %w", err)
}
// create network for images
if r.podId, r.podContainerId, err = PodmanCreatePod(
ctx,
r.podName,
QualifiedId(postgresImageId+":"+postgresVersion),
QualifiedId(nakamaImageId+":"+nakamaVersion),
); err != nil {
return fmt.Errorf("unable to create pod: %w", err)
}
// run postgres
if err := r.RunPostgres(ctx, qualifiedPostgresId); err != nil {
return fmt.Errorf("unable to start postgres: %w", err)
}
// run nakama
if err := r.RunNakama(ctx, qualifiedNakamaId); err != nil {
return fmt.Errorf("unable to start nakama: %w", err)
}
go func() {
<-ctx.Done()
PodmanStopAndRemove(ctx, qualifiedNakamaId, r.nakamaContainerId)
_ = PodmanWait(ctx, r.nakamaContainerId)
PodmanStopAndRemove(ctx, qualifiedPostgresId, r.postgresContainerId)
_ = PodmanWait(ctx, r.postgresContainerId)
_ = PodmanPodKill(ctx, r.podName)
}()
return nil
}
// BuildModules builds the nakama modules.
func (r *Runner) BuildModules(ctx context.Context, id string) error {
for i, bc := range r.buildConfigs {
ctx, cancel := context.WithCancel(ctx)
if err := r.BuildModule(ctx, id, &bc); err != nil {
cancel()
return fmt.Errorf("unable to build module %d: %w", i, err)
}
cancel()
}
return nil
}
// BuildModule builds a nakama plugin module.
func (r *Runner) BuildModule(ctx context.Context, id string, bc *BuildConfig) error {
// check module path
if bc.modulePath == "" {
return fmt.Errorf("must supply module path")
}
if strings.HasPrefix(bc.modulePath, "./") {
bc.modulePath = filepath.Join(r.dir, bc.modulePath)
}
dir, err := realpath.Realpath(bc.modulePath)
if err != nil {
return fmt.Errorf("unable to determine real path for %s: %w", dir, err)
}
// ensure module path is sub dir
if err := IsSubDir(r.dir, dir); err != nil {
return fmt.Errorf("%s must be subdir of %s: %w", dir, r.dir, err)
}
pkgDir, err := filepath.Rel(r.dir, dir)
if err != nil {
return fmt.Errorf("unable to make %s relative to %s: %w", dir, r.dir, err)
}
// apply module opts
for _, o := range bc.opts {
if err := o(bc); err != nil {
return fmt.Errorf("unable to configure module %s: %w", bc.modulePath, err)
}
}
// set defaults
if bc.name == "" {
bc.name = filepath.Base(dir)
}
if bc.out == "" {
bc.out = bc.name + ".so"
}
// build out
out := filepath.Join(r.volumeDir, "nakama", "modules", bc.out)
// remove module if exists
switch fi, err := os.Stat(out); {
case err == nil && !fi.IsDir():
if err := os.Remove(out); err != nil {
return fmt.Errorf("unable to remove %s: %w", out, err)
}
case err != nil && errors.Is(err, os.ErrNotExist):
case err != nil:
return fmt.Errorf("could not stat %s: %w", out, err)
}
entrypoint := []string{
"go",
"build",
"-trimpath",
"-buildmode=plugin",
}
if UnderCI(ctx) {
entrypoint = append(entrypoint,
"-a",
"-v",
"-x",
"-work",
)
}
entrypoint = append(entrypoint, bc.buildOpts...)
entrypoint = append(entrypoint, "-o=/nakama/modules/"+bc.out, "./"+pkgDir)
containerId, err := PodmanRun(
ctx,
id,
r.podId,
bc.env,
append(
bc.mounts,
filepath.Join(r.dir)+":/builder",
filepath.Join(r.volumeDir, "nakama")+":/nakama",
),
entrypoint...,
)
if err != nil {
return fmt.Errorf("unable to run %s: %w", id, err)
}
go func() {
<-ctx.Done()
PodmanStopAndRemove(ctx, id, containerId)
}()
if err := PodmanFollowLogs(ctx, containerId, NakamaBuilderContainerShortName); err != nil {
return fmt.Errorf("unable to follow logs for %s: %w", ShortId(containerId), err)
}
ctx, cancel := context.WithTimeout(ctx, BuildTimeout(ctx))
defer cancel()
if err := PodmanWait(ctx, containerId); err != nil {
return err
}
// ensure it exists
fi, err := os.Stat(out)
switch {
case err != nil && errors.Is(err, os.ErrNotExist):
return fmt.Errorf("missing %s: %w", out, err)
case err != nil:
return fmt.Errorf("could not stat %s: %w", out, err)
}
// make out relative to wd if possible
if rel, err := filepath.Rel(r.wd, out); err == nil {
out = "./" + rel
}
Trace(ctx).Str("out", out).Int64("size", fi.Size()).Msg("built")
return nil
}
// RunPostgres runs the postgres server.
func (r *Runner) RunPostgres(ctx context.Context, id string) error {
var err error
r.postgresContainerId, err = PodmanRun(
ctx,
id,
r.podId,
map[string]string{
"listen_addresses": "'*'",
"POSTGRES_PASSWORD": r.name,
"POSTGRES_USER": r.name,
"POSTGRES_DB": r.name,
},
[]string{
filepath.Join(r.volumeDir, "postgres") + ":/var/lib/postgresql/data",
},
)
if err != nil {
return fmt.Errorf("unable to run %s: %w", id, err)
}
if err := PodmanFollowLogs(ctx, r.postgresContainerId, PostgresContainerShortName); err != nil {
return err
}
if err := PodmanWaitService(ctx, r.podId, "5432/tcp", func(ctx context.Context, local, remote string) error {
r.postgresLocal = fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", r.name, r.name, local, r.name)
r.postgresRemote = fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=disable", r.name, r.name, remote, r.name)
db, err := sql.Open("postgres", r.postgresLocal)
if err != nil {
return err
}
return db.Ping()
}); err != nil {
return fmt.Errorf("unable to connect to postgres %s: %w", ShortId(r.postgresContainerId), err)
}
return nil
}
// RunNakama runs the nakama server.
func (r *Runner) RunNakama(ctx context.Context, id string) error {
var err error
r.nakamaContainerId, err = PodmanRun(
ctx,
id,
r.podId,
r.runEnv,
[]string{
filepath.Join(r.volumeDir, "nakama") + ":/nakama/data",
},
`/bin/sh`,
`-ecx`,
`/nakama/nakama migrate up `+
`--database.address=`+r.postgresRemote+` && `+
`exec /nakama/nakama `+
`--config=/nakama/data/config.yml `+
`--database.address=`+r.postgresRemote,
)
if err != nil {
return fmt.Errorf("unable to run %s: %w", id, err)
}
// follow logs
if err := PodmanFollowLogs(ctx, r.nakamaContainerId, NakamaContainerShortName); err != nil {
return fmt.Errorf("unable to follow logs for %s: %w", ShortId(r.nakamaContainerId), err)
}
// wait for http to be available
if err := PodmanWaitService(ctx, r.podId, "7350/tcp", func(ctx context.Context, local, remote string) error {
r.httpLocal = "http://" + local
r.httpRemote = "http://" + remote
req, err := http.NewRequestWithContext(ctx, "GET", r.httpLocal+"/healthcheck", nil)
if err != nil {
return err
}
cl := &http.Client{}
res, err := cl.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return fmt.Errorf("status %d != 200", res.StatusCode)
}
return nil
}); err != nil {
return fmt.Errorf("unable to connect to %s (http): %w", ShortId(r.nakamaContainerId), err)
}
// grpc ports
if err := PodmanWaitService(ctx, r.podId, "7349/tcp", func(ctx context.Context, local, remote string) error {
r.grpcLocal = local
r.grpcRemote = remote
return nil
}); err != nil {
return fmt.Errorf("unable to connect to %s (grpc): %w", ShortId(r.nakamaContainerId), err)
}
// console ports
if err := PodmanWaitService(ctx, r.podId, "7351/tcp", func(ctx context.Context, local, remote string) error {
prefix := "http://" + r.name + ":" + r.name + "_password@"
r.consoleLocal = prefix + local
r.consoleRemote = prefix + remote
return nil
}); err != nil {
return fmt.Errorf("unable to connect to %s (console): %w", ShortId(r.nakamaContainerId), err)
}
return nil
}
// RunProxy creates and runs a http proxy until the context is closed.
func (r *Runner) RunProxy(ctx context.Context, opts ...ProxyOption) (string, error) {
return NewProxy(opts...).Run(ctx, r.httpLocal)
}
// PodId returns the pod id.
func (r *Runner) PodId() string {
return r.podId
}
// PodContainerId returns the pod infrastructure container id.
func (r *Runner) PodContainerId() string {
return r.podContainerId
}
// PostgresLocal returns the postgres local address.
func (r *Runner) PostgresLocal() string {
return r.postgresLocal
}
// PostgresRemote returns the postgres remote address.
func (r *Runner) PostgresRemote() string {
return r.postgresRemote
}
// HttpLocal returns the http local address.
func (r *Runner) HttpLocal() string {
return r.httpLocal
}
// HttpRemote returns the http remote address.
func (r *Runner) HttpRemote() string {
return r.httpRemote
}
// GrpcLocal returns the grpc local address.
func (r *Runner) GrpcLocal() string {
return r.grpcLocal
}
// GrpcRemote returns the grpc remote address.
func (r *Runner) GrpcRemote() string {
return r.grpcRemote
}
// ConsoleLocal returns the console local address.
func (r *Runner) ConsoleLocal() string {
return r.consoleLocal
}
// ConsoleRemote returns the console remote address.
func (r *Runner) ConsoleRemote() string {
return r.consoleRemote
}
// Name returns the name.
func (r *Runner) Name() string {
return r.name
}
// HttpKey returns the http key.
func (r *Runner) HttpKey() string {
return r.name
}
// ServerKey returns the server key.
func (r *Runner) ServerKey() string {
return r.name + "_server"
}
// Option is a nakama test runner option.
type Option func(*Runner)
// WithDir is a nakama test runner option to set the project root dir.
func WithDir(dir string) Option {
return func(r *Runner) {
r.dir = dir
}
}
// WithVolumeDir is a nakama test runner option to set the volume dir, where
// nakama and postgres data/configs are written. Default is <project
// root>/.cache. Must be a sub dir of the project root.
func WithVolumeDir(volumeDir string) Option {
return func(r *Runner) {
r.volumeDir = volumeDir
}
}
// WithRunEnv is a nakama test runner option to set run environment variables.
func WithRunEnv(runEnv map[string]string) Option {
return func(r *Runner) {
r.runEnv = runEnv
}
}
// WithBuildConfig is a nakama test runner option to add a module path, and extra
// options to the build config.
func WithBuildConfig(modulePath string, opts ...BuildConfigOption) Option {
return func(r *Runner) {
r.buildConfigs = append(r.buildConfigs, BuildConfig{
modulePath: modulePath,
opts: opts,
})
}
}
// BuildConfig is a nakama module build config.
type BuildConfig struct {
// modulePath is the package build path for the module. Must be sub dir of
// the working directory.
modulePath string
// opts are the module options.
opts []BuildConfigOption
// name is the name of the module.
name string
// out is the out filename of the module. Will be written to
// modules/<name>.
out string
// env are additional environment variables to pass to
env map[string]string
// mounts are additional volume mounts.
mounts []string
// buildOpts are additional go build options.
buildOpts []string
}
// BuildConfigOption is nakama module build config option.
type BuildConfigOption func(*BuildConfig) error
// WithOut is a nakama module build config option to set the out name. When not
// specified, the name will be derived from the directory name of the module.
func WithOut(out string) BuildConfigOption {
return func(bc *BuildConfig) error {
bc.out = out
return nil
}
}
// WithEnv is a nakama module build config option to set additional env
// variables used during builds.
func WithEnv(env map[string]string) BuildConfigOption {
return func(bc *BuildConfig) error {
if bc.env == nil {
bc.env = make(map[string]string)
}
for k, v := range env {
bc.env[k] = v
}
return nil
}
}
// WithGoEnv is a nakama module build config option to copy the host Go
// environment variables.
func WithGoEnv(env ...string) BuildConfigOption {
return func(bc *BuildConfig) error {
if bc.env == nil {
bc.env = make(map[string]string)
}
goPath, err := exec.LookPath("go")
if err != nil {
return fmt.Errorf("unable to locate go")
}
for _, k := range env {
v, err := GoEnvVar(goPath, k)
if err != nil {
return fmt.Errorf("unable to exec go env %s: %w", k, err)
}
bc.env[k] = v
}
return nil
}
}
// WithDefaultGoEnv is a nakama module build config option to copy default host
// environment variables for Go.
//
// Copies:
//
// GONOPROXY
// GONOSUMDB
// GOPRIVATE
// GOPROXY
// GOSUMDB
func WithDefaultGoEnv() BuildConfigOption {
return WithGoEnv(
"GONOPROXY",
"GONOSUMDB",
"GOPRIVATE",
"GOPROXY",
"GOSUMDB",
)
}
// WithMounts is a nakama module build config option to set additional mounts
// used during builds.
func WithMounts(mounts ...string) BuildConfigOption {
return func(bc *BuildConfig) error {
bc.mounts = append(bc.mounts, mounts...)
return nil
}
}
// WithGoVolumes is a nakama module build config option to mount the host's Go
// directories (ie, the Go environment's GOCACHE, GOMODCACHE, and GOPATH
// locations) to the plugin builder container. Significantly speeds up build
// times.
//
// Note: use WithDefaultGoVolumes (see below).
func WithGoEnvVolumes(volumes ...EnvVolumeInfo) BuildConfigOption {
return func(bc *BuildConfig) error {
goPath, err := exec.LookPath("go")
if err != nil {
return fmt.Errorf("unable to locate go")
}
for _, vol := range volumes {
v, err := GoEnvVar(goPath, vol.Key)
if err != nil {
return fmt.Errorf("unable to exec go env %s: %w", vol.Key, err)
}
if vol.Sub != "" {
v = filepath.Join(v, vol.Sub)
}
if err := os.MkdirAll(v, 0o755); err != nil {
return fmt.Errorf("unable to mkdir: %w", err)
}
if v, err = realpath.Realpath(v); err != nil {
key := vol.Key
if vol.Sub != "" {
key += "/" + vol.Sub
}
return fmt.Errorf("unable to get real path for go env %s (%s): %w", key, v, err)
}
bc.mounts = append(bc.mounts, v+":"+vol.Target)
}
return nil
}
}
// WithGoVolumes is a nakama module build config option to mount the host's Go
// directories (GOCACHE, GOMODCACHE, and GOPATH) to the plugin builder
// container. Significantly speeds up build times.
func WithDefaultGoVolumes() BuildConfigOption {
return WithGoEnvVolumes(
NewEnvVolume("GOCACHE", "/root/.cache/go-build", ""),
NewEnvVolume("GOMODCACHE", "/go/pkg/mod", ""),
NewEnvVolume("GOPATH", "/go/src", "src"),
)
}
// WithGoBuildOptions is a nakama module build config option to add additional
// command-line options to Go build.
func WithGoBuildOptions(buildOpts ...string) BuildConfigOption {
return func(bc *BuildConfig) error {
bc.buildOpts = append(bc.buildOpts, buildOpts...)
return nil
}
}
// EnvVolumeInfo holds information about an environment variable derived
// volume.
type EnvVolumeInfo struct {
Key string
Target string
Sub string
}
// NewEnvVolume creates a new environment volume.
func NewEnvVolume(key, target, sub string) EnvVolumeInfo {
return EnvVolumeInfo{
Key: key,
Target: target,
Sub: sub,
}
}
// ReadCachedFile reads a cached file from disk, returns error if the file name
// on disk is past the ttl.
func ReadCachedFile(name string, ttl time.Duration) ([]byte, error) {
fi, err := os.Stat(name)
switch {
case err != nil:
return nil, err
case fi.IsDir():
return nil, fmt.Errorf("%s is a directory", name)
case fi.ModTime().Add(ttl).Before(time.Now()):
return nil, fmt.Errorf("%s needs to be refreshed (past %v)", name, ttl)
}
return os.ReadFile(name)
}
// IsSubDir determines if b is subdir of a.
func IsSubDir(a, b string) error {
if _, err := filepath.Rel(a, b); err != nil {
return fmt.Errorf("%s is not subdir of %s: %w", b, a, err)
}
ai, err := os.Lstat(a)
if err != nil {
return fmt.Errorf("%s does not exist", a)
}
for b != "" {
bi, err := os.Lstat(b)
if err != nil {
return fmt.Errorf("%s does not exist", b)
}
if os.SameFile(ai, bi) {
return nil
}
n := filepath.Dir(b)
if b == n {
break
}
b = n
}
return fmt.Errorf("%s is not a subdir of %s", b, a)
}
// GoEnvVar reads the go env variable from `go env <name>`.
func GoEnvVar(goPath, name string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
buf, err := exec.CommandContext(ctx, goPath, "env", name).CombinedOutput()
if err != nil {
return "", err
}
return strings.TrimSpace(string(buf)), nil
}