github.com/rawahars/moby@v24.0.4+incompatible/opts/address_pools.go (about) 1 package opts 2 3 import ( 4 "encoding/csv" 5 "encoding/json" 6 "fmt" 7 "strconv" 8 "strings" 9 10 types "github.com/docker/docker/libnetwork/ipamutils" 11 ) 12 13 // PoolsOpt is a Value type for parsing the default address pools definitions 14 type PoolsOpt struct { 15 Values []*types.NetworkToSplit 16 } 17 18 // UnmarshalJSON fills values structure info from JSON input 19 func (p *PoolsOpt) UnmarshalJSON(raw []byte) error { 20 return json.Unmarshal(raw, &(p.Values)) 21 } 22 23 // Set predefined pools 24 func (p *PoolsOpt) Set(value string) error { 25 csvReader := csv.NewReader(strings.NewReader(value)) 26 fields, err := csvReader.Read() 27 if err != nil { 28 return err 29 } 30 31 poolsDef := types.NetworkToSplit{} 32 33 for _, field := range fields { 34 // TODO(thaJeztah): this should not be case-insensitive. 35 key, val, ok := strings.Cut(strings.ToLower(field), "=") 36 if !ok { 37 return fmt.Errorf("invalid field '%s' must be a key=value pair", field) 38 } 39 40 switch key { 41 case "base": 42 poolsDef.Base = val 43 case "size": 44 size, err := strconv.Atoi(val) 45 if err != nil { 46 return fmt.Errorf("invalid size value: %q (must be integer): %v", value, err) 47 } 48 poolsDef.Size = size 49 default: 50 return fmt.Errorf("unexpected key '%s' in '%s'", key, field) 51 } 52 } 53 54 p.Values = append(p.Values, &poolsDef) 55 56 return nil 57 } 58 59 // Type returns the type of this option 60 func (p *PoolsOpt) Type() string { 61 return "pool-options" 62 } 63 64 // String returns a string repr of this option 65 func (p *PoolsOpt) String() string { 66 var pools []string 67 for _, pool := range p.Values { 68 repr := fmt.Sprintf("%s %d", pool.Base, pool.Size) 69 pools = append(pools, repr) 70 } 71 return strings.Join(pools, ", ") 72 } 73 74 // Value returns the mounts 75 func (p *PoolsOpt) Value() []*types.NetworkToSplit { 76 return p.Values 77 } 78 79 // Name returns the flag name of this option 80 func (p *PoolsOpt) Name() string { 81 return "default-address-pools" 82 }