github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/monitors/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 monitorsPkg
     6  
     7  import (
     8  	"errors"
     9  	"path/filepath"
    10  
    11  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config"
    12  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/file"
    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 *MonitorsOptions) validateMonitors() 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 len(opts.Addrs) > 0 && (opts.List || opts.Count) {
    32  		return validate.Usage("Do not provide addresses with {0} or {1}.", "--list", "--count")
    33  	}
    34  
    35  	if opts.List {
    36  		// All other options are ignored
    37  
    38  	} else {
    39  		// Count dominates if present
    40  		if !opts.Count {
    41  			if opts.Watch {
    42  				if opts.Globals.IsApiMode() {
    43  					return validate.Usage("The {0} options is not available from the API", "--watch")
    44  				}
    45  
    46  				if len(opts.Globals.File) > 0 {
    47  					return validate.Usage("The {0} option is not allowed with the {1} option. Use {2} instead.", "--file", "--watch", "--commands")
    48  				}
    49  
    50  				if len(opts.Commands) == 0 {
    51  					return validate.Usage("The {0} option requires {1}.", "--watch", "a --commands file")
    52  				} else {
    53  					cmdFile, err := filepath.Abs(opts.Commands)
    54  					if err != nil || !file.FileExists(cmdFile) {
    55  						return validate.Usage("The {0} option requires {1} to exist.", "--watch", opts.Commands)
    56  					}
    57  					if file.FileSize(cmdFile) == 0 {
    58  						logger.Fatal(validate.Usage("The file you specified ({0}) was found but contained no commands.", cmdFile).Error())
    59  					}
    60  				}
    61  
    62  				if len(opts.Watchlist) == 0 {
    63  					return validate.Usage("The {0} option requires {1}.", "--watch", "a --watchlist file")
    64  				} else {
    65  					if opts.Watchlist != "existing" {
    66  						watchList, err := filepath.Abs(opts.Watchlist)
    67  						if err != nil || !file.FileExists(watchList) {
    68  							return validate.Usage("The {0} option requires {1} to exist.", "--watch", opts.Watchlist)
    69  						}
    70  						if file.FileSize(watchList) == 0 {
    71  							logger.Fatal(validate.Usage("The file you specified ({0}) was found but contained no addresses.", watchList).Error())
    72  						}
    73  					}
    74  				}
    75  
    76  				if err := index.IsInitialized(chain, config.ExpectedVersion()); err != nil {
    77  					if (errors.Is(err, index.ErrNotInitialized) || errors.Is(err, index.ErrIncorrectHash)) && !opts.Globals.IsApiMode() {
    78  						logger.Fatal(err)
    79  					}
    80  					return err
    81  				}
    82  
    83  				if opts.BatchSize < 1 {
    84  					return validate.Usage("The {0} option must be greater than zero.", "--batch_size")
    85  				}
    86  			} else {
    87  				if opts.BatchSize != 8 {
    88  					return validate.Usage("The {0} option is not available{1}.", "--batch_size", " without --watch")
    89  				} else {
    90  					opts.BatchSize = 0
    91  				}
    92  
    93  				if opts.RunCount > 0 {
    94  					return validate.Usage("The {0} option is not available{1}.", "--run_count", " without --watch")
    95  				}
    96  
    97  				if opts.Sleep != 14 {
    98  					return validate.Usage("The {0} option is not available{1}.", "--sleep", " without --watch")
    99  				}
   100  
   101  				// We validate some of the simpler curd commands here and the rest in HandleCrud
   102  				if opts.Undelete {
   103  					if opts.Delete || opts.Remove {
   104  						return validate.Usage("The --undelete option may not be used with --delete or --remove.")
   105  					}
   106  				}
   107  
   108  				if !opts.Clean && len(opts.Addrs) == 0 {
   109  					return validate.Usage("You must provide at least one Ethereum address for this command.")
   110  				}
   111  
   112  				if !opts.Clean && !opts.Delete && !opts.Undelete && !opts.Remove && !opts.Globals.Decache {
   113  					return validate.Usage("Please provide either --clean or one of the CRUD commands.")
   114  				}
   115  
   116  				if !opts.Globals.IsApiMode() && !opts.Clean {
   117  					if len(opts.Globals.File) > 0 {
   118  						// Do nothing
   119  					} else {
   120  						err := validate.ValidateAtLeastOneNonSentinal(opts.Addrs)
   121  						if err != nil {
   122  							return err
   123  						}
   124  					}
   125  				}
   126  			}
   127  		}
   128  	}
   129  
   130  	return opts.Globals.Validate()
   131  }