/
module.go
48 lines (41 loc) · 976 Bytes
/
module.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
package clifx
import (
"context"
"github.com/rs/zerolog/log"
"os"
"time"
"github.com/urfave/cli/v2"
"go.uber.org/fx"
)
func Main(opts ...fx.Option) {
rootCtx, cancel := context.WithCancel(context.Background())
var cliApp *cli.App
app := fx.New(fx.Options(opts...), fx.Options(
fx.Provide(
NewApp,
),
fx.Populate(&cliApp),
// todo figure out logger situation
fx.NopLogger,
))
// In a typical application, we could just use app.Run() here. Since this
// is a CLI tool, we'll use the more-explicit Start and Stop.
startCtx, cancel := context.WithTimeout(rootCtx, 15*time.Second)
defer cancel()
if err := app.Start(startCtx); err != nil {
panic(err)
}
err := cliApp.RunContext(rootCtx, os.Args)
if err != nil {
log.Error().
Err(err).
Msg("failed to run")
return
}
cancel()
stopCtx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
if err := app.Stop(stopCtx); err != nil {
panic(err)
}
}