woj-server/cmd/common.go

96 lines
1.5 KiB
Go

package cmd
import (
"github.com/getsentry/sentry-go"
"github.com/urfave/cli/v2"
"log"
"time"
)
var App = &cli.App{
Name: "WOJ",
EnableBashCompletion: true,
Authors: []*cli.Author{
{
Name: "Paul",
Email: "i@0x7f.app",
},
{
Name: "cxy004",
Email: "cxy004@qq.com",
},
{
Name: "wzt",
Email: "w.zhongtao@qq.com",
},
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "path to the config file",
Value: "config.yaml",
EnvVars: []string{"APP_CONFIG"},
},
},
After: cleanupSentry,
}
var (
BuildTime string
Version string
GitCommit string
SentryDSN string
)
func init() {
if BuildTime == "" {
// First Commit
BuildTime = "20220907-153437"
}
App.Compiled = getBuildTime()
if Version == "" {
Version = "0.0.0"
}
App.Version = Version
if GitCommit == "" {
GitCommit = "out-of-tree"
}
if SentryDSN != "" {
setupSentry()
}
}
func getBuildTime() time.Time {
build, err := time.Parse("20060102-150405", BuildTime)
if err != nil {
log.Printf("failed to parse build time: %v", err)
build = time.Now()
}
return build
}
func setupSentry() {
err := sentry.Init(sentry.ClientOptions{
Dsn: SentryDSN,
AttachStacktrace: true,
EnableTracing: false,
IgnoreErrors: []string{},
SendDefaultPII: true,
Release: GitCommit,
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
}
}
func cleanupSentry(*cli.Context) error {
if SentryDSN != "" {
sentry.Flush(time.Second * 2)
}
return nil
}