http-boot-server/bootserver/services/services.go

60 lines
1.4 KiB
Go
Raw Normal View History

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"
)
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
}
func NewClientHandlerService() *ClientHandlerService {
return &ClientHandlerService{
2023-07-29 19:23:36 +00:00
clients: make(map[uuid.UUID]*bootoption.Client),
}
}
func (chs *ClientHandlerService) AddClient(client *bootoption.Client) {
2023-07-29 19:23:36 +00:00
chs.clients[client.ID] = client
}
2023-07-29 19:23:36 +00:00
func (chs *ClientHandlerService) GetClientSelectedBootOption(client uuid.UUID) (*bootoption.EFIApp, error) {
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 {
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
}