|
1 | 1 | package network |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "fmt" |
5 | 6 |
|
6 | 7 | "github.com/spf13/cobra" |
| 8 | + flag "github.com/spf13/pflag" |
| 9 | + |
| 10 | + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/config" |
7 | 11 |
|
8 | 12 | cliConfig "github.com/oasisprotocol/cli/config" |
| 13 | + "github.com/oasisprotocol/oasis-sdk/client-sdk/go/connection" |
| 14 | +) |
| 15 | + |
| 16 | +var ( |
| 17 | + chainContext string |
| 18 | + |
| 19 | + setChainContextCmd = &cobra.Command{ |
| 20 | + Use: "set-chain-context <name>", |
| 21 | + Short: "Sets the chain context of the given network", |
| 22 | + Args: cobra.ExactArgs(1), |
| 23 | + Run: func(cmd *cobra.Command, args []string) { |
| 24 | + cfg := cliConfig.Global() |
| 25 | + name := args[0] |
| 26 | + |
| 27 | + net := cfg.Networks.All[name] |
| 28 | + if net == nil { |
| 29 | + cobra.CheckErr(fmt.Errorf("network '%s' does not exist", name)) |
| 30 | + return // To make staticcheck happy as it doesn't know CheckErr exits. |
| 31 | + } |
| 32 | + |
| 33 | + if chainContext != "" { |
| 34 | + net.ChainContext = chainContext |
| 35 | + } else { |
| 36 | + // Connect to the network and query the chain context. |
| 37 | + network := config.Network{ |
| 38 | + RPC: net.RPC, |
| 39 | + } |
| 40 | + ctx := context.Background() |
| 41 | + conn, err := connection.ConnectNoVerify(ctx, &network) |
| 42 | + cobra.CheckErr(err) |
| 43 | + chainCtx, err := conn.Consensus().GetChainContext(ctx) |
| 44 | + cobra.CheckErr(err) |
| 45 | + net.ChainContext = chainCtx |
| 46 | + cobra.CheckErr(net.Validate()) |
| 47 | + } |
| 48 | + |
| 49 | + err := cfg.Save() |
| 50 | + cobra.CheckErr(err) |
| 51 | + }, |
| 52 | + } |
9 | 53 | ) |
10 | 54 |
|
11 | | -var setChainContextCmd = &cobra.Command{ |
12 | | - Use: "set-chain-context <name> <chain-context>", |
13 | | - Short: "Sets the chain context of the given network", |
14 | | - Args: cobra.ExactArgs(2), |
15 | | - Run: func(cmd *cobra.Command, args []string) { |
16 | | - cfg := cliConfig.Global() |
17 | | - name, chainContext := args[0], args[1] |
18 | | - |
19 | | - net := cfg.Networks.All[name] |
20 | | - if net == nil { |
21 | | - cobra.CheckErr(fmt.Errorf("network '%s' does not exist", name)) |
22 | | - return // To make staticcheck happy as it doesn't know CheckErr exits. |
23 | | - } |
24 | | - |
25 | | - net.ChainContext = chainContext |
26 | | - |
27 | | - err := cfg.Save() |
28 | | - cobra.CheckErr(err) |
29 | | - }, |
| 55 | +func init() { |
| 56 | + chainContextFlag := flag.NewFlagSet("", flag.ContinueOnError) |
| 57 | + chainContextFlag.StringVar(&chainContext, "chain-context", "", "set chain-context") |
| 58 | + setChainContextCmd.Flags().AddFlagSet(chainContextFlag) |
30 | 59 | } |
0 commit comments