github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/cli/opts/ulimit.go (about)

     1  package opts
     2  
     3  import (
     4  	"fmt"
     5  	"sort"
     6  
     7  	"github.com/docker/go-units"
     8  )
     9  
    10  // UlimitOpt defines a map of Ulimits
    11  type UlimitOpt struct {
    12  	values *map[string]*units.Ulimit
    13  }
    14  
    15  // NewUlimitOpt creates a new UlimitOpt. Ulimits are not validated.
    16  func NewUlimitOpt(ref *map[string]*units.Ulimit) *UlimitOpt {
    17  	if ref == nil {
    18  		ref = &map[string]*units.Ulimit{}
    19  	}
    20  	return &UlimitOpt{ref}
    21  }
    22  
    23  // Set validates a Ulimit and sets its name as a key in UlimitOpt
    24  func (o *UlimitOpt) Set(val string) error {
    25  	l, err := units.ParseUlimit(val)
    26  	if err != nil {
    27  		return err
    28  	}
    29  
    30  	(*o.values)[l.Name] = l
    31  
    32  	return nil
    33  }
    34  
    35  // String returns Ulimit values as a string. Values are sorted by name.
    36  func (o *UlimitOpt) String() string {
    37  	var out []string
    38  	for _, v := range *o.values {
    39  		out = append(out, v.String())
    40  	}
    41  	sort.Strings(out)
    42  	return fmt.Sprintf("%v", out)
    43  }
    44  
    45  // GetList returns a slice of pointers to Ulimits. Values are sorted by name.
    46  func (o *UlimitOpt) GetList() []*units.Ulimit {
    47  	var ulimits []*units.Ulimit
    48  	for _, v := range *o.values {
    49  		ulimits = append(ulimits, v)
    50  	}
    51  	sort.SliceStable(ulimits, func(i, j int) bool {
    52  		return ulimits[i].Name < ulimits[j].Name
    53  	})
    54  	return ulimits
    55  }
    56  
    57  // Type returns the option type
    58  func (o *UlimitOpt) Type() string {
    59  	return "ulimit"
    60  }