refactor: change structure project

This commit is contained in:
lorsan
2026-05-03 19:17:55 +03:00
parent e289365ce8
commit c44fedb488
38 changed files with 62 additions and 59 deletions
@@ -0,0 +1,50 @@
package config_yaml
import (
"fmt"
"os"
"github.com/rs/zerolog"
"gopkg.in/yaml.v3"
)
type AgentConfig struct {
AppName string `yaml:"app_name"`
HubConnect struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
} `yaml:"hub"`
LogLevel string `yaml:"log_level"`
SettingsPath string `yaml:"settings_path"`
}
func NewConfig() (*AgentConfig, error) {
yamlFile, err := os.ReadFile("agent.dev.yaml")
if err != nil {
return nil, fmt.Errorf("failed open file: %v", err)
}
var cfg AgentConfig
if err = yaml.Unmarshal(yamlFile, &cfg); err != nil {
return nil, fmt.Errorf("failed read yaml: %v", err)
}
return &cfg, nil
}
func (c *AgentConfig) GetLogLevel() zerolog.Level {
level, err := zerolog.ParseLevel(c.LogLevel)
if err != nil {
return zerolog.InfoLevel
}
return level
}
func (c *AgentConfig) GetMode() string {
return "DEV"
}
func (c *AgentConfig) GetGRPCAddress() string {
return fmt.Sprintf("%v:%v", c.HubConnect.Host, c.HubConnect.Port)
}
@@ -0,0 +1,41 @@
package config_yaml
import (
"testing"
"github.com/rs/zerolog"
)
func TestAgentConfig_GetLogLevel(t *testing.T) {
t.Parallel()
tests := []struct {
name string
cfg AgentConfig
wantLogLevel zerolog.Level
}{
{
name: "success",
cfg: AgentConfig{LogLevel: "DEBUG"},
wantLogLevel: zerolog.DebugLevel,
},
{
name: "failed parse",
cfg: AgentConfig{LogLevel: "TEST"},
wantLogLevel: zerolog.InfoLevel,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
logLevel := tt.cfg.GetLogLevel()
if logLevel != tt.wantLogLevel {
t.Fatalf("expected %v, got: %v", tt.wantLogLevel, logLevel)
}
})
}
}