This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
create_subnet.go
104 lines (91 loc) · 2.54 KB
/
create_subnet.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
94
95
96
97
98
99
100
101
102
103
104
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package cmd
import (
"context"
"fmt"
"os"
"github.com/ava-labs/subnet-cli/client"
"github.com/ava-labs/subnet-cli/pkg/color"
"github.com/manifoldco/promptui"
"github.com/onsi/ginkgo/v2/formatter"
"github.com/spf13/cobra"
)
func newCreateSubnetCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "subnet",
Short: "Creates a subnet",
Long: `
Creates a subnet based on the configuration.
$ subnet-cli create subnet \
--private-key-path=.insecure.ewoq.key \
--public-uri=http://localhost:52250
`,
RunE: createSubnetFunc,
}
return cmd
}
func createSubnetFunc(cmd *cobra.Command, args []string) error {
cli, info, err := InitClient(publicURI, true)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
sid, _, err := cli.P().CreateSubnet(ctx, info.key, client.WithDryMode(true))
cancel()
if err != nil {
return err
}
info.txFee = uint64(info.feeData.CreateSubnetTxFee)
info.subnetIDType = "EXPECTED SUBNET ID"
info.subnetID = sid
if err := info.CheckBalance(); err != nil {
return err
}
msg := MakeCreateTable(info)
if enablePrompt {
msg = formatter.F("\n{{blue}}{{bold}}Ready to create subnet resources, should we continue?{{/}}\n") + msg
}
fmt.Fprint(formatter.ColorableStdOut, msg)
if enablePrompt {
prompt := promptui.Select{
Label: "\n",
Stdout: os.Stdout,
Items: []string{
formatter.F("{{red}}No, stop it!{{/}}"),
formatter.F("{{green}}Yes, let's create! {{bold}}{{underline}}I agree to pay the fee{{/}}{{green}}!{{/}}"),
},
}
idx, _, err := prompt.Run()
if err != nil {
panic(err)
}
if idx == 0 {
return nil
}
}
println()
println()
println()
ctx, cancel = context.WithTimeout(context.Background(), requestTimeout)
subnetID, took, err := cli.P().CreateSubnet(ctx, info.key)
cancel()
if err != nil {
return err
}
info.subnetIDType = "CREATED SUBNET ID"
info.subnetID = subnetID
color.Outf("{{magenta}}created subnet{{/}} %q {{light-gray}}(took %v){{/}}\n", info.subnetID, took)
color.Outf("({{orange}}subnet must be whitelisted beforehand via{{/}} {{cyan}}{{bold}}--whitelisted-subnets{{/}} {{orange}}flag!{{/}})\n\n")
info.requiredBalance = 0
info.stakeAmount = 0
info.txFee = 0
ctx, cancel = context.WithTimeout(context.Background(), requestTimeout)
info.balance, err = cli.P().Balance(ctx, info.key)
cancel()
if err != nil {
return err
}
fmt.Fprint(formatter.ColorableStdOut, MakeCreateTable(info))
return nil
}