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