/
root.go
110 lines (92 loc) · 2.97 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
package cmd
import (
"fmt"
"io"
"os"
"github.com/elysiumstation/fury/cmd/furywallet/commands/cli"
"github.com/elysiumstation/fury/cmd/furywallet/commands/flags"
"github.com/spf13/cobra"
)
var rootExamples = cli.Examples(`
# Specify a custom Fury home directory
{{.Software}} --home PATH_TO_DIR COMMAND
# Change the output to JSON
{{.Software}} --output json COMMAND
# Disable colors on output using environment variable
NO_COLOR=1 {{.Software}} COMMAND
`)
func NewCmdRoot(w io.Writer) *cobra.Command {
return BuildCmdRoot(w)
}
func BuildCmdRoot(w io.Writer) *cobra.Command {
f := &RootFlags{}
cmd := &cobra.Command{
Use: os.Args[0],
Short: "The Fury wallet",
Long: "The Fury wallet",
Example: rootExamples,
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
// The `__complete` command is being run to build up the auto-completion
// file. We should skip any verification to not temper with the process.
// Any additional printing will end up in the auto-completion registry.
// The `completion` command output the completion script for a given
// shell, that should not be tempered with. We should skip it as well.
if cmd.Name() == "__complete" || cmd.Name() == "completion" {
return nil
}
if err := f.Validate(); err != nil {
return err
}
return nil
},
}
cmd.PersistentFlags().StringVarP(&f.Output,
"output", "o",
flags.InteractiveOutput,
fmt.Sprintf("Specify the output format: %v", flags.AvailableOutputs),
)
cmd.PersistentFlags().StringVar(&f.Home,
"home",
"",
"Specify the location of a custom Fury home",
)
_ = cmd.MarkPersistentFlagDirname("home")
_ = cmd.RegisterFlagCompletionFunc("output", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return flags.AvailableOutputs, cobra.ShellCompDirectiveDefault
})
// Root commands
cmd.AddCommand(NewCmdInit(w, f))
cmd.AddCommand(NewCmdDisclaimer(w, f))
// Sub-commands
cmd.AddCommand(NewCmdAPIToken(w, f))
cmd.AddCommand(NewCmdKey(w, f))
cmd.AddCommand(NewCmdMessage(w, f))
cmd.AddCommand(NewCmdNetwork(w, f))
cmd.AddCommand(NewCmdPassphrase(w, f))
cmd.AddCommand(NewCmdPermissions(w, f))
cmd.AddCommand(NewCmdRawTransaction(w, f))
cmd.AddCommand(NewCmdService(w, f))
cmd.AddCommand(NewCmdSession(w, f))
cmd.AddCommand(NewCmdShell(w, f))
cmd.AddCommand(NewCmdSoftware(w, f))
cmd.AddCommand(NewCmdTransaction(w, f))
// Wallet commands
// We don't have a wrapper sub-command for wallet commands.
cmd.AddCommand(NewCmdCreateWallet(w, f))
cmd.AddCommand(NewCmdDeleteWallet(w, f))
cmd.AddCommand(NewCmdDescribeWallet(w, f))
cmd.AddCommand(NewCmdImportWallet(w, f))
cmd.AddCommand(NewCmdListWallets(w, f))
cmd.AddCommand(NewCmdLocateWallets(w, f))
cmd.AddCommand(NewCmdRenameWallet(w, f))
return cmd
}
type RootFlags struct {
Output string
Home string
}
func (f *RootFlags) Validate() error {
return flags.ValidateOutput(f.Output)
}