/
root.go
117 lines (99 loc) · 3.34 KB
/
root.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
package cmd
import (
"fmt"
stdLog "log"
"github.com/spf13/cobra"
aurora "github.com/shantanu-hashcash/go/services/aurora/internal"
"github.com/shantanu-hashcash/go/support/config"
)
var (
globalConfig, globalFlags = aurora.Flags()
RootCmd = &cobra.Command{
Use: "aurora",
Short: "client-facing api server for the Hcnet network",
SilenceErrors: true,
SilenceUsage: true,
Long: "Client-facing API server for the Hcnet network. It acts as the interface between Hcnet Core " +
"and applications that want to access the Hcnet network. It allows you to submit transactions to the " +
"network, check the status of accounts, subscribe to event streams and more.\n" +
"DEPRECATED - the use of command-line flags has been deprecated in favor of environment variables. Please" +
"consult our Configuring section in the developer documentation on how to use them - https://developers.hcnet.org/docs/run-api-server/configuring",
RunE: func(cmd *cobra.Command, args []string) error {
app, err := aurora.NewAppFromFlags(globalConfig, globalFlags)
if err != nil {
return err
}
return app.Serve()
},
}
originalHelpFunc = RootCmd.HelpFunc()
originalUsageFunc = RootCmd.UsageFunc()
)
// ErrUsage indicates we should print the usage string and exit with code 1
type ErrUsage struct {
cmd *cobra.Command
}
func (e ErrUsage) Error() string {
return e.cmd.UsageString()
}
// ErrExitCode Indicates we want to exit with a specific error code without printing an error.
type ErrExitCode int
func (e ErrExitCode) Error() string {
return fmt.Sprintf("exit code: %d", e)
}
func init() {
// override the default help output, apply further filtering on which global flags
// will be shown on the help outout dependent on the command help was issued upon.
RootCmd.SetHelpFunc(func(c *cobra.Command, args []string) {
enableGlobalOptionsInHelp(c, globalFlags)
originalHelpFunc(c, args)
})
RootCmd.SetUsageFunc(func(c *cobra.Command) error {
enableGlobalOptionsInHelp(c, globalFlags)
return originalUsageFunc(c)
})
err := globalFlags.Init(RootCmd)
if err != nil {
stdLog.Fatal(err.Error())
}
}
func Execute() error {
return RootCmd.Execute()
}
func enableGlobalOptionsInHelp(cmd *cobra.Command, cos config.ConfigOptions) {
for _, co := range cos {
if co.Hidden {
// this options was configured statically to be hidden
// Init() has already set that once, leave it as-is.
continue
}
// we don't want' to display global flags in help output
// if no sub-command context given yet, i.e. just '-h' was used
// or there are subcomands required to use.
if cmd.Parent() == nil || cmd.HasAvailableSubCommands() {
co.ToggleHidden(true)
continue
}
if len(co.UsedInCommands) > 0 &&
!contains(co.UsedInCommands, cmd) {
co.ToggleHidden(true)
} else {
co.ToggleHidden(false)
}
}
}
// check if this command or any of it's sub-level parents match
// supportedCommands
func contains(supportedCommands []string, cmd *cobra.Command) bool {
for _, supportedCommand := range supportedCommands {
if supportedCommand == cmd.Name() {
return true
}
}
// don't do inheritance matching on the top most sub-commands.
// they are second level deep, the aurora itself is top level.
if cmd.Parent() != nil && cmd.Parent().Parent() != nil {
return contains(supportedCommands, cmd.Parent())
}
return false
}