mirror of
https://github.com/lorsanstand/HomeOps-Hub.git
synced 2026-06-19 17:45:17 +03:00
refactor: change structure settings to interface
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/lorsanstand/HomeOps-Hub/agent/internal/utils/config_yaml"
|
||||
"github.com/lorsanstand/HomeOps-Hub/agent/internal/utils/settings"
|
||||
"github.com/lorsanstand/HomeOps-Hub/shared/domain"
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
@@ -14,6 +13,11 @@ type Collector interface {
|
||||
GatherInfoSystem() (domain.HostInfo, []domain.Capability)
|
||||
}
|
||||
|
||||
type Settings interface {
|
||||
InsertAgentID(agentID string) error
|
||||
GetAgentID() string
|
||||
}
|
||||
|
||||
type HubConnection interface {
|
||||
RegisterAgent(ctx context.Context, RegisterData domain.RegisterAgentRequest) (domain.RegisterAgentResponse, error)
|
||||
}
|
||||
@@ -24,13 +28,13 @@ type AgentService struct {
|
||||
log zerolog.Logger
|
||||
cfg *config_yaml.AgentConfig
|
||||
heartBeat int
|
||||
settings *settings.Settings
|
||||
settings Settings
|
||||
}
|
||||
|
||||
func NewAgentService(
|
||||
collector Collector,
|
||||
conn HubConnection,
|
||||
settings *settings.Settings,
|
||||
settings Settings,
|
||||
cfg *config_yaml.AgentConfig,
|
||||
logger zerolog.Logger,
|
||||
) *AgentService {
|
||||
@@ -43,7 +47,7 @@ func (a *AgentService) RegisterAgentConn(ctx context.Context) error {
|
||||
a.log.Debug().Msg("getting info by system")
|
||||
info, caps := a.collect.GatherInfoSystem()
|
||||
a.log.Debug().Msg("create request data for register agent")
|
||||
AgentID := a.settings.AgentID
|
||||
AgentID := a.settings.GetAgentID()
|
||||
AgentName := a.cfg.AppName
|
||||
AgentData := domain.RegisterAgentRequest{
|
||||
AgentId: AgentID,
|
||||
@@ -58,9 +62,10 @@ func (a *AgentService) RegisterAgentConn(ctx context.Context) error {
|
||||
return fmt.Errorf("register agent: %w", err)
|
||||
}
|
||||
|
||||
if err = a.settings.Insert(settings.Settings{AgentID: data.AgentID}); err != nil {
|
||||
if err = a.settings.InsertAgentID(data.AgentID); err != nil {
|
||||
return fmt.Errorf("save agent ID: %w", err)
|
||||
}
|
||||
a.log.Info().Str("AgentID", data.AgentID).Msg("agent registration end")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -51,16 +51,22 @@ func ReadSettings(path string) (*Settings, error) {
|
||||
return &settings, nil
|
||||
}
|
||||
|
||||
func (s *Settings) Insert(sett Settings) error {
|
||||
func (s *Settings) InsertAgentID(agentID string) error {
|
||||
file, err := os.OpenFile(s.path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
sett := Settings{AgentID: agentID}
|
||||
|
||||
if err = json.NewEncoder(file).Encode(sett); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Settings) GetAgentID() string {
|
||||
return s.AgentID
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user