code.vegaprotocol.io/vega@v0.79.0/cmd/blockexplorer/commands/common.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  	"fmt"
    20  
    21  	"code.vegaprotocol.io/vega/blockexplorer/config"
    22  	"code.vegaprotocol.io/vega/logging"
    23  	"code.vegaprotocol.io/vega/paths"
    24  
    25  	"github.com/jessevdk/go-flags"
    26  )
    27  
    28  func loadConfig(logger *logging.Logger, vegaHome string) (*config.Config, error) {
    29  	vegaPaths := paths.New(vegaHome)
    30  
    31  	loader, err := config.NewLoader(vegaPaths)
    32  	if err != nil {
    33  		return nil, fmt.Errorf("could not create config loader: %w", err)
    34  	}
    35  
    36  	exists, err := loader.ConfigExists()
    37  	if err != nil {
    38  		return nil, fmt.Errorf("could not check for existence of config file: %w", err)
    39  	}
    40  
    41  	var cfg *config.Config
    42  	if exists {
    43  		cfg, err = loader.Get()
    44  		if err != nil {
    45  			return nil, fmt.Errorf("could not load config: %w", err)
    46  		}
    47  	} else {
    48  		logger.Warn("No config file found; using defaults. Create one with with 'blockexplorer init'")
    49  		defaultCfg := config.NewDefaultConfig()
    50  		cfg = &defaultCfg
    51  	}
    52  
    53  	// Apply any command line overrides
    54  	flags.NewParser(cfg, flags.Default|flags.IgnoreUnknown).Parse()
    55  	return cfg, nil
    56  }