github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/init/validate.go (about)

     1  // Copyright 2021 The TrueBlocks Authors. All rights reserved.
     2  // Use of this source code is governed by a license that can
     3  // be found in the LICENSE file.
     4  
     5  package initPkg
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  	"path/filepath"
    11  	"strings"
    12  
    13  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config"
    14  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/file"
    15  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/history"
    16  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/usage"
    17  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/validate"
    18  )
    19  
    20  func (opts *InitOptions) validateInit() error {
    21  	chain := opts.Globals.Chain
    22  
    23  	opts.testLog()
    24  
    25  	if opts.BadFlag != nil {
    26  		return opts.BadFlag
    27  	}
    28  
    29  	if !config.IsChainConfigured(chain) {
    30  		return validate.Usage("chain {0} is not properly configured.", chain)
    31  	}
    32  
    33  	if opts.Globals.TestMode {
    34  		return validate.Usage("integration testing was skipped for chifra init")
    35  	}
    36  
    37  	if len(opts.Publisher) > 0 {
    38  		err := validate.ValidateExactlyOneAddr([]string{opts.Publisher})
    39  		if err != nil {
    40  			return err
    41  		}
    42  	}
    43  
    44  	if len(opts.Example) > 0 {
    45  		cwd, _ := os.Getwd()
    46  		if !strings.HasSuffix(cwd, "examples") {
    47  			return fmt.Errorf("must be in the ./examples directory to run this command")
    48  		}
    49  
    50  		if valid, err := file.IsValidFolderName(opts.Example); err != nil {
    51  			return usage.Usage("{0} {1}", opts.Example, fmt.Sprintf("%v", err))
    52  		} else if !valid {
    53  			return usage.Usage("{0} is not a valid folder name", opts.Example)
    54  		}
    55  		// if len(opts.Template) > 0 {
    56  		// must exist
    57  		// }
    58  		// } else if len(opts.Template) > 0 {
    59  		// 	return validate.Usage("The {0} option requires the {1} flag.", "--template", "--example")
    60  	} else {
    61  		historyFile := filepath.Join(config.PathToCache(chain), "tmp/history.txt")
    62  		if history.FromHistoryBool(historyFile, "init") && !opts.All {
    63  			msg := `You previously called chifra init with the --all option.
    64  			
    65  You must continue to do so or remove the history file here:
    66  			
    67  	{{.HistoryFile}}
    68  
    69  This is a dangerous operation, please don't say we didn't warn you.
    70  `
    71  			msg = strings.ReplaceAll(msg, "{{.HistoryFile}}", historyFile)
    72  			return validate.Usage(msg)
    73  		}
    74  	}
    75  
    76  	return opts.Globals.Validate()
    77  }