-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
126 lines (105 loc) · 3.1 KB
/
main.go
File metadata and controls
126 lines (105 loc) · 3.1 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"context"
"fmt"
"os"
"github.com/ProtoconNet/mitum-currency-extension/v2/cmds"
"github.com/ProtoconNet/mitum2/base"
"github.com/ProtoconNet/mitum2/launch"
"github.com/ProtoconNet/mitum2/util"
"github.com/ProtoconNet/mitum2/util/logging"
"github.com/alecthomas/kong"
"github.com/pkg/errors"
)
var (
Version = "v0.0.1"
BuildTime = "-"
GitBranch = "master"
GitCommit = "-"
version util.Version
)
//revive:disable:nested-structs
type CLI struct { //nolint:govet //...
launch.BaseFlags
Import cmds.ImportCommand `cmd:"" help:"import from block data"`
Init cmds.INITCommand `cmd:"" help:"init node"`
Run cmds.RunCommand `cmd:"" help:"run node"`
Operation cmds.OperationCommand `cmd:"" help:"create operation"`
Network cmds.NetworkCommand `cmd:"" help:"network"`
Key cmds.KeyCommand `cmd:"" help:"key"`
Version struct{} `cmd:"" help:"version"`
}
var flagDefaults = kong.Vars{
"log_out": "stderr",
"log_format": "terminal",
"log_level": "debug",
"log_force_color": "false",
"design_uri": launch.DefaultDesignURI,
"create_account_threshold": "100",
"create_contract_account_threshold": "100",
"safe_threshold": base.SafeThreshold.String(),
}
func main() {
cli := CLI{
Import: cmds.NewImportCommand(),
Init: cmds.NewINITCommand(),
Run: cmds.NewRunCommand(),
Operation: cmds.NewOperationCommand(),
Network: cmds.NewNetworkCommand(),
Key: cmds.NewKeyCommand(),
}
kctx := kong.Parse(&cli, flagDefaults)
if err := checkVersion(); err != nil {
kctx.FatalIfErrorf(err)
}
if kctx.Command() == "version" {
showVersion()
return
}
pctx := context.Background()
pctx = context.WithValue(pctx, launch.VersionContextKey, version)
pctx = context.WithValue(pctx, launch.FlagsContextKey, cli.BaseFlags)
pctx = context.WithValue(pctx, launch.KongContextContextKey, kctx)
pss := launch.DefaultMainPS()
switch i, err := pss.Run(pctx); {
case err != nil:
kctx.FatalIfErrorf(err)
default:
pctx = i
kctx = kong.Parse(&cli, kong.BindTo(pctx, (*context.Context)(nil)), flagDefaults)
}
var log *logging.Logging
if err := util.LoadFromContextOK(pctx, launch.LoggingContextKey, &log); err != nil {
kctx.FatalIfErrorf(err)
}
log.Log().Debug().Interface("flags", os.Args).Msg("flags")
log.Log().Debug().Interface("main_process", pss.Verbose()).Msg("processed")
if err := func() error {
defer log.Log().Debug().Msg("stopped")
return errors.WithStack(kctx.Run(pctx))
}(); err != nil {
log.Log().Error().Err(err).Msg("stopped by error")
kctx.FatalIfErrorf(err)
}
}
func checkVersion() error {
if len(Version) < 1 {
return errors.Errorf("empty version")
}
v, err := util.ParseVersion(Version)
if err != nil {
return err
}
if err := v.IsValid(nil); err != nil {
return err
}
version = v
return nil
}
func showVersion() {
_, _ = fmt.Fprintf(os.Stdout, `version: %s
branch: %s
commit: %s
build: %s
`, version, GitBranch, GitCommit, BuildTime)
}