diff --git a/cmd/cylinder/root.go b/cmd/cylinder/root.go index 4f1b75790..c84c5f9d3 100644 --- a/cmd/cylinder/root.go +++ b/cmd/cylinder/root.go @@ -2,6 +2,7 @@ package main import ( "encoding/hex" + "fmt" "os" "path" "path/filepath" @@ -15,6 +16,7 @@ import ( "cosmossdk.io/log" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/version" @@ -90,7 +92,7 @@ func createPersistentPreRunE(rootCmd *cobra.Command, ctx *context.Context) func( cmd *cobra.Command, args []string, ) error { - return func(_ *cobra.Command, _ []string) error { + return func(c *cobra.Command, _ []string) error { // create home directory home, err := rootCmd.PersistentFlags().GetString(flags.FlagHome) if err != nil { @@ -142,7 +144,27 @@ func createPersistentPreRunE(rootCmd *cobra.Command, ctx *context.Context) func( } *ctx = *newCtx - return initConfig(ctx, rootCmd) + if err := initConfig(ctx, rootCmd); err != nil { + return err + } + + // set up client context if nodeURI is provided + if ctx.Config.NodeURI != "" { + clientCtx := client.GetClientContextFromCmd(c) + + newClient, err := client.NewClientFromNode(ctx.Config.NodeURI) + if err != nil { + return fmt.Errorf("couldn't get client from nodeURI: %v", err) + } + + clientCtx = clientCtx.WithNodeURI(ctx.Config.NodeURI).WithClient(newClient) + + if err := client.SetCmdClientContext(c, clientCtx); err != nil { + return err + } + } + + return nil } } diff --git a/yoda/main.go b/yoda/main.go index 39110061c..ce3ac15e6 100644 --- a/yoda/main.go +++ b/yoda/main.go @@ -13,6 +13,7 @@ import ( "cosmossdk.io/log" + "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/crypto/keyring" sdk "github.com/cosmos/cosmos-sdk/types" @@ -88,7 +89,8 @@ func Main() { runCmd(ctx), version.NewVersionCommand(), ) - rootCmd.PersistentPreRunE = func(_ *cobra.Command, _ []string) error { + + rootCmd.PersistentPreRunE = func(c *cobra.Command, _ []string) error { home, err := rootCmd.PersistentFlags().GetString(flags.FlagHome) if err != nil { return err @@ -131,8 +133,30 @@ func Main() { if err != nil { return err } - return initConfig(ctx) + + // read config + if err := initConfig(ctx); err != nil { + return err + } + + // set up client context if nodeURI is provided + if cfg.NodeURI != "" { + clientCtx := client.GetClientContextFromCmd(c) + + newClient, err := client.NewClientFromNode(cfg.NodeURI) + if err != nil { + return fmt.Errorf("couldn't get client from nodeURI: %v", err) + } + + clientCtx = clientCtx.WithNodeURI(cfg.NodeURI).WithClient(newClient) + if err := client.SetCmdClientContext(c, clientCtx); err != nil { + return err + } + } + + return nil } + rootCmd.PersistentFlags().String(flags.FlagHome, DefaultYodaHome, "home directory") if err := rootCmd.Execute(); err != nil { fmt.Println(err)