github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/command/flag/megabytes.go (about)

     1  package flag
     2  
     3  import (
     4  	"strings"
     5  
     6  	"code.cloudfoundry.org/cli/types"
     7  
     8  	"code.cloudfoundry.org/bytefmt"
     9  	flags "github.com/jessevdk/go-flags"
    10  )
    11  
    12  const (
    13  	ALLOWED_UNITS = "mg"
    14  )
    15  
    16  type Megabytes struct {
    17  	types.NullUint64
    18  }
    19  
    20  func (m *Megabytes) UnmarshalFlag(val string) error {
    21  	if val == "" {
    22  		return nil
    23  	}
    24  
    25  	size, err := bytefmt.ToMegabytes(val)
    26  
    27  	if err != nil ||
    28  		!strings.ContainsAny(strings.ToLower(val), ALLOWED_UNITS) ||
    29  		strings.Contains(strings.ToLower(val), ".") {
    30  		return &flags.Error{
    31  			Type:    flags.ErrRequired,
    32  			Message: `Byte quantity must be an integer with a unit of measurement like M, MB, G, or GB`,
    33  		}
    34  	}
    35  
    36  	m.Value = size
    37  	m.IsSet = true
    38  
    39  	return nil
    40  }