2023-07-24 20:56:10 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"git.faercol.me/faercol/http-boot-server/bootserver/bootoption"
|
2023-07-29 19:23:36 +00:00
|
|
|
"github.com/google/uuid"
|
2023-07-24 20:56:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var ErrUnknownClient = errors.New("unknown client")
|
|
|
|
var ErrUnselectedBootOption = errors.New("unselected boot option")
|
|
|
|
var ErrUnknownBootOption = errors.New("unknown boot option")
|
|
|
|
|
|
|
|
type ClientHandlerService struct {
|
2023-07-29 19:23:36 +00:00
|
|
|
clients map[uuid.UUID]*bootoption.Client
|
2023-07-24 20:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewClientHandlerService() *ClientHandlerService {
|
|
|
|
return &ClientHandlerService{
|
2023-07-29 19:23:36 +00:00
|
|
|
clients: make(map[uuid.UUID]*bootoption.Client),
|
2023-07-24 20:56:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (chs *ClientHandlerService) AddClient(client *bootoption.Client) {
|
2023-07-29 19:23:36 +00:00
|
|
|
chs.clients[client.ID] = client
|
2023-07-24 20:56:10 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 19:23:36 +00:00
|
|
|
func (chs *ClientHandlerService) GetClientSelectedBootOption(client uuid.UUID) (*bootoption.EFIApp, error) {
|
2023-07-24 20:56:10 +00:00
|
|
|
clientDetails, ok := chs.clients[client]
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrUnknownClient
|
|
|
|
}
|
|
|
|
|
|
|
|
if clientDetails.SelectedOption == "" {
|
|
|
|
return nil, ErrUnselectedBootOption
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range clientDetails.Options {
|
|
|
|
if o.Name == clientDetails.SelectedOption {
|
|
|
|
return &o, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, ErrUnknownBootOption
|
|
|
|
}
|
|
|
|
|
2023-07-29 19:23:36 +00:00
|
|
|
func (chs *ClientHandlerService) SetClientBootOption(client uuid.UUID, option string) error {
|
2023-07-24 20:56:10 +00:00
|
|
|
clientDetails, ok := chs.clients[client]
|
|
|
|
if !ok {
|
|
|
|
return ErrUnknownClient
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range clientDetails.Options {
|
|
|
|
if o.Name == option {
|
|
|
|
clientDetails.SelectedOption = option
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ErrUnknownBootOption
|
|
|
|
}
|