File: labels.go

package info (click to toggle)
golang-github-crc-org-crc 2.34.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,548 kB
  • sloc: sh: 398; makefile: 326; javascript: 40
file content (96 lines) | stat: -rw-r--r-- 2,050 bytes parent folder | download
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package preflight

import (
	"runtime"

	"github.com/crc-org/crc/v2/pkg/crc/network"
)

type LabelName uint32

const (
	Os LabelName = iota
	NetworkMode

	// Keep it last
	// will be used in OS-specific go files to extend LabelName
	lastLabelName // nolint
)

type LabelValue uint32

const (
	// os
	Darwin LabelValue = iota
	Linux
	Windows

	// network mode
	User
	System

	// Keep it last
	// will be used in OS-specific go files to extend LabelValue
	lastLabelValue // nolint
)

var (
	None = labels{}
)

type labels map[LabelName]LabelValue

type preflightFilter map[LabelName]LabelValue

func newFilter() preflightFilter {
	switch runtime.GOOS {
	case "darwin":
		return preflightFilter{Os: Darwin}
	case "linux":
		return preflightFilter{Os: Linux}
	case "windows":
		return preflightFilter{Os: Windows}
	default:
		// In case of different platform (Should not happen)
		return preflightFilter{Os: Linux}
	}
}

func (filter preflightFilter) SetNetworkMode(networkMode network.Mode) {
	switch networkMode {
	case network.SystemNetworkingMode:
		filter[NetworkMode] = System
	case network.UserNetworkingMode:
		filter[NetworkMode] = User
	}
}

/* This will iterate over 'checks' and only keep the checks which match the filter:
 * - if a key is present in the filter and not in the check labels, the check is kept
 * - if a key is present in the check labels, but not in the filter, the check is kept
 * - if a key is present both in the filter and in the check labels, the check
 *   is kept only if they have the same value, it is dropped if their values differ
 */
func (filter preflightFilter) Apply(checks []Check) []Check {
	var filteredChecks []Check

	for _, check := range checks {
		if !skipCheck(check, filter) {
			filteredChecks = append(filteredChecks, check)
		}

	}

	return filteredChecks
}

func skipCheck(check Check, filter preflightFilter) bool {
	for filterKey, filterValue := range filter {
		checkValue, present := check.labels[filterKey]
		if present && checkValue != filterValue {
			return true
		}
	}

	return false
}