-
Notifications
You must be signed in to change notification settings - Fork 649
/
main.go
124 lines (108 loc) · 3.93 KB
/
main.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package main
import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"github.com/spf13/cobra"
"github.com/ava-labs/avalanchego/tests/fixture/testnet"
"github.com/ava-labs/avalanchego/tests/fixture/testnet/local"
"github.com/ava-labs/avalanchego/version"
)
const cliVersion = "0.0.1"
var (
errAvalancheGoRequired = fmt.Errorf("--avalanchego-path or %s are required", local.AvalancheGoPathEnvName)
errNetworkDirRequired = fmt.Errorf("--network-dir or %s are required", local.NetworkDirEnvName)
)
func main() {
rootCmd := &cobra.Command{
Use: "testnetctl",
Short: "testnetctl commands",
}
versionCmd := &cobra.Command{
Use: "version",
Short: "Print version details",
RunE: func(*cobra.Command, []string) error {
msg := cliVersion
if len(version.GitCommit) > 0 {
msg += ", commit=" + version.GitCommit
}
fmt.Fprintf(os.Stdout, msg+"\n")
return nil
},
}
rootCmd.AddCommand(versionCmd)
var (
rootDir string
execPath string
nodeCount uint8
fundedKeyCount uint8
)
startNetworkCmd := &cobra.Command{
Use: "start-network",
Short: "Start a new local network",
RunE: func(*cobra.Command, []string) error {
if len(execPath) == 0 {
return errAvalancheGoRequired
}
// Root dir will be defaulted on start if not provided
network := &local.LocalNetwork{
LocalConfig: local.LocalConfig{
ExecPath: execPath,
},
}
ctx, cancel := context.WithTimeout(context.Background(), local.DefaultNetworkStartTimeout)
defer cancel()
network, err := local.StartNetwork(ctx, os.Stdout, rootDir, network, int(nodeCount), int(fundedKeyCount))
if err != nil {
return err
}
// Symlink the new network to the 'latest' network to simplify usage
networkRootDir := filepath.Dir(network.Dir)
networkDirName := filepath.Base(network.Dir)
latestSymlinkPath := filepath.Join(networkRootDir, "latest")
if err := os.Remove(latestSymlinkPath); err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}
if err := os.Symlink(networkDirName, latestSymlinkPath); err != nil {
return err
}
fmt.Fprintf(os.Stdout, "\nConfigure testnetctl to target this network by default with one of the following statements:")
fmt.Fprintf(os.Stdout, "\n - source %s\n", network.EnvFilePath())
fmt.Fprintf(os.Stdout, " - %s\n", network.EnvFileContents())
fmt.Fprintf(os.Stdout, " - export %s=%s\n", local.NetworkDirEnvName, latestSymlinkPath)
return nil
},
}
startNetworkCmd.PersistentFlags().StringVar(&rootDir, "root-dir", os.Getenv(local.RootDirEnvName), "The path to the root directory for local networks")
startNetworkCmd.PersistentFlags().StringVar(&execPath, "avalanchego-path", os.Getenv(local.AvalancheGoPathEnvName), "The path to an avalanchego binary")
startNetworkCmd.PersistentFlags().Uint8Var(&nodeCount, "node-count", testnet.DefaultNodeCount, "Number of nodes the network should initially consist of")
startNetworkCmd.PersistentFlags().Uint8Var(&fundedKeyCount, "funded-key-count", testnet.DefaultFundedKeyCount, "Number of funded keys the network should start with")
rootCmd.AddCommand(startNetworkCmd)
var networkDir string
stopNetworkCmd := &cobra.Command{
Use: "stop-network",
Short: "Stop a local network",
RunE: func(*cobra.Command, []string) error {
if len(networkDir) == 0 {
return errNetworkDirRequired
}
if err := local.StopNetwork(networkDir); err != nil {
return err
}
fmt.Fprintf(os.Stdout, "Stopped network configured at: %s\n", networkDir)
return nil
},
}
stopNetworkCmd.PersistentFlags().StringVar(&networkDir, "network-dir", os.Getenv(local.NetworkDirEnvName), "The path to the configuration directory of a local network")
rootCmd.AddCommand(stopNetworkCmd)
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "testnetctl failed: %v\n", err)
os.Exit(1)
}
os.Exit(0)
}