100 lines
1.7 KiB
Go
100 lines
1.7 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
|
|
SentryDSN string
|
|
)
|
|
|
|
func init() {
|
|
if BuildTime == "" {
|
|
BuildTime = "2022-09-06-01-00-00"
|
|
}
|
|
App.Compiled = getBuildTime()
|
|
|
|
if Version == "" {
|
|
Version = "0.0.0+None"
|
|
}
|
|
App.Version = Version
|
|
|
|
if SentryDSN != "" {
|
|
setupSentry()
|
|
}
|
|
}
|
|
|
|
func getBuildTime() time.Time {
|
|
build, err := time.Parse("2006-01-02-15-04-05", 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,
|
|
EnableTracing: true,
|
|
TracesSampleRate: 1.0,
|
|
})
|
|
if err != nil {
|
|
log.Fatalf("sentry.Init: %s", err)
|
|
}
|
|
}
|
|
|
|
func cleanupSentry(*cli.Context) error {
|
|
if SentryDSN != "" {
|
|
sentry.Flush(time.Second * 2)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func PanicWrapper(f func(*cli.Context) error) func(*cli.Context) error {
|
|
return func(c *cli.Context) error {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
sentry.CaptureException(r.(error))
|
|
sentry.Flush(time.Second * 2)
|
|
log.Printf("Panic Captured: %v", r)
|
|
}
|
|
}()
|
|
return f(c)
|
|
}
|
|
}
|