-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate.go
106 lines (89 loc) · 2.5 KB
/
generate.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
package catalogcmd
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"path"
"syscall"
"github.com/peterbourgon/ff/v3/ffcli"
"github.com/rs/zerolog/log"
)
func (c *Config) GenerateCommand() *ffcli.Command {
fs := flag.NewFlagSet("catalog-api catalog generate", flag.ExitOnError)
// register catalog generate flags
c.RegisterGenerateFlags(fs)
// register catalog & global flags
c.RegisterFlags(fs)
return &ffcli.Command{
Name: "generate",
ShortUsage: "catalog-api catalog generate [flags]",
ShortHelp: "Generate a static catalog API",
FlagSet: fs,
Exec: c.rootConfig.PreExec(c.execGenerate),
}
}
func (c *Config) RegisterGenerateFlags(fs *flag.FlagSet) {
fs.StringVar(&c.tempDir, "temp-dir", defaultTempDir, "path to a temporary directory for generated files")
fs.BoolVar(&c.snapshot, "snapshot", defaultSnapshot, "generate a catalog api for the current catalog branch")
fs.BoolVar(&c.watch, "watch", defaultWatchMode, "enter watch mode, which rebuilds on file change")
}
func (c *Config) execGenerate(ctx context.Context, _ []string) error {
if c.watch {
return c.execGenerateWatcher(ctx)
}
return c.execRunGenerate(ctx)
}
func (c *Config) execGenerateWatcher(ctx context.Context) error {
// produce initial build
outdir, err := c.generate(ctx)
if err != nil {
return err
}
log.Info().Msgf("outdir: %s", outdir)
// configure channel for exit signal
exit := make(chan os.Signal, 1)
signal.Notify(exit, os.Interrupt, syscall.SIGTERM)
// setup file-system notifications
watcher, err := c.createWatcher(ctx)
if err != nil {
return err
}
defer watcher.Close()
// wait on fs events or exit signal
for {
lastOutdir := outdir
select {
case err := <-dedupeWatchEvents(ctx, watcher):
if err != nil {
log.Error().Err(err)
continue
}
log.Debug().Msg("update detected, rebuilding...")
outdir, err = c.generate(ctx)
if err != nil {
log.Error().Err(err)
}
log.Debug().Msgf("deleting last tmpdir: %s", lastOutdir)
if err := os.RemoveAll(lastOutdir); err != nil {
log.Error().Err(err)
}
log.Info().Msgf("new outdir: %s", outdir)
case <-exit:
return nil
}
}
}
func (c *Config) execRunGenerate(ctx context.Context) error {
outDir, err := c.generate(ctx)
if err != nil {
return err
}
releaseDir := path.Join(outDir, "release")
// print outputs for github actions
// TODO(jk): enable this with a command flag,
// e.g. --with-github-action-outputs
fmt.Printf("::set-output name=release-dir::%s\n", releaseDir)
return nil
}