-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd/network): Autodetect chain context if none passed
- Loading branch information
Showing
8 changed files
with
59 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,59 @@ | ||
package network | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
flag "github.com/spf13/pflag" | ||
|
||
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/config" | ||
|
||
cliConfig "github.com/oasisprotocol/cli/config" | ||
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection" | ||
) | ||
|
||
var ( | ||
chainContext string | ||
|
||
setChainContextCmd = &cobra.Command{ | ||
Use: "set-chain-context <name>", | ||
Short: "Sets the chain context of the given network", | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cfg := cliConfig.Global() | ||
name := args[0] | ||
|
||
net := cfg.Networks.All[name] | ||
if net == nil { | ||
cobra.CheckErr(fmt.Errorf("network '%s' does not exist", name)) | ||
return // To make staticcheck happy as it doesn't know CheckErr exits. | ||
} | ||
|
||
if chainContext != "" { | ||
net.ChainContext = chainContext | ||
} else { | ||
// Connect to the network and query the chain context. | ||
network := config.Network{ | ||
RPC: net.RPC, | ||
} | ||
ctx := context.Background() | ||
conn, err := connection.ConnectNoVerify(ctx, &network) | ||
cobra.CheckErr(err) | ||
chainCtx, err := conn.Consensus().GetChainContext(ctx) | ||
cobra.CheckErr(err) | ||
net.ChainContext = chainCtx | ||
cobra.CheckErr(net.Validate()) | ||
} | ||
|
||
err := cfg.Save() | ||
cobra.CheckErr(err) | ||
}, | ||
} | ||
) | ||
|
||
var setChainContextCmd = &cobra.Command{ | ||
Use: "set-chain-context <name> <chain-context>", | ||
Short: "Sets the chain context of the given network", | ||
Args: cobra.ExactArgs(2), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cfg := cliConfig.Global() | ||
name, chainContext := args[0], args[1] | ||
|
||
net := cfg.Networks.All[name] | ||
if net == nil { | ||
cobra.CheckErr(fmt.Errorf("network '%s' does not exist", name)) | ||
return // To make staticcheck happy as it doesn't know CheckErr exits. | ||
} | ||
|
||
net.ChainContext = chainContext | ||
|
||
err := cfg.Save() | ||
cobra.CheckErr(err) | ||
}, | ||
func init() { | ||
chainContextFlag := flag.NewFlagSet("", flag.ContinueOnError) | ||
chainContextFlag.StringVar(&chainContext, "chain-context", "", "set chain-context") | ||
setChainContextCmd.Flags().AddFlagSet(chainContextFlag) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
oasis network set-chain-context mainnet_local --chain-context 01234513331133a715c7a150313877dF1d33e77a715c7a150313877dF1d33e77 |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
oasis network set-chain-context mainnet_local |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters