File: parse.go

package info (click to toggle)
golang-github-containers-common 0.64.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 5,932 kB
  • sloc: makefile: 132; sh: 111
file content (54 lines) | stat: -rw-r--r-- 1,196 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package util

import (
	"fmt"
	"strconv"
)

// ParseMTU parses the mtu option
func ParseMTU(mtu string) (int, error) {
	if mtu == "" {
		return 0, nil // default
	}
	m, err := strconv.Atoi(mtu)
	if err != nil {
		return 0, err
	}
	if m < 0 {
		return 0, fmt.Errorf("mtu %d is less than zero", m)
	}
	return m, nil
}

// ParseVlan parses the vlan option
func ParseVlan(vlan string) (int, error) {
	if vlan == "" {
		return 0, nil // default
	}
	v, err := strconv.Atoi(vlan)
	if err != nil {
		return 0, err
	}
	if v < 0 || v > 4094 {
		return 0, fmt.Errorf("vlan ID %d must be between 0 and 4094", v)
	}
	return v, nil
}

// ParseIsolate parses the isolate option
func ParseIsolate(isolate string) (string, error) {
	switch isolate {
	case "":
		return "false", nil
	case "strict":
		return isolate, nil
	default:
		// isolate option accepts "strict" and Rust boolean values "true" or "false"
		optIsolateBool, err := strconv.ParseBool(isolate)
		if err != nil {
			return "", fmt.Errorf("failed to parse isolate option: %w", err)
		}
		// Rust boolean only support "true" or "false" while go can parse 1 and 0 as well so we need to change it
		return strconv.FormatBool(optIsolateBool), nil
	}
}