github.com/influxdata/telegraf@v1.30.3/internal/choice/choice.go (about)

     1  // Package choice provides basic functions for working with
     2  // plugin options that must be one of several values.
     3  package choice
     4  
     5  import "fmt"
     6  
     7  // Contains return true if the choice in the list of choices.
     8  func Contains(choice string, choices []string) bool {
     9  	for _, item := range choices {
    10  		if item == choice {
    11  			return true
    12  		}
    13  	}
    14  	return false
    15  }
    16  
    17  // Check returns an error if a choice is not one of
    18  // the available choices.
    19  func Check(choice string, available []string) error {
    20  	if !Contains(choice, available) {
    21  		return fmt.Errorf("unknown choice %s", choice)
    22  	}
    23  	return nil
    24  }
    25  
    26  // CheckSlice returns an error if the choices is not a subset of
    27  // available.
    28  func CheckSlice(choices, available []string) error {
    29  	for _, choice := range choices {
    30  		err := Check(choice, available)
    31  		if err != nil {
    32  			return err
    33  		}
    34  	}
    35  	return nil
    36  }