/
root.go
157 lines (136 loc) · 6.19 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
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
package iwallet
import (
"fmt"
"os"
"time"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/iost-official/go-iost/sdk"
)
var cfgFile string
var startTime time.Time
// rootCmd represents the base command when called without any subcommands.
var rootCmd = &cobra.Command{
Use: "iwallet",
Short: "IOST client",
Long: `An IOST RPC client`,
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
startTime = time.Now()
iwalletSDK = sdk.NewIOSTDevSDK()
iwalletSDK.SetChainID(chainID)
iwalletSDK.SetServer(server)
iwalletSDK.SetVerbose(verbose)
iwalletSDK.SetSignAlgo(signAlgo)
iwalletSDK.SetCheckResult(checkResult, checkResultDelay, checkResultMaxRetry)
limit, err := ParseAmountLimit(amountLimit)
if err != nil {
return fmt.Errorf("invalid amount limit %v: %v", amountLimit, err)
}
if !(0 < expiration && expiration <= 90) {
return fmt.Errorf("expiration should be in (0, 90]")
}
iwalletSDK.SetTxInfo(gasLimit, gasRatio, expiration, delaySecond, limit)
iwalletSDK.SetUseLongestChain(useLongestChain)
return nil
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if elapsedTime && (len(cmd.Use) < 4 || cmd.Use[:4] != "help") {
fmt.Println("Executed in", time.Since(startTime))
}
},
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println("\033[31mERROR:\033[m", err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "configuration file (default $HOME/.iwallet.yaml)")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", true, "print verbose information")
rootCmd.PersistentFlags().BoolVarP(&elapsedTime, "elapsed_time", "", false, "print elapsed time")
rootCmd.PersistentFlags().StringVarP(&accountName, "account", "a", "", "which account to use")
rootCmd.PersistentFlags().StringVarP(&accountDir, "account_dir", "", "", "$(account_dir)/.iwallet will be used to save accounts (default $HOME/.iwallet)")
rootCmd.PersistentFlags().StringVarP(&server, "server", "s", "localhost:30002", "set server of this client")
rootCmd.PersistentFlags().BoolVarP(&useLongestChain, "use_longest", "", false, "get info on longest chain")
rootCmd.PersistentFlags().BoolVarP(&checkResult, "check_result", "", true, "check publish/call status after sending to chain")
rootCmd.PersistentFlags().Float32VarP(&checkResultDelay, "check_result_delay", "", 3, "rpc checking will occur at [checkResultDelay] seconds after sending to chain.")
rootCmd.PersistentFlags().Int32VarP(&checkResultMaxRetry, "check_result_max_retry", "", 30, "max times to call grpc to check tx status")
rootCmd.PersistentFlags().StringVarP(&signAlgo, "sign_algo", "", "ed25519", "sign algorithm")
rootCmd.PersistentFlags().StringSliceVarP(&signers, "signers", "", []string{}, "additional signers")
rootCmd.PersistentFlags().Float64VarP(&gasLimit, "gas_limit", "l", 1000000, "gas limit for a transaction")
rootCmd.PersistentFlags().Float64VarP(&gasRatio, "gas_ratio", "p", 1.0, "gas ratio for a transaction")
rootCmd.PersistentFlags().StringVarP(&amountLimit, "amount_limit", "", "*:unlimited", "amount limit for one transaction, eg iost:300.00|ram:2000")
rootCmd.PersistentFlags().Int64VarP(&expiration, "expiration", "e", 90, "expiration time for a transaction in seconds")
rootCmd.PersistentFlags().Uint32VarP(&chainID, "chain_id", "", uint32(1024), "chain id which distinguishes different network")
rootCmd.PersistentFlags().StringVarP(&txTime, "tx_time", "", "", fmt.Sprintf("use the special tx time instead of now, format: %v", time.Now().Format(time.RFC3339)))
rootCmd.PersistentFlags().Uint32VarP(&txTimeDelay, "tx_time_delay", "", 0, "delay the tx time from now")
rootCmd.PersistentFlags().StringVarP(&signPerm, "sign_permission", "", "active", "permission used to sign transactions")
rootCmd.PersistentFlags().StringVarP(&outputTxFile, "output", "o", "", "output json file to save transaction request")
// the following three flags are used for multiple signature
rootCmd.PersistentFlags().StringSliceVarP(&signKeyFiles, "sign_key_files", "", []string{}, "optional private key files used for signing, split by comma")
rootCmd.PersistentFlags().StringSliceVarP(&signatureFiles, "signature_files", "", []string{}, "optional signature files, split by comma")
rootCmd.PersistentFlags().BoolVarP(&asPublisherSign, "as_publisher_sign", "", false, "use signKeyFiles/signatureFiles for publisher sign")
// Cobra also supports local flags, which will only run
// when this action is called directly.
//rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
// initConfig reads config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Search config in home directory with name ".iwallet" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".iwallet")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
var iwalletSDK *sdk.IOSTDevSDK
var (
server string
accountName string
accountDir string
signAlgo string
signers []string
signPerm string
gasLimit float64
gasRatio float64
expiration int64
amountLimit string
delaySecond int64
txTime string
txTimeDelay uint32
outputTxFile string
// Used for multi sig.
signKeyFiles []string
signatureFiles []string
asPublisherSign bool
checkResult bool
checkResultDelay float32
checkResultMaxRetry int32
useLongestChain bool
verbose bool
elapsedTime bool
chainID uint32
)