first-valid.go 1.3 KB
Newer Older
songlinfeng's avatar
songlinfeng committed
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
/**
# Copyright (c) 2024, HCUOpt CORPORATION.  All rights reserved.
**/

package discover

import "errors"

type firstOf []Discover

// FirstValid returns a discoverer that returns the first non-error result from a list of discoverers.
func FirstValid(discoverers ...Discover) Discover {
	var f firstOf
	for _, d := range discoverers {
		if d == nil {
			continue
		}
		f = append(f, d)
	}
	return f
}

func (f firstOf) Devices() ([]Device, error) {
	var errs error
	for _, d := range f {
		devices, err := d.Devices()
		if err != nil {
			errs = errors.Join(errs, err)
			continue
		}
		return devices, nil
	}
	return nil, errs
}

func (f firstOf) Hooks() ([]Hook, error) {
	var errs error
	for _, d := range f {
		hooks, err := d.Hooks()
		if err != nil {
			errs = errors.Join(errs, err)
			continue
		}
		return hooks, nil
	}
	return nil, errs
}

func (f firstOf) Mounts() ([]Mount, error) {
	var errs error
	for _, d := range f {
		mounts, err := d.Mounts()
		if err != nil {
			errs = errors.Join(errs, err)
			continue
		}
		return mounts, nil
	}
	return nil, nil
}

func (f firstOf) AdditionalGIDs() ([]uint32, error) {
	var errs error
	for _, d := range f {
		additionalGids, err := d.AdditionalGIDs()
		if err != nil {
			errs = errors.Join(errs, err)
			continue
		}
		return additionalGids, nil
	}
	return nil, nil
}