-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (67 loc) · 2.01 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
69
70
71
72
73
74
package main
import (
gozdrofitapi "github.com/butwhoareyou/gozdrofit-api"
"github.com/butwhoareyou/gozdrofit-cli/cmd"
log "github.com/go-pkgz/lgr"
"github.com/umputun/go-flags"
"os"
"os/signal"
"runtime"
"syscall"
)
type Opts struct {
BookCmd cmd.BookCommand `command:"book"`
ZdrofitUrl string `long:"url" env:"ZDROFIT_URL" required:"true" description:"url to zdrofit"`
ZdrofitUsername string `long:"username" env:"ZDROFIT_USERNAME" required:"true" description:"registered zdrofit user name"`
ZdrofitPassword string `long:"password" env:"ZDROFIT_PASSWORD" required:"true" description:"registered zdrofit user password"`
DryRun bool `long:"dry-run" env:"DRY_RUN" description:"dry run mode"`
Debug bool `long:"debug" env:"DEBUG" description:"debug mode"`
}
func main() {
var opts Opts
p := flags.NewParser(&opts, flags.Default)
p.CommandHandler = func(command flags.Commander, args []string) error {
c := command.(cmd.CommonCommander)
c.SetCommon(cmd.CommonOpts{
BaseUrl: opts.ZdrofitUrl,
Username: opts.ZdrofitUsername,
Password: opts.ZdrofitPassword,
DryRun: opts.DryRun,
Debug: opts.Debug,
HttpClient: gozdrofitapi.NewDefaultHttpClient(),
})
err := c.Execute(args)
if err != nil {
log.Printf("[ERROR] failed with %+v", err)
}
return err
}
if _, err := p.Parse(); err != nil {
if flagsErr, ok := err.(*flags.Error); ok && flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
os.Exit(1)
}
}
}
// getDump reads runtime stack and returns as a string
func getDump() string {
maxSize := 5 * 1024 * 1024
stacktrace := make([]byte, maxSize)
length := runtime.Stack(stacktrace, true)
if length > maxSize {
length = maxSize
}
return string(stacktrace[:length])
}
// nolint:gochecknoinits // can't avoid it in this place
func init() {
// catch SIGQUIT and print stack traces
sigChan := make(chan os.Signal)
go func() {
for range sigChan {
log.Printf("[INFO] SIGQUIT detected, dump:\n%s", getDump())
}
}()
signal.Notify(sigChan, syscall.SIGQUIT)
}