github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/when/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 whenPkg
     6  
     7  import (
     8  	"errors"
     9  
    10  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
    11  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config"
    12  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/validate"
    13  )
    14  
    15  func (opts *WhenOptions) validateWhen() error {
    16  	chain := opts.Globals.Chain
    17  
    18  	opts.testLog()
    19  
    20  	if opts.BadFlag != nil {
    21  		return opts.BadFlag
    22  	}
    23  
    24  	if !config.IsChainConfigured(chain) {
    25  		return validate.Usage("chain {0} is not properly configured.", chain)
    26  	}
    27  
    28  	if opts.Count {
    29  		opts.Timestamps = true
    30  	}
    31  
    32  	if opts.Timestamps {
    33  		if opts.List {
    34  			return validate.Usage("Please choose only one of {0}.", "--timestamps or --list")
    35  		}
    36  
    37  		if opts.Deep && !opts.Check {
    38  			return validate.Usage("The {0} option is only available with the {1} option.", "--deep", "--timestamps --check")
    39  		}
    40  
    41  		if opts.Repair && len(opts.Blocks) == 0 {
    42  			return validate.Usage("The {0} option requires at least one block identifier.", "--repair")
    43  		}
    44  
    45  	} else {
    46  		if opts.Check {
    47  			return validate.Usage("The {0} option is only available with the {1} option.", "--check", "--timestamps")
    48  
    49  		}
    50  
    51  		if opts.Deep {
    52  			return validate.Usage("The {0} option is only available with the {1} option.", "--deep", "--timestamps --check")
    53  		}
    54  
    55  		if opts.Update {
    56  			return validate.Usage("The {0} option is only available with the {1} option.", "--update", "--timestamps")
    57  		}
    58  
    59  		if opts.Truncate != base.NOPOSN {
    60  			return validate.Usage("The {0} option is only available with the {1} option.", "--truncate", "--timestamps")
    61  		}
    62  
    63  		if opts.Repair {
    64  			return validate.Usage("The {0} option is only available with the {1} option.", "--repair", "--timestamps")
    65  		}
    66  	}
    67  
    68  	if len(opts.Blocks) == 0 {
    69  		if !opts.Timestamps {
    70  			opts.List = true
    71  		}
    72  
    73  	} else {
    74  		if opts.List && opts.Timestamps {
    75  			// Cannot have both --list and --timestamps
    76  			return validate.Usage("Please use either {0} or {1}.", "--list", "--timestamps")
    77  
    78  		} else if opts.List {
    79  			// Cannot have both block identifiers and --list
    80  			return validate.Usage("Please supply either {0} or the {1} option.", "block identifiers", "--list")
    81  
    82  		}
    83  	}
    84  
    85  	if opts.Globals.TestMode && opts.Timestamps && !opts.Check && !opts.Count {
    86  		return validate.Usage("--timestamp option not tested in testMode")
    87  	}
    88  
    89  	err := validate.ValidateIdentifiers(
    90  		chain,
    91  		opts.Blocks,
    92  		validate.ValidBlockIdWithRangeAndDate,
    93  		1,
    94  		&opts.BlockIds,
    95  	)
    96  	if err != nil {
    97  		if invalidLiteral, ok := err.(*validate.InvalidIdentifierLiteralError); ok {
    98  			return invalidLiteral
    99  		}
   100  
   101  		if errors.Is(err, validate.ErrTooManyRanges) {
   102  			return validate.Usage("Specify only a single block range at a time.")
   103  		}
   104  
   105  		return err
   106  	}
   107  
   108  	return opts.Globals.Validate()
   109  }