dhcu.go 964 Bytes
Newer Older
songlinfeng's avatar
songlinfeng committed
1
2
3
4
5
6
7
/**
# Copyright (c) 2024, HCUOpt CORPORATION.  All rights reserved.
**/

package dhcu

import (
8
9
10
	"dcu-container-toolkit/internal/discover"
	"dcu-container-toolkit/internal/logger"
	"dcu-container-toolkit/pkg/go-c3000lib/pkg/device"
songlinfeng's avatar
songlinfeng committed
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
	"errors"
)

// NewForDevice creates a discoverer for the specified Device.
func NewForDevice(d device.Device, opts ...Option) (discover.Discover, error) {
	o := new(opts...)

	var discoverers []discover.Discover
	var errs error

	c3000smiDiscoverer, err := o.newC3000SimDHCUDiscoverer(d)
	if err != nil {
		errs = errors.Join(errs, err)
	} else if c3000smiDiscoverer != nil {
		discoverers = append(discoverers, c3000smiDiscoverer)
	}

	if len(discoverers) == 0 {
		return nil, errs
	}

	return discover.WithCache(
		discover.FirstValid(
			discoverers...,
		),
	), nil
}

func new(opts ...Option) *options {
	o := &options{}
	for _, opt := range opts {
		opt(o)
	}

	if o.logger == nil {
		o.logger = logger.New()
	}

	return o
}