2016-12-23 14:09:12 -05:00
|
|
|
package opts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-09-10 05:51:35 -04:00
|
|
|
"sort"
|
2016-12-23 14:09:12 -05:00
|
|
|
|
|
|
|
"github.com/docker/go-units"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UlimitOpt defines a map of Ulimits
|
|
|
|
type UlimitOpt struct {
|
|
|
|
values *map[string]*units.Ulimit
|
|
|
|
}
|
|
|
|
|
2020-09-10 05:51:35 -04:00
|
|
|
// NewUlimitOpt creates a new UlimitOpt. Ulimits are not validated.
|
2016-12-23 14:09:12 -05:00
|
|
|
func NewUlimitOpt(ref *map[string]*units.Ulimit) *UlimitOpt {
|
|
|
|
if ref == nil {
|
|
|
|
ref = &map[string]*units.Ulimit{}
|
|
|
|
}
|
|
|
|
return &UlimitOpt{ref}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set validates a Ulimit and sets its name as a key in UlimitOpt
|
|
|
|
func (o *UlimitOpt) Set(val string) error {
|
|
|
|
l, err := units.ParseUlimit(val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
(*o.values)[l.Name] = l
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-10 05:51:35 -04:00
|
|
|
// String returns Ulimit values as a string. Values are sorted by name.
|
2016-12-23 14:09:12 -05:00
|
|
|
func (o *UlimitOpt) String() string {
|
|
|
|
var out []string
|
|
|
|
for _, v := range *o.values {
|
|
|
|
out = append(out, v.String())
|
|
|
|
}
|
2020-09-10 05:51:35 -04:00
|
|
|
sort.Strings(out)
|
2016-12-23 14:09:12 -05:00
|
|
|
return fmt.Sprintf("%v", out)
|
|
|
|
}
|
|
|
|
|
2020-09-10 05:51:35 -04:00
|
|
|
// GetList returns a slice of pointers to Ulimits. Values are sorted by name.
|
2016-12-23 14:09:12 -05:00
|
|
|
func (o *UlimitOpt) GetList() []*units.Ulimit {
|
|
|
|
var ulimits []*units.Ulimit
|
|
|
|
for _, v := range *o.values {
|
|
|
|
ulimits = append(ulimits, v)
|
|
|
|
}
|
2020-09-10 05:51:35 -04:00
|
|
|
sort.SliceStable(ulimits, func(i, j int) bool {
|
|
|
|
return ulimits[i].Name < ulimits[j].Name
|
|
|
|
})
|
2016-12-23 14:09:12 -05:00
|
|
|
return ulimits
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the option type
|
|
|
|
func (o *UlimitOpt) Type() string {
|
|
|
|
return "ulimit"
|
|
|
|
}
|