Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions cmd/blockchaincmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ var (
icmKeyName string
cchainIcmKeyName string
relayerAllowPrivateIPs bool
overwriteL1 bool

poSMinimumStakeAmount uint64
poSMaximumStakeAmount uint64
Expand Down Expand Up @@ -219,6 +220,8 @@ so you can take your locally tested Blockchain and deploy it on Fuji or Mainnet.

cmd.Flags().BoolVar(&partialSync, "partial-sync", true, "set primary network partial sync for new validators")
cmd.Flags().Uint32Var(&numNodes, "num-nodes", constants.LocalNetworkNumNodes, "number of nodes to be created on local network deploy")
cmd.Flags().BoolVarP(&overwriteL1, "overwrite", "o", false, "Overwrite the current local L1 deploy")
cmd.Flags().BoolVarP(&AsJson, "json", "j", false, "Print the output in JSON format")
return cmd
}

Expand Down Expand Up @@ -397,6 +400,7 @@ func getSubnetEVMMainnetChainID(sc *models.Sidecar, blockchainName string) error

// deployBlockchain is the cobra command run for deploying subnets
func deployBlockchain(cmd *cobra.Command, args []string) error {
ux.Table.SetAsJson(&AsJson)
blockchainName := args[0]

if err := CreateBlockchainFirst(cmd, blockchainName, skipCreatePrompt); err != nil {
Expand Down Expand Up @@ -658,14 +662,16 @@ func deployBlockchain(cmd *cobra.Command, args []string) error {
blockchainName,
network.Name(),
)
yes, err := app.Prompt.CaptureNoYes(
fmt.Sprintf("Do you want to overwrite the current local L1 deploy for %s?", blockchainName),
)
if err != nil {
return err
}
if !yes {
return nil
if !overwriteL1 {
yes, err := app.Prompt.CaptureNoYes(
fmt.Sprintf("Do you want to overwrite the current local L1 deploy for %s?", blockchainName),
)
if err != nil {
return err
}
if !yes {
return nil
}
}
_ = node.DestroyLocalNode(app, clusterName)
}
Expand Down Expand Up @@ -1252,7 +1258,7 @@ func deployBlockchain(cmd *cobra.Command, args []string) error {
ux.Logger.PrintToUser("To deploy a local relayer later on, call `avalanche interchain relayer deploy`")
ux.Logger.PrintToUser("This does not affect L1 operations besides Interchain Messaging")
}

ux.Table.PrintIfJson()
return nil
}

Expand Down
8 changes: 6 additions & 2 deletions cmd/blockchaincmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
)

var printGenesisOnly bool
var AsJson bool

// avalanche blockchain describe
func newDescribeCmd() *cobra.Command {
Expand All @@ -60,6 +61,7 @@ flag, the command instead prints out the raw genesis file.`,
false,
"Print the genesis to the console directly instead of the summary",
)
cmd.Flags().BoolVarP(&AsJson, "json", "j", false, "Print the output in JSON format")
return cmd
}

Expand Down Expand Up @@ -233,7 +235,7 @@ func PrintSubnetInfo(blockchainName string, onlyLocalnetInfo bool) error {

if locallyDeployed {
ux.Logger.PrintToUser("")
if err := localnet.PrintEndpoints(app, ux.Logger.PrintToUser, sc.Name); err != nil {
if err := localnet.PrintEndpoints(app, ux.Logger.PrintToUser, sc.Name, AsJson); err != nil {
return err
}

Expand Down Expand Up @@ -423,7 +425,7 @@ func printPrecompiles(genesis core.Genesis) {
}

func addPrecompileAllowListToTable(
t table.Writer,
t ux.CustomTable,
label string,
adminAddresses []common.Address,
managerAddresses []common.Address,
Expand All @@ -450,6 +452,7 @@ func addPrecompileAllowListToTable(
}

func describe(_ *cobra.Command, args []string) error {
ux.Table.SetAsJson(&AsJson)
blockchainName := args[0]
if !app.GenesisExists(blockchainName) {
ux.Logger.PrintToUser("The provided blockchain name %q does not exist", blockchainName)
Expand All @@ -473,5 +476,6 @@ func describe(_ *cobra.Command, args []string) error {
ux.Logger.PrintToUser("Printing genesis")
return printGenesis(blockchainName)
}
ux.Table.PrintIfJson()
return nil
}
9 changes: 6 additions & 3 deletions cmd/blockchaincmd/upgradecmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ var (
avalanchegoChainConfigFlag = "avalanchego-chain-config-dir"
avalanchegoChainConfigDir string

print bool
print bool
AsJson bool
)

// avalanche blockchain upgrade apply
Expand Down Expand Up @@ -84,11 +85,13 @@ Refer to https://docs.avax.network/nodes/maintain/chain-config-flags#subnet-chai
cmd.Flags().BoolVar(&print, "print", false, "if true, print the manual config without prompting (for public networks only)")
cmd.Flags().BoolVar(&force, "force", false, "If true, don't prompt for confirmation of timestamps in the past")
cmd.Flags().StringVar(&avalanchegoChainConfigDir, avalanchegoChainConfigFlag, os.ExpandEnv(avalanchegoChainConfigDirDefault), "avalanchego's chain config file directory")
cmd.Flags().BoolVarP(&AsJson, "json", "j", false, "Print the output in JSON format")

return cmd
}

func applyCmd(_ *cobra.Command, args []string) error {
ux.Table.SetAsJson(&AsJson)
blockchainName := args[0]

if !app.BlockchainConfigExists(blockchainName) {
Expand All @@ -114,7 +117,7 @@ func applyCmd(_ *cobra.Command, args []string) error {
case mainnetDeployment:
return applyPublicNetworkUpgrade(blockchainName, models.Mainnet.String(), &sc)
}

ux.Table.PrintIfJson()
return nil
}

Expand Down Expand Up @@ -224,7 +227,7 @@ func applyLocalNetworkUpgrade(blockchainName, networkKey string, sc *models.Side
}
ux.Logger.PrintToUser("The next upgrade will go into effect %s", time.Unix(nextUpgrade, 0).Local().Format(constants.TimeParseLayout))
ux.Logger.PrintToUser("")
if err := localnet.PrintEndpoints(app, ux.Logger.PrintToUser, blockchainName); err != nil {
if err := localnet.PrintEndpoints(app, ux.Logger.PrintToUser, blockchainName, AsJson); err != nil {
return err
}

Expand Down
7 changes: 5 additions & 2 deletions cmd/networkcmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type StartFlags struct {
}

var startFlags StartFlags
var AsJson bool

func newStartCmd() *cobra.Command {
cmd := &cobra.Command{
Expand Down Expand Up @@ -72,11 +73,13 @@ already running.`,
constants.LatestPreReleaseVersionTag,
"use this relayer version",
)
cmd.Flags().BoolVarP(&AsJson, "json", "j", false, "Print the output in JSON format")

return cmd
}

func start(*cobra.Command, []string) error {
ux.Table.SetAsJson(&AsJson)
return Start(startFlags, true)
}

Expand Down Expand Up @@ -298,11 +301,11 @@ func Start(flags StartFlags, printEndpoints bool) error {
ux.Logger.PrintToUser("")

if printEndpoints {
if err := localnet.PrintEndpoints(app, ux.Logger.PrintToUser, ""); err != nil {
if err := localnet.PrintEndpoints(app, ux.Logger.PrintToUser, "", AsJson); err != nil {
return err
}
}

ux.Table.PrintIfJson()
return nil
}

Expand Down
9 changes: 6 additions & 3 deletions cmd/networkcmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func newStatusCmd() *cobra.Command {
return &cobra.Command{
cmd := &cobra.Command{
Use: "status",
Short: "Prints the status of the local network",
Long: `The network status command prints whether or not a local Avalanche
Expand All @@ -20,9 +20,12 @@ network is running and some basic stats about the network.`,
RunE: networkStatus,
Args: cobrautils.ExactArgs(0),
}
cmd.Flags().BoolVarP(&AsJson, "json", "j", false, "Print the output in JSON format")
return cmd
}

func networkStatus(*cobra.Command, []string) error {
ux.Table.SetAsJson(&AsJson)
clusterInfo, err := localnet.GetClusterInfo()
if err != nil {
if server.IsServerError(err, server.ErrNotBootstrapped) {
Expand All @@ -38,7 +41,7 @@ func networkStatus(*cobra.Command, []string) error {
ux.Logger.PrintToUser(" Network Healthy: %t", clusterInfo.Healthy)
ux.Logger.PrintToUser(" Custom VMs Healthy: %t", clusterInfo.CustomChainsHealthy)
ux.Logger.PrintToUser("")
if err := localnet.PrintEndpoints(app, ux.Logger.PrintToUser, ""); err != nil {
if err := localnet.PrintEndpoints(app, ux.Logger.PrintToUser, "", AsJson); err != nil {
return err
}
} else {
Expand All @@ -47,6 +50,6 @@ func networkStatus(*cobra.Command, []string) error {

// TODO: verbose output?
// ux.Logger.PrintToUser(status.String())

ux.Table.PrintIfJson()
return nil
}
34 changes: 24 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ var (
)

func NewRootCmd() *cobra.Command {
persistentPreRunE, initLogger := createAppClosure()
// rootCmd represents the base command when called without any subcommands
rootCmd := &cobra.Command{
Use: "avalanche",
Expand All @@ -63,7 +64,7 @@ build and test Blockchain networks.

To get started, look at the documentation for the subcommands or jump right
in with avalanche blockchain create myNewBlockchain.`,
PersistentPreRunE: createApp,
PersistentPreRunE: persistentPreRunE,
Version: Version,
PersistentPostRun: handleTracking,
SilenceErrors: true,
Expand All @@ -80,6 +81,8 @@ in with avalanche blockchain create myNewBlockchain.`,
rootCmd.PersistentFlags().
BoolVar(&skipCheck, constants.SkipUpdateFlag, false, "skip check for new versions")

initLogger()

// add sub commands
rootCmd.AddCommand(blockchaincmd.NewCmd(app))
rootCmd.AddCommand(primarycmd.NewCmd(app))
Expand Down Expand Up @@ -131,15 +134,24 @@ in with avalanche blockchain create myNewBlockchain.`,
return rootCmd
}

func createApp(cmd *cobra.Command, _ []string) error {
baseDir, err := setupEnv()
if err != nil {
return err
}
log, err := setupLogging(baseDir)
if err != nil {
return err
}
// createAppClosure returns createApp as persistentPreRunE for cobra and a function that initializes the logger to be called after rootCmd creation and logLevel assignment but before the call to createApp and sub command creation
func createAppClosure() (func(cmd *cobra.Command, _ []string) error, func()) {
baseDir, errEnv := setupEnv()
// we need to store the logger in this scope to be able to use it in the createApp function
var (log logging.Logger; errLog error)

return func(cmd *cobra.Command, args []string) error {
if errEnv != nil {
return errEnv
}
if errLog != nil {
return errLog
}
return createApp(cmd, log, baseDir)
}, func(){log, errLog = setupLogging(baseDir)}
}

func createApp(cmd *cobra.Command, log logging.Logger, baseDir string) error {
log.Info("-----------")
log.Info(fmt.Sprintf("cmd: %s", strings.Join(os.Args[1:], " ")))
cf := config.New()
Expand Down Expand Up @@ -351,6 +363,8 @@ func setupLogging(baseDir string) (logging.Logger, error) {
}
// create the user facing logger as a global var
ux.NewUserLog(log, os.Stdout)
// create table singleton for the sub commands as it use flag for json output
ux.NewCustomTable("", nil)
return log, nil
}

Expand Down
5 changes: 5 additions & 0 deletions cmd/validatorcmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"github.com/spf13/cobra"
)

var AsJson bool

func NewListCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list [blockchainName]",
Expand All @@ -35,10 +37,12 @@ func NewListCmd() *cobra.Command {
}

networkoptions.AddNetworkFlagsToCmd(cmd, &globalNetworkFlags, true, getBalanceSupportedNetworkOptions)
cmd.Flags().BoolVarP(&AsJson, "json", "j", false, "Print the output in JSON format")
return cmd
}

func list(_ *cobra.Command, args []string) error {
ux.Table.SetAsJson(&AsJson)
blockchainName := args[0]
sc, err := app.LoadSidecar(blockchainName)
if err != nil {
Expand Down Expand Up @@ -119,5 +123,6 @@ func list(_ *cobra.Command, args []string) error {
t.AppendRow(table.Row{nodeID, validationID, validator.Weight, float64(balance) / float64(units.Avax)})
}
fmt.Println(t.Render())
ux.Table.PrintIfJson()
return nil
}
9 changes: 6 additions & 3 deletions pkg/localnet/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,27 @@ func PrintEndpoints(
app *application.Avalanche,
printFunc func(msg string, args ...interface{}),
subnetName string,
AsJson bool,
) error {
clusterInfo, err := GetClusterInfo()
if err != nil {
return err
}
for _, chainInfo := range clusterInfo.CustomChains {
if subnetName == "" || chainInfo.ChainName == subnetName {
if err := PrintSubnetEndpoints(app, printFunc, clusterInfo, chainInfo); err != nil {
if err := PrintSubnetEndpoints(app, printFunc, clusterInfo, chainInfo, AsJson); err != nil {
return err
}
printFunc("")
}
}
if err := PrintNetworkEndpoints("Primary Nodes", printFunc, clusterInfo); err != nil {
if err := PrintNetworkEndpoints("Primary Nodes", printFunc, clusterInfo, AsJson); err != nil {
return err
}
clusterInfo, err = GetClusterInfoWithEndpoint(binutils.LocalClusterGRPCServerEndpoint)
if err == nil {
printFunc("")
if err := PrintNetworkEndpoints("L1 Nodes", printFunc, clusterInfo); err != nil {
if err := PrintNetworkEndpoints("L1 Nodes", printFunc, clusterInfo, AsJson); err != nil {
return err
}
}
Expand All @@ -53,6 +54,7 @@ func PrintSubnetEndpoints(
printFunc func(msg string, args ...interface{}),
clusterInfo *rpcpb.ClusterInfo,
chainInfo *rpcpb.CustomChainInfo,
AsJson bool,
) error {
nodeInfos := maps.Values(clusterInfo.NodeInfos)
nodeUris := utils.Map(nodeInfos, func(nodeInfo *rpcpb.NodeInfo) string { return nodeInfo.GetUri() })
Expand Down Expand Up @@ -94,6 +96,7 @@ func PrintNetworkEndpoints(
title string,
printFunc func(msg string, args ...interface{}),
clusterInfo *rpcpb.ClusterInfo,
AsJson bool,
) error {
header := table.Row{"Name", "Node ID", "Localhost Endpoint"}
insideCodespace := utils.InsideCodespace()
Expand Down
Loading