code.vegaprotocol.io/vega@v0.79.0/cmd/blockexplorer/commands/init.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package commands
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  
    22  	"code.vegaprotocol.io/vega/blockexplorer/config"
    23  	"code.vegaprotocol.io/vega/paths"
    24  
    25  	"github.com/jessevdk/go-flags"
    26  )
    27  
    28  type InitCmd struct {
    29  	config.VegaHomeFlag
    30  
    31  	Force bool `description:"Erase exiting blockexplorer configuration at the specified path" long:"force" short:"f"`
    32  }
    33  
    34  func (opts *InitCmd) Execute(_ []string) error {
    35  	paths := paths.New(opts.VegaHome)
    36  	loader, err := config.NewLoader(paths)
    37  	if err != nil {
    38  		return fmt.Errorf("couldn't initialise configuration loader: %w", err)
    39  	}
    40  
    41  	configExists, err := loader.ConfigExists()
    42  	if err != nil {
    43  		return fmt.Errorf("couldn't verify configuration presence: %w", err)
    44  	}
    45  
    46  	if configExists && !opts.Force {
    47  		return fmt.Errorf("configuration already exists at `%s` please remove it first or re-run using -f", loader.ConfigFilePath())
    48  	}
    49  
    50  	config := config.NewDefaultConfig()
    51  	loader.Save(&config)
    52  	fmt.Println("wrote config file: ", loader.ConfigFilePath())
    53  	return nil
    54  }
    55  
    56  var initCmd InitCmd
    57  
    58  func Init(ctx context.Context, parser *flags.Parser) error {
    59  	initCmd = InitCmd{}
    60  
    61  	short := "Create a default config file"
    62  	long := "Generate the minimal configuration required for a block explorer to start"
    63  
    64  	_, err := parser.AddCommand("init", short, long, &initCmd)
    65  	return err
    66  }