/
tajriba.go
98 lines (77 loc) · 2.12 KB
/
tajriba.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
package cmd
import (
"context"
"os"
"os/signal"
"syscall"
"github.com/empiricaly/empirica/internal/settings"
logger "github.com/empiricaly/empirica/internal/utils/log"
"github.com/empiricaly/tajriba"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func addTajribaCommand(parent *cobra.Command) error {
cmd := &cobra.Command{
Use: "tajriba",
Short: "Run standalone tajriba server",
// Long: ``,
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.NoArgs,
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
if err := settings.InstallVoltaIfNeeded(initContext()); err != nil {
return errors.Wrap(err, "install volta")
}
econf := getConfig()
err := logger.Init(econf.Log)
if err != nil {
return errors.Wrap(err, "init logs")
}
conf := econf.Tajriba
if err := conf.Validate(); err != nil {
log.Fatal().Err(err).Msg("invalid config")
}
ctx, cancel := context.WithCancel(context.Background())
go func() {
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGHUP)
<-s
cancel()
s = make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
<-s
log.Fatal().Msg("Force quit")
}()
t, err := tajriba.Start(ctx, conf, false)
if err != nil {
log.Fatal().Err(err).Msg("failed starting tajriba")
}
log.Info().Msg("tajriba: started")
<-ctx.Done()
// Give the closing a few seconds to cleanup
ctx, cancel = context.WithTimeout(context.Background(), closeMaxCuration)
defer cancel()
t.Close()
return nil
},
}
if len(os.Args) > 1 && os.Args[1] == "tajriba" {
err := tajriba.ConfigFlags(cmd, "tajriba", "tajriba.json")
if err != nil {
return errors.Wrap(err, "define flags")
}
err = viper.BindPFlags(cmd.Flags())
if err != nil {
return errors.Wrap(err, "bind root flags")
}
err = viper.BindPFlags(cmd.PersistentFlags())
if err != nil {
return errors.Wrap(err, "bind root flags")
}
}
parent.AddCommand(cmd)
return nil
}