github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/app/utils/sanity/type.go (about)

     1  package sanity
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/spf13/viper"
     8  )
     9  
    10  const (
    11  	FlagDisableSanity = "disable-sanity"
    12  )
    13  
    14  // item: app's flags
    15  type item interface {
    16  	// label: get item's name
    17  	label() string
    18  	// check: whether the userSetting value is equal to expect value
    19  	check() bool
    20  	// verbose: show the readable flag
    21  	verbose() string
    22  }
    23  
    24  type boolItem struct {
    25  	name   string
    26  	expect bool
    27  }
    28  
    29  func (b boolItem) label() string {
    30  	return b.name
    31  }
    32  
    33  func (b boolItem) check() bool {
    34  	return viper.GetBool(b.label()) == b.expect
    35  }
    36  
    37  func (b boolItem) verbose() string {
    38  	return fmt.Sprintf("--%v=%v", b.name, b.expect)
    39  }
    40  
    41  type stringItem struct {
    42  	name   string
    43  	expect string
    44  }
    45  
    46  func (s stringItem) label() string {
    47  	return s.name
    48  }
    49  
    50  func (s stringItem) check() bool {
    51  	return strings.ToLower(viper.GetString(s.label())) == s.expect
    52  }
    53  
    54  func (s stringItem) verbose() string {
    55  	return fmt.Sprintf("--%v=%v", s.name, s.expect)
    56  }
    57  
    58  type funcItem struct {
    59  	name   string
    60  	expect bool
    61  	actual bool
    62  	f      func() bool
    63  }
    64  
    65  func (f funcItem) label() string {
    66  	return f.name
    67  }
    68  
    69  func (f funcItem) check() bool {
    70  	f.actual = f.f()
    71  	return f.actual == f.expect
    72  }
    73  
    74  func (f funcItem) verbose() string {
    75  	return fmt.Sprintf("%v=%v", f.name, f.actual)
    76  }
    77  
    78  type dependentPair struct {
    79  	config       item
    80  	reliedConfig item
    81  }
    82  
    83  func (cp *dependentPair) check() error {
    84  	//if config is true,  then the reliedConfig must be checked as true
    85  	if cp.config.check() &&
    86  		!cp.reliedConfig.check() {
    87  		return fmt.Errorf(" %v must be set explicitly, as %v", cp.reliedConfig.verbose(), cp.config.verbose())
    88  	}
    89  	return nil
    90  }
    91  
    92  // conflictPair: configA and configB are conflict pair
    93  type conflictPair struct {
    94  	configA item
    95  	configB item
    96  	tips    string
    97  }
    98  
    99  // checkConflict: check configA vs configB
   100  // if both configA and configB are got expect values
   101  // then complain it. if there is a custom tips use it.
   102  func (cp *conflictPair) check() error {
   103  	if cp.configA.check() &&
   104  		cp.configB.check() {
   105  		if cp.tips == "" {
   106  			return fmt.Errorf(" %v conflict with %v", cp.configA.verbose(), cp.configB.verbose())
   107  		}
   108  		return fmt.Errorf(cp.tips)
   109  	}
   110  
   111  	return nil
   112  }
   113  
   114  type rangeItem struct {
   115  	enumRange []int
   116  	value     int
   117  	name      string
   118  }
   119  
   120  func (i rangeItem) label() string {
   121  	return i.name
   122  }
   123  
   124  func (i rangeItem) checkRange() error {
   125  	i.value = viper.GetInt(i.label())
   126  
   127  	for _, v := range i.enumRange {
   128  		if v == i.value {
   129  			return nil
   130  		}
   131  	}
   132  
   133  	return fmt.Errorf(" %v", i.verbose())
   134  }
   135  
   136  func (b rangeItem) verbose() string {
   137  	return fmt.Sprintf("--%v=%v not in %v", b.name, b.value, b.enumRange)
   138  }