-
Notifications
You must be signed in to change notification settings - Fork 55
/
helpers.go
93 lines (87 loc) · 2.71 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (C) 2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package subnetcmd
import (
"fmt"
"github.com/ava-labs/avalanche-cli/pkg/keychain"
"github.com/ava-labs/avalanche-cli/pkg/models"
"github.com/ava-labs/avalanche-cli/pkg/networkoptions"
"github.com/ava-labs/avalanche-cli/pkg/txutils"
"github.com/ava-labs/avalanchego/ids"
"github.com/spf13/cobra"
)
var globalNetworkFlags networkoptions.NetworkFlags
func CreateSubnetFirst(cmd *cobra.Command, subnetName string, skipPrompt bool) error {
if !app.SubnetConfigExists(subnetName) {
if !skipPrompt {
yes, err := app.Prompt.CaptureNoYes(fmt.Sprintf("Subnet %s is not created yet. Do you want to create it first?", subnetName))
if err != nil {
return err
}
if !yes {
return fmt.Errorf("subnet not available and not being created first")
}
}
return createSubnetConfig(cmd, []string{subnetName})
}
return nil
}
func DeploySubnetFirst(cmd *cobra.Command, subnetName string, skipPrompt bool, supportedNetworkOptions []networkoptions.NetworkOption) error {
var (
doDeploy bool
msg string
errIfNoChoosen error
)
if !app.SubnetConfigExists(subnetName) {
doDeploy = true
msg = fmt.Sprintf("Subnet %s is not created yet. Do you want to create it first?", subnetName)
errIfNoChoosen = fmt.Errorf("subnet not available and not being created first")
} else {
filteredSupportedNetworkOptions, _, _, err := networkoptions.GetSupportedNetworkOptionsForSubnet(app, subnetName, supportedNetworkOptions)
if err != nil {
return err
}
if len(filteredSupportedNetworkOptions) == 0 {
doDeploy = true
msg = fmt.Sprintf("Subnet %s is not deployed yet to a supported network. Do you want to deploy it first?", subnetName)
errIfNoChoosen = fmt.Errorf("subnet not deployed and not being deployed first")
}
}
if doDeploy {
if !skipPrompt {
yes, err := app.Prompt.CaptureNoYes(msg)
if err != nil {
return err
}
if !yes {
return errIfNoChoosen
}
}
return runDeploy(cmd, []string{subnetName}, supportedNetworkOptions)
}
return nil
}
func UpdateKeychainWithSubnetControlKeys(
kc *keychain.Keychain,
network models.Network,
subnetName string,
) error {
sc, err := app.LoadSidecar(subnetName)
if err != nil {
return err
}
subnetID := sc.Networks[network.Name()].SubnetID
if subnetID == ids.Empty {
return errNoSubnetID
}
transferSubnetOwnershipTxID := sc.Networks[network.Name()].TransferSubnetOwnershipTxID
controlKeys, _, err := txutils.GetOwners(network, subnetID, transferSubnetOwnershipTxID)
if err != nil {
return err
}
// add control keys to the keychain whenever possible
if err := kc.AddAddresses(controlKeys); err != nil {
return err
}
return nil
}