github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/opts/ulimit.go (about)

     1  package opts // import "github.com/demonoid81/moby/opts"
     2  
     3  import (
     4  	"fmt"
     5  
     6  	units "github.com/docker/go-units"
     7  )
     8  
     9  // UlimitOpt defines a map of Ulimits
    10  type UlimitOpt struct {
    11  	values *map[string]*units.Ulimit
    12  }
    13  
    14  // NewUlimitOpt creates a new UlimitOpt
    15  func NewUlimitOpt(ref *map[string]*units.Ulimit) *UlimitOpt {
    16  	if ref == nil {
    17  		ref = &map[string]*units.Ulimit{}
    18  	}
    19  	return &UlimitOpt{ref}
    20  }
    21  
    22  // Set validates a Ulimit and sets its name as a key in UlimitOpt
    23  func (o *UlimitOpt) Set(val string) error {
    24  	l, err := units.ParseUlimit(val)
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	(*o.values)[l.Name] = l
    30  
    31  	return nil
    32  }
    33  
    34  // String returns Ulimit values as a string.
    35  func (o *UlimitOpt) String() string {
    36  	var out []string
    37  	for _, v := range *o.values {
    38  		out = append(out, v.String())
    39  	}
    40  
    41  	return fmt.Sprintf("%v", out)
    42  }
    43  
    44  // GetList returns a slice of pointers to Ulimits.
    45  func (o *UlimitOpt) GetList() []*units.Ulimit {
    46  	var ulimits []*units.Ulimit
    47  	for _, v := range *o.values {
    48  		ulimits = append(ulimits, v)
    49  	}
    50  
    51  	return ulimits
    52  }
    53  
    54  // Type returns the option type
    55  func (o *UlimitOpt) Type() string {
    56  	return "ulimit"
    57  }
    58  
    59  // NamedUlimitOpt defines a named map of Ulimits
    60  type NamedUlimitOpt struct {
    61  	name string
    62  	UlimitOpt
    63  }
    64  
    65  var _ NamedOption = &NamedUlimitOpt{}
    66  
    67  // NewNamedUlimitOpt creates a new NamedUlimitOpt
    68  func NewNamedUlimitOpt(name string, ref *map[string]*units.Ulimit) *NamedUlimitOpt {
    69  	if ref == nil {
    70  		ref = &map[string]*units.Ulimit{}
    71  	}
    72  	return &NamedUlimitOpt{
    73  		name:      name,
    74  		UlimitOpt: *NewUlimitOpt(ref),
    75  	}
    76  }
    77  
    78  // Name returns the option name
    79  func (o *NamedUlimitOpt) Name() string {
    80  	return o.name
    81  }