github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/names/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 namesPkg
     6  
     7  import (
     8  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
     9  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config"
    10  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/rpc"
    11  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/validate"
    12  )
    13  
    14  func (opts *NamesOptions) validateNames() error {
    15  	chain := opts.Globals.Chain
    16  
    17  	opts.testLog()
    18  
    19  	if opts.BadFlag != nil {
    20  		return opts.BadFlag
    21  	}
    22  
    23  	if !config.IsChainConfigured(chain) {
    24  		return validate.Usage("chain {0} is not properly configured.", chain)
    25  	}
    26  
    27  	if len(opts.Terms) != 1 {
    28  		if opts.Delete {
    29  			return validate.Usage("The {0} option requires exactly one address.", "--delete")
    30  		} else if opts.Undelete {
    31  			return validate.Usage("The {0} option requires exactly one address.", "--undelete")
    32  		} else if opts.Remove {
    33  			return validate.Usage("The {0} option requires exactly one address.", "--remove")
    34  		}
    35  	}
    36  
    37  	isDryRunnable := opts.Clean || len(opts.Autoname) > 0
    38  	if opts.DryRun && !isDryRunnable {
    39  		return validate.Usage("The {0} option is only available with the {1} options.", "--dry_run", "--clean or --autoname")
    40  	}
    41  
    42  	if opts.Tags {
    43  		if opts.Addr {
    44  			return validate.Usage("The {0} option is not available{1}.", "--tags", " with the --addr option")
    45  		}
    46  		if opts.Clean {
    47  			return validate.Usage("The {0} option is not available{1}.", "--tags", " with the --clean option")
    48  		}
    49  		if opts.anyCrud() {
    50  			return validate.Usage("The {0} option is not available{1}.", "--tags", " with the any CRUD option")
    51  		}
    52  	}
    53  
    54  	if opts.Addr {
    55  		if opts.Tags {
    56  			return validate.Usage("The {0} option is not available{1}.", "--addr", " with the --tags option")
    57  		}
    58  		if opts.Clean {
    59  			return validate.Usage("The {0} option is not available{1}.", "--addr", " with the --clean option")
    60  		}
    61  		if opts.anyCrud() {
    62  			return validate.Usage("The {0} option is not available{1}.", "--addr", " with the any CRUD option")
    63  		}
    64  	}
    65  
    66  	if opts.MatchCase && len(opts.Terms) == 0 {
    67  		return validate.Usage("The {0} option requires at least one {1}.", "--match_case", "term")
    68  	}
    69  
    70  	if opts.Prefund {
    71  		if opts.Clean || len(opts.Autoname) > 0 || opts.anyCrud() {
    72  			return validate.Usage("You may not use the {0} option when editing names.", "--prefund")
    73  		}
    74  	}
    75  
    76  	if len(opts.Autoname) > 0 {
    77  		if opts.Regular {
    78  			return validate.Usage("The {0} option is not available{1}.", "--regular", " with the --autoname option")
    79  		}
    80  
    81  		if !base.IsValidAddress(opts.Autoname) || opts.AutonameAddr.IsZero() {
    82  			return validate.Usage("You must provide an address to the {0} option.", "--autoname")
    83  		}
    84  		if err := opts.Conn.IsContractAtLatest(opts.AutonameAddr); err != nil {
    85  			if err == rpc.ErrNotAContract {
    86  				return validate.Usage("The address provided to the {0} option is not a contract.", "--autoname")
    87  			}
    88  			// ignore this error... we'll catch it later
    89  		}
    90  	}
    91  
    92  	return opts.Globals.Validate()
    93  }