diff --git a/client/cmd.go b/client/cmd.go index 437022695d..8c71c40cf4 100644 --- a/client/cmd.go +++ b/client/cmd.go @@ -99,6 +99,11 @@ func ReadPersistentCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Cont clientCtx = clientCtx.WithHomeDir(homeDir) } + if !clientCtx.Simulate || flagSet.Changed(flags.FlagDryRun) { + dryRun, _ := flagSet.GetBool(flags.FlagDryRun) + clientCtx = clientCtx.WithSimulation(dryRun) + } + if clientCtx.KeyringDir == "" || flagSet.Changed(flags.FlagKeyringDir) { keyringDir, _ := flagSet.GetString(flags.FlagKeyringDir) @@ -191,11 +196,6 @@ func readTxCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Context, err clientCtx = clientCtx.WithGenerateOnly(genOnly) } - if !clientCtx.Simulate || flagSet.Changed(flags.FlagDryRun) { - dryRun, _ := flagSet.GetBool(flags.FlagDryRun) - clientCtx = clientCtx.WithSimulation(dryRun) - } - if !clientCtx.Offline || flagSet.Changed(flags.FlagOffline) { offline, _ := flagSet.GetBool(flags.FlagOffline) clientCtx = clientCtx.WithOffline(offline) diff --git a/client/context.go b/client/context.go index f7f555339c..292f914943 100644 --- a/client/context.go +++ b/client/context.go @@ -27,6 +27,7 @@ type Context struct { InterfaceRegistry codectypes.InterfaceRegistry Input io.Reader Keyring keyring.Keyring + KeyringOptions []keyring.Option Output io.Writer OutputFormat string Height int64 @@ -56,6 +57,12 @@ func (ctx Context) WithKeyring(k keyring.Keyring) Context { return ctx } +// WithKeyringOptions returns a copy of the context with an updated keyring. +func (ctx Context) WithKeyringOptions(opts ...keyring.Option) Context { + ctx.KeyringOptions = opts + return ctx +} + // WithInput returns a copy of the context with an updated input. func (ctx Context) WithInput(r io.Reader) Context { ctx.Input = r @@ -324,9 +331,9 @@ func GetFromFields(kr keyring.Keyring, from string, genOnly bool) (sdk.AccAddres } func newKeyringFromFlags(ctx Context, backend string) (keyring.Keyring, error) { - if ctx.GenerateOnly { - return keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, ctx.KeyringDir, ctx.Input) + if ctx.GenerateOnly || ctx.Simulate { + return keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, ctx.KeyringDir, ctx.Input, ctx.KeyringOptions...) } - return keyring.New(sdk.KeyringServiceName(), backend, ctx.KeyringDir, ctx.Input) + return keyring.New(sdk.KeyringServiceName(), backend, ctx.KeyringDir, ctx.Input, ctx.KeyringOptions...) } diff --git a/client/keys/add.go b/client/keys/add.go index ae937b5a4b..e0344decdf 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -90,21 +90,7 @@ func runAddCmd(cmd *cobra.Command, args []string) error { return err } - var kr keyring.Keyring - - dryRun, _ := cmd.Flags().GetBool(flags.FlagDryRun) - if dryRun { - kr, err = keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, clientCtx.KeyringDir, buf) - } else { - backend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend) - kr, err = keyring.New(sdk.KeyringServiceName(), backend, clientCtx.KeyringDir, buf) - } - - if err != nil { - return err - } - - return RunAddCmd(cmd, args, kr, buf) + return RunAddCmd(cmd, args, clientCtx.Keyring, buf) } /*