mirror of
https://github.com/kidoman/embd
synced 2024-12-22 21:00:05 +01:00
- use a Register() method to register describers, instead of directly
setting on a map - allow the host, rev to be overridden by the user
This commit is contained in:
parent
0842517208
commit
6802eb3fa0
@ -20,6 +20,9 @@ type Describer func(rev int) *Descriptor
|
|||||||
// Describers is a global list of registered host Describers.
|
// Describers is a global list of registered host Describers.
|
||||||
var describers = make(map[Host]Describer)
|
var describers = make(map[Host]Describer)
|
||||||
|
|
||||||
|
// Register makes a host describer available by the provided host key.
|
||||||
|
// If Register is called twice with the same host or if describer is nil,
|
||||||
|
// it panics.
|
||||||
func Register(host Host, describer Describer) {
|
func Register(host Host, describer Describer) {
|
||||||
if describer == nil {
|
if describer == nil {
|
||||||
panic("embd: describer is nil")
|
panic("embd: describer is nil")
|
||||||
@ -30,12 +33,33 @@ func Register(host Host, describer Describer) {
|
|||||||
describers[host] = describer
|
describers[host] = describer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var hostOverride Host
|
||||||
|
var hostRevOverride int
|
||||||
|
var hostOverriden bool
|
||||||
|
|
||||||
|
// SetHost overrides the host and revision no.
|
||||||
|
func SetHost(host Host, rev int) {
|
||||||
|
hostOverride = host
|
||||||
|
hostRevOverride = rev
|
||||||
|
|
||||||
|
hostOverriden = true
|
||||||
|
}
|
||||||
|
|
||||||
// DescribeHost returns the detected host descriptor.
|
// DescribeHost returns the detected host descriptor.
|
||||||
|
// Can be overriden by calling SetHost though.
|
||||||
func DescribeHost() (*Descriptor, error) {
|
func DescribeHost() (*Descriptor, error) {
|
||||||
host, rev, err := DetectHost()
|
var host Host
|
||||||
|
var rev int
|
||||||
|
|
||||||
|
if hostOverriden {
|
||||||
|
host, rev = hostOverride, hostRevOverride
|
||||||
|
} else {
|
||||||
|
var err error
|
||||||
|
host, rev, err = DetectHost()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
describer, ok := describers[host]
|
describer, ok := describers[host]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user