github.com/blend/go-sdk@v1.20220411.3/logger/flags.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package logger
     9  
    10  import (
    11  	"strings"
    12  )
    13  
    14  // NewFlags returns a new flag set from an array of flag values.
    15  // It applies some parsing rules, such as a `-` prefix denotes disabling the flag explicitly.
    16  // `All` and `None` are special flag values that indicate all flags are enabled or none are enabled.
    17  // Flags are caseless, and are lowercase in final output.
    18  func NewFlags(flags ...string) *Flags {
    19  	flagSet := &Flags{
    20  		flags: make(map[string]bool),
    21  	}
    22  
    23  	for _, rawFlag := range flags {
    24  		parsedFlag := strings.ToLower(strings.TrimSpace(rawFlag))
    25  		if parsedFlag == FlagAll {
    26  			flagSet.all = true
    27  			continue
    28  		}
    29  
    30  		if parsedFlag == FlagNone {
    31  			flagSet.none = true
    32  			return flagSet
    33  		}
    34  
    35  		if strings.HasPrefix(parsedFlag, "-") {
    36  			flagSet.flags[strings.TrimPrefix(parsedFlag, "-")] = false
    37  		} else {
    38  			flagSet.flags[parsedFlag] = true
    39  		}
    40  	}
    41  
    42  	return flagSet
    43  }
    44  
    45  // FlagsAll returns a flags set with all enabled.
    46  func FlagsAll() *Flags { return &Flags{all: true, flags: make(map[string]bool)} }
    47  
    48  // FlagsNone returns a flags set with no flags enabled.
    49  func FlagsNone() *Flags { return &Flags{none: true, flags: make(map[string]bool)} }
    50  
    51  // Flags is a set of event flags.
    52  type Flags struct {
    53  	flags map[string]bool
    54  	all   bool
    55  	none  bool
    56  }
    57  
    58  // Enable enables an event flag.
    59  func (efs *Flags) Enable(flags ...string) {
    60  	efs.none = false
    61  	for _, flag := range flags {
    62  		efs.flags[strings.ToLower(strings.TrimSpace(flag))] = true
    63  	}
    64  }
    65  
    66  // Disable disables a flag.
    67  func (efs *Flags) Disable(flags ...string) {
    68  	for _, flag := range flags {
    69  		efs.flags[strings.ToLower(strings.TrimSpace(flag))] = false
    70  	}
    71  }
    72  
    73  // SetAll flips the `all` bit on the flag set to true.
    74  // Note: flags that are explicitly disabled will remain disabled.
    75  func (efs *Flags) SetAll() {
    76  	efs.all = true
    77  	efs.none = false
    78  }
    79  
    80  // All returns if the all bit is flipped to true.
    81  func (efs *Flags) All() bool {
    82  	return efs.all
    83  }
    84  
    85  // SetNone flips the `none` bit on the flag set to true.
    86  // It also disables the `all` bit, and empties the enabled flag set.
    87  func (efs *Flags) SetNone() {
    88  	efs.all = false
    89  	efs.flags = make(map[string]bool)
    90  	efs.none = true
    91  }
    92  
    93  // None returns if the none bit is flipped to true.
    94  func (efs *Flags) None() bool {
    95  	return efs.none
    96  }
    97  
    98  // IsEnabled checks to see if an event is enabled.
    99  func (efs Flags) IsEnabled(flag string) bool {
   100  	switch {
   101  	case efs.all:
   102  		if efs.flags != nil {
   103  			if enabled, hasEvent := efs.flags[flag]; hasEvent && !enabled {
   104  				return false
   105  			}
   106  		}
   107  		return true
   108  	case efs.none:
   109  		return false
   110  	case efs.flags != nil:
   111  		if enabled, hasFlag := efs.flags[flag]; hasFlag {
   112  			return enabled
   113  		}
   114  	}
   115  	return false
   116  }
   117  
   118  // String returns a string representation of the flags.
   119  func (efs Flags) String() string {
   120  	return strings.Join(efs.Flags(), ", ")
   121  }
   122  
   123  // Flags returns an array of flags.
   124  func (efs Flags) Flags() []string {
   125  	if efs.none {
   126  		return []string{FlagNone}
   127  	}
   128  
   129  	var flags []string
   130  	if efs.all {
   131  		flags = []string{FlagAll}
   132  	}
   133  	for key, enabled := range efs.flags {
   134  		if key != FlagAll {
   135  			if enabled {
   136  				if !efs.all {
   137  					flags = append(flags, string(key))
   138  				}
   139  			} else {
   140  				flags = append(flags, "-"+string(key))
   141  			}
   142  		}
   143  	}
   144  	return flags
   145  }
   146  
   147  // MergeWith sets the set from another, with the other taking precedence.
   148  func (efs Flags) MergeWith(other *Flags) {
   149  	if other.all {
   150  		efs.all = true
   151  	}
   152  	if other.none {
   153  		efs.none = true
   154  	}
   155  	for key, value := range other.flags {
   156  		efs.flags[key] = value
   157  	}
   158  }