...

Source file src/edge-infra.dev/pkg/lib/fog/flags.go

Documentation: edge-infra.dev/pkg/lib/fog

     1  package fog
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"strconv"
     7  	"strings"
     8  )
     9  
    10  var levels = map[string]int8{
    11  	"info":  0,
    12  	"debug": 1,
    13  	"error": -1, // -1 signals via options to only log errors
    14  }
    15  
    16  var flagOpts options
    17  
    18  func BindFlags(_ *flag.FlagSet) {
    19  	var lvl lvlFlag
    20  	lvl.setFunc = func(v int8) {
    21  		flagOpts.lvl = v
    22  	}
    23  }
    24  
    25  type lvlFlag struct {
    26  	setFunc func(int8)
    27  	value   string
    28  }
    29  
    30  // assert that our log level flag implements interface for binding to a command
    31  // line flag
    32  var _ flag.Value = &lvlFlag{}
    33  
    34  // Set checks if the flag value matches one of the string representations for a
    35  // logging level. If it is, it returns the corresponding int from the levels map.
    36  // Otherwise the value is converted into an integer. Passing an integer value
    37  // greater than 1 will allow you to use logging levels above debug, even though
    38  // that is the most verbose log level in GCP.
    39  func (f *lvlFlag) Set(v string) error {
    40  	f.value = v
    41  
    42  	lvl, ok := levels[strings.ToLower(v)]
    43  	if ok {
    44  		f.setFunc(lvl)
    45  		return nil
    46  	}
    47  
    48  	intLvl, err := strconv.Atoi(v)
    49  	if err != nil || intLvl < 0 {
    50  		return fmt.Errorf("invalid log level '%s': %w", v, err)
    51  	}
    52  	f.setFunc(int8(toZapLevel(intLvl)))
    53  
    54  	return nil
    55  }
    56  
    57  func (f *lvlFlag) String() string {
    58  	return f.value
    59  }
    60  

View as plain text