/
app.go
190 lines (156 loc) · 5.48 KB
/
app.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package app
import (
"fmt"
"os"
goversion "github.com/hashicorp/go-version"
"github.com/pkg/errors"
flag "github.com/spf13/pflag"
"go.uber.org/dig"
"github.com/gohornet/hornet/pkg/app"
"github.com/gohornet/hornet/pkg/node"
"github.com/gohornet/hornet/pkg/toolset"
"github.com/iotaledger/hive.go/configuration"
"github.com/iotaledger/hive.go/logger"
)
var (
// Name of the app.
Name = "HORNET"
// Version of the app.
Version = "1.2.1"
)
var (
version = flag.BoolP("version", "v", false, "Prints the HORNET version")
help = flag.BoolP("help", "h", false, "Prints the HORNET help (--full for all parameters)")
helpFull = flag.Bool("full", false, "Prints full HORNET help (only in combination with -h)")
// configs
nodeConfig = configuration.New()
peeringConfig = configuration.New()
profileConfig = configuration.New()
// config file flags
configFilesFlagSet = flag.NewFlagSet("config_files", flag.ContinueOnError)
nodeCfgFilePath = configFilesFlagSet.StringP(CfgConfigFilePathNodeConfig, "c", "config.json", "file path of the config file")
peeringCfgFilePath = configFilesFlagSet.StringP(CfgConfigFilePathPeeringConfig, "n", "peering.json", "file path of the peering config file")
profilesCfgFilePath = configFilesFlagSet.String(CfgConfigFilePathProfilesConfig, "profiles.json", "file path of the profiles config file")
nonHiddenFlag = map[string]struct{}{
"config": {},
"config-dir": {},
"node.profile": {},
"node.disablePlugins": {},
"node.enablePlugins": {},
"peeringConfig": {},
"profilesConfig": {},
"version": {},
"help": {},
}
cfgNames = map[string]struct{}{
"nodeConfig": {},
"peeringConfig": {},
"profileConfig": {},
}
ErrConfigDoesNotExist = errors.New("config does not exist")
)
func init() {
InitPlugin = &node.InitPlugin{
Pluggable: node.Pluggable{
Name: "App",
Params: params,
InitConfigPars: initConfigPars,
Provide: provide,
Configure: configure,
},
Configs: map[string]*configuration.Configuration{
"nodeConfig": nodeConfig,
"peeringConfig": peeringConfig,
"profileConfig": profileConfig,
},
Init: initialize,
}
}
var (
InitPlugin *node.InitPlugin
)
func initialize(params map[string][]*flag.FlagSet, maskedKeys []string) (*node.InitConfig, error) {
if toolset.ShouldHandleTools() {
toolset.HandleTools()
// HandleTools will call os.Exit
}
flag.Usage = func() {
fmt.Fprintf(os.Stderr, `Usage of %s (%s %s):
Run '%s tools' to list all available tools.
Command line flags:
`, os.Args[0], Name, Version, os.Args[0])
flag.PrintDefaults()
}
configFlagSets, err := normalizeFlagSets(params)
if err != nil {
return nil, err
}
var flagSetsToParse = configFlagSets
flagSetsToParse["config_files"] = configFilesFlagSet
parseFlags(flagSetsToParse)
printVersion(flagSetsToParse)
if err = loadCfg(configFlagSets); err != nil {
return nil, err
}
if err = nodeConfig.SetDefault(logger.ConfigurationKeyDisableCaller, true); err != nil {
panic(err)
}
if err = logger.InitGlobalLogger(nodeConfig); err != nil {
panic(err)
}
versionString := Version
if _, err := goversion.NewSemver(Version); err == nil {
// version is a valid SemVer => release version
versionString = " v" + versionString
} else {
// version is not a valid SemVer => maybe self-compiled
versionString = "commit: " + versionString
}
fmt.Printf(`
██╗ ██╗ ██████╗ ██████╗ ███╗ ██╗███████╗████████╗
██║ ██║██╔═══██╗██╔══██╗████╗ ██║██╔════╝╚══██╔══╝
███████║██║ ██║██████╔╝██╔██╗ ██║█████╗ ██║
██╔══██║██║ ██║██╔══██╗██║╚██╗██║██╔══╝ ██║
██║ ██║╚██████╔╝██║ ██║██║ ╚████║███████╗ ██║
╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═══╝╚══════╝ ╚═╝
%s
`+"\n", versionString)
printConfig(maskedKeys)
return &node.InitConfig{
EnabledPlugins: nodeConfig.Strings(CfgNodeEnablePlugins),
DisabledPlugins: nodeConfig.Strings(CfgNodeDisablePlugins),
}, nil
}
func initConfigPars(c *dig.Container) {
type cfgResult struct {
dig.Out
NodeConfig *configuration.Configuration `name:"nodeConfig"`
PeeringConfig *configuration.Configuration `name:"peeringConfig"`
ProfileConfig *configuration.Configuration `name:"profilesConfig"`
PeeringConfigFilePath string `name:"peeringConfigFilePath"`
}
if err := c.Provide(func() cfgResult {
return cfgResult{
NodeConfig: nodeConfig,
PeeringConfig: peeringConfig,
ProfileConfig: profileConfig,
PeeringConfigFilePath: *peeringCfgFilePath,
}
}); err != nil {
InitPlugin.LogPanic(err)
}
}
func provide(c *dig.Container) {
if err := c.Provide(func() *app.AppInfo {
return &app.AppInfo{
Name: Name,
Version: Version,
LatestGitHubVersion: "",
}
}); err != nil {
InitPlugin.LogPanic(err)
}
}
func configure() {
InitPlugin.LogInfo("Loading plugins ...")
}