Dmitrii Stepanov
46732b61d7
All checks were successful
DCO action / DCO (pull_request) Successful in 24m11s
Build / Build Components (1.21) (pull_request) Successful in 29m53s
Tests and linters / gopls check (pull_request) Successful in 29m20s
Vulncheck / Vulncheck (pull_request) Successful in 29m28s
Build / Build Components (1.22) (pull_request) Successful in 31m21s
Tests and linters / Staticcheck (pull_request) Successful in 35m29s
Pre-commit hooks / Pre-commit (pull_request) Successful in 38m33s
Tests and linters / Lint (pull_request) Successful in 2m29s
Tests and linters / Tests (1.21) (pull_request) Successful in 3m37s
Tests and linters / Tests (1.22) (pull_request) Successful in 13m55s
Tests and linters / Tests with -race (pull_request) Successful in 6m1s
Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
92 lines
2.4 KiB
Go
92 lines
2.4 KiB
Go
package commonflags
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
// Common CLI flag keys, shorthands, default
|
|
// values and their usage descriptions.
|
|
const (
|
|
GenerateKey = "generate-key"
|
|
GenerateKeyShorthand = "g"
|
|
GenerateKeyDefault = false
|
|
GenerateKeyUsage = "Generate new private key"
|
|
|
|
WalletPath = "wallet"
|
|
WalletPathShorthand = "w"
|
|
WalletPathDefault = ""
|
|
WalletPathUsage = "Path to the wallet or binary key"
|
|
|
|
Account = "address"
|
|
AccountShorthand = ""
|
|
AccountDefault = ""
|
|
AccountUsage = "Address of wallet account"
|
|
|
|
RPC = "rpc-endpoint"
|
|
RPCShorthand = "r"
|
|
RPCDefault = ""
|
|
RPCUsage = "Remote node address (as 'multiaddr' or '<host>:<port>')"
|
|
|
|
Timeout = "timeout"
|
|
TimeoutShorthand = "t"
|
|
TimeoutDefault = 15 * time.Second
|
|
TimeoutUsage = "Timeout for an operation"
|
|
|
|
Verbose = "verbose"
|
|
VerboseShorthand = "v"
|
|
VerboseUsage = "Verbose output"
|
|
|
|
ForceFlag = "force"
|
|
ForceFlagShorthand = "f"
|
|
|
|
CIDFlag = "cid"
|
|
CIDFlagUsage = "Container ID."
|
|
|
|
OIDFlag = "oid"
|
|
OIDFlagUsage = "Object ID."
|
|
|
|
TracingFlag = "trace"
|
|
TracingFlagUsage = "Generate trace ID and print it."
|
|
|
|
AwaitFlag = "await"
|
|
AwaitFlagUsage = "Wait for the operation to complete"
|
|
)
|
|
|
|
// Init adds common flags to the command:
|
|
// - GenerateKey,
|
|
// - WalletPath,
|
|
// - Account,
|
|
// - RPC,
|
|
// - Tracing,
|
|
// - Timeout.
|
|
func Init(cmd *cobra.Command) {
|
|
InitWithoutRPC(cmd)
|
|
|
|
ff := cmd.Flags()
|
|
ff.StringP(RPC, RPCShorthand, RPCDefault, RPCUsage)
|
|
ff.Bool(TracingFlag, false, TracingFlagUsage)
|
|
ff.DurationP(Timeout, TimeoutShorthand, TimeoutDefault, TimeoutUsage)
|
|
}
|
|
|
|
// InitWithoutRPC is similar to Init but doesn't create the RPC flag.
|
|
func InitWithoutRPC(cmd *cobra.Command) {
|
|
ff := cmd.Flags()
|
|
|
|
ff.BoolP(GenerateKey, GenerateKeyShorthand, GenerateKeyDefault, GenerateKeyUsage)
|
|
ff.StringP(WalletPath, WalletPathShorthand, WalletPathDefault, WalletPathUsage)
|
|
ff.StringP(Account, AccountShorthand, AccountDefault, AccountUsage)
|
|
}
|
|
|
|
// Bind binds common command flags to the viper.
|
|
func Bind(cmd *cobra.Command) {
|
|
ff := cmd.Flags()
|
|
|
|
_ = viper.BindPFlag(GenerateKey, ff.Lookup(GenerateKey))
|
|
_ = viper.BindPFlag(WalletPath, ff.Lookup(WalletPath))
|
|
_ = viper.BindPFlag(Account, ff.Lookup(Account))
|
|
_ = viper.BindPFlag(RPC, ff.Lookup(RPC))
|
|
_ = viper.BindPFlag(Timeout, ff.Lookup(Timeout))
|
|
}
|