github.com/MetalBlockchain/metalgo@v1.11.9/main/main.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package main
     5  
     6  import (
     7  	"encoding/json"
     8  	"errors"
     9  	"fmt"
    10  	"os"
    11  
    12  	"github.com/spf13/pflag"
    13  	"golang.org/x/term"
    14  
    15  	"github.com/MetalBlockchain/metalgo/app"
    16  	"github.com/MetalBlockchain/metalgo/config"
    17  	"github.com/MetalBlockchain/metalgo/version"
    18  )
    19  
    20  func main() {
    21  	fs := config.BuildFlagSet()
    22  	v, err := config.BuildViper(fs, os.Args[1:])
    23  
    24  	if errors.Is(err, pflag.ErrHelp) {
    25  		os.Exit(0)
    26  	}
    27  
    28  	if err != nil {
    29  		fmt.Printf("couldn't configure flags: %s\n", err)
    30  		os.Exit(1)
    31  	}
    32  
    33  	if v.GetBool(config.VersionJSONKey) && v.GetBool(config.VersionKey) {
    34  		fmt.Println("can't print both JSON and human readable versions")
    35  		os.Exit(1)
    36  	}
    37  
    38  	if v.GetBool(config.VersionJSONKey) {
    39  		versions := version.GetVersions()
    40  		jsonBytes, err := json.MarshalIndent(versions, "", "  ")
    41  		if err != nil {
    42  			fmt.Printf("couldn't marshal versions: %s\n", err)
    43  			os.Exit(1)
    44  		}
    45  		fmt.Println(string(jsonBytes))
    46  		os.Exit(0)
    47  	}
    48  
    49  	if v.GetBool(config.VersionKey) {
    50  		fmt.Println(version.GetVersions().String())
    51  		os.Exit(0)
    52  	}
    53  
    54  	nodeConfig, err := config.GetNodeConfig(v)
    55  	if err != nil {
    56  		fmt.Printf("couldn't load node config: %s\n", err)
    57  		os.Exit(1)
    58  	}
    59  
    60  	if term.IsTerminal(int(os.Stdout.Fd())) {
    61  		fmt.Println(app.Header)
    62  	}
    63  
    64  	nodeApp, err := app.New(nodeConfig)
    65  	if err != nil {
    66  		fmt.Printf("couldn't start node: %s\n", err)
    67  		os.Exit(1)
    68  	}
    69  
    70  	exitCode := app.Run(nodeApp)
    71  	os.Exit(exitCode)
    72  }