github.com/supabase/cli@v1.168.1/internal/utils/enum.go (about)

     1  package utils
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/go-errors/errors"
     8  )
     9  
    10  // Ref: https://github.com/spf13/pflag/issues/236#issuecomment-931600452
    11  type EnumFlag struct {
    12  	Allowed []string
    13  	Value   string
    14  }
    15  
    16  func (a EnumFlag) String() string {
    17  	return a.Value
    18  }
    19  
    20  func (a *EnumFlag) Set(p string) error {
    21  	isIncluded := func(opts []string, val string) bool {
    22  		for _, opt := range opts {
    23  			if val == opt {
    24  				return true
    25  			}
    26  		}
    27  		return false
    28  	}
    29  	if !isIncluded(a.Allowed, p) {
    30  		return errors.Errorf("must be one of [ %s ]", strings.Join(a.Allowed, " | "))
    31  	}
    32  	a.Value = p
    33  	return nil
    34  }
    35  
    36  func (a *EnumFlag) Type() string {
    37  	values := strings.Join(a.Allowed, " | ")
    38  	if len(values) < 40 {
    39  		return fmt.Sprintf("[ %s ]", values)
    40  	}
    41  	return "string"
    42  }