github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/list/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 listPkg
     6  
     7  import (
     8  	"errors"
     9  	"fmt"
    10  
    11  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
    12  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config"
    13  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/index"
    14  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger"
    15  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/validate"
    16  )
    17  
    18  func (opts *ListOptions) validateList() error {
    19  	chain := opts.Globals.Chain
    20  
    21  	opts.testLog()
    22  
    23  	if opts.BadFlag != nil {
    24  		return opts.BadFlag
    25  	}
    26  
    27  	if !config.IsChainConfigured(chain) {
    28  		return validate.Usage("chain {0} is not properly configured.", chain)
    29  	}
    30  
    31  	if opts.LastBlock == 0 {
    32  		opts.LastBlock = base.NOPOSN
    33  	}
    34  
    35  	if opts.MaxRecords == 0 {
    36  		opts.MaxRecords = 250
    37  	}
    38  
    39  	if opts.FirstBlock >= opts.LastBlock {
    40  		msg := fmt.Sprintf("first_block (%d) must be strictly earlier than last_block (%d).", opts.FirstBlock, opts.LastBlock)
    41  		return validate.Usage(msg)
    42  	}
    43  
    44  	if opts.LastBlock != base.NOPOSN && !opts.Globals.TestMode {
    45  		latest := opts.Conn.GetLatestBlockNumber()
    46  		if opts.LastBlock > latest {
    47  			msg := fmt.Sprintf("latest block (%d) must be before the chain's latest block (%d).", opts.LastBlock, latest)
    48  			return validate.Usage(msg)
    49  		}
    50  	}
    51  
    52  	if opts.Globals.TestMode && opts.Unripe {
    53  		return validate.Usage("--unripe is disabled for testing.")
    54  	}
    55  
    56  	if opts.Count && opts.MaxRecords != 250 {
    57  		x := fmt.Sprintf("%d", opts.MaxRecords)
    58  		return validate.Usage("The {0} option is not available{1}.", "--count", "with the --max_records-"+x+" option")
    59  	}
    60  
    61  	if opts.NoZero && !opts.Count {
    62  		return validate.Usage("The {0} option is only available with the {1} option.", "--no_zero", "--count")
    63  	}
    64  
    65  	if len(opts.Globals.File) == 0 {
    66  		err := validate.ValidateAtLeastOneNonSentinal(opts.Addrs)
    67  		if err != nil {
    68  			return err
    69  		}
    70  	}
    71  
    72  	if len(opts.Publisher) > 0 {
    73  		err := validate.ValidateExactlyOneAddr([]string{opts.Publisher})
    74  		if err != nil {
    75  			return err
    76  		}
    77  	}
    78  
    79  	if err := index.IsInitialized(chain, config.ExpectedVersion()); err != nil {
    80  		if (errors.Is(err, index.ErrNotInitialized) || errors.Is(err, index.ErrIncorrectHash)) && !opts.Globals.IsApiMode() {
    81  			logger.Fatal(err)
    82  		}
    83  		return err
    84  	}
    85  
    86  	return nil
    87  }