This repository has been archived by the owner on Feb 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (62 loc) · 1.93 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
package main
import (
"errors"
"fmt"
"io"
"os"
"github.com/dimboknv/p24-cli/cmd"
log "github.com/go-pkgz/lgr"
"github.com/jessevdk/go-flags"
)
// Opts with all cli commands and flags
// nolint:govet // need to save commands order
type Opts struct {
BalanceCmd cmd.BalanceCmd `command:"balance" description:"Get card balance of specified merchant"`
StatementsCmd cmd.StatementsCmd `command:"statements" description:"Load statements list for specified merchant and export it to a file/stdout"` // nolint
VersionCmd cmd.VersionCmd `command:"version" description:"Show the 'p24' version information"`
Debug bool `long:"debug" description:"Is debug mode?"`
}
var (
version = "dev"
commit = "unknown"
date = "unknown"
)
func main() {
var opts Opts
p := flags.NewParser(&opts, flags.HelpFlag|flags.PassDoubleDash)
p.CommandHandler = func(command flags.Commander, args []string) error {
setupLogging(opts.Debug)
c := command.(cmd.CommonOptionsCommander)
c.SetCommon(cmd.CommonOpts{
Debug: opts.Debug,
BuildInfo: cmd.BuildInfo{
Version: version,
Commit: commit,
Date: date,
},
})
err := c.Execute(args)
if err != nil {
log.Printf("[ERROR] command %q failed with: %+v", p.Active.Name, err)
}
return err
}
if _, err := p.Parse(); err != nil {
// internal flags.Error error like 'option `-o1, --option1' uses the same long name as option `-o2, --option1'
// wouldn't be printed by flags.Default
w, code := os.Stderr, 1
if flagsErr := (&flags.Error{}); errors.As(err, &flagsErr) && flagsErr.Type == flags.ErrHelp {
w, code = os.Stdout, 0
}
_, _ = fmt.Fprintln(w, err)
os.Exit(code)
}
}
func setupLogging(debug bool) {
opts := []log.Option{log.Out(io.Discard), log.Err(io.Discard)}
if debug {
opts = []log.Option{log.Out(os.Stderr), log.Debug, log.CallerFile, log.CallerFunc, log.Msec, log.LevelBraces}
}
log.Setup(opts...)
log.SetupStdLogger(opts...)
}