forked from maxence-charriere/go-app
/
main.go
68 lines (53 loc) · 1.32 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
//go:generate go run gen.go
//go:generate go fmt
package main
import (
"context"
"os"
"os/signal"
_ "github.com/maxence-charriere/app"
"github.com/segmentio/conf"
)
func main() {
ld := conf.Loader{
Name: "goapp",
Args: os.Args[1:],
Commands: []conf.Command{
{Name: "init", Help: "Init the project layout and install the wasm dependencies."},
{Name: "build", Help: "Build the wasm app and its server."},
{Name: "run", Help: "Build and run the wasm app and its server."},
{Name: "clean", Help: "Delete the wasm app and its server."},
{Name: "update", Help: "Update to the latest version."},
{Name: "help", Help: "Show the help."},
},
}
ctx, cancel := ctxWithSignals(context.Background(), os.Interrupt)
defer cancel()
switch cmd, args := conf.LoadWith(nil, ld); cmd {
case "init":
initProject(ctx, args)
case "build":
buildProject(ctx, args)
case "run":
runProject(ctx, args)
case "clean":
cleanProject(ctx, args)
case "update":
update(ctx, args)
case "help":
ld.PrintHelp(nil)
default:
panic("unreachable")
}
}
func ctxWithSignals(ctx context.Context, sigs ...os.Signal) (context.Context, func()) {
ctx, cancel := context.WithCancel(ctx)
sigc := make(chan os.Signal)
signal.Notify(sigc, sigs...)
go func() {
defer close(sigc)
<-sigc
cancel()
}()
return ctx, cancel
}