Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Use default struct when loading config #4

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 32 additions & 39 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,63 +76,56 @@ func replaceEnvVariables(input string) (string, error) {
}

func LoadConfig(path string) (DeploymentConfig, error) {
var config DeploymentConfig
yamlFile, err := os.Open(path)
f, err := os.Open(path)
if err != nil {
return config, fmt.Errorf("error opening config file: %v", err)
return DeploymentConfig{}, fmt.Errorf("error opening config file: %v", err)
}

// close the file when we're done
defer yamlFile.Close()
defer f.Close()

// Read the file content
yamlData, _ := io.ReadAll(yamlFile)

// Unmarshal the YAML into the config struct
err = yaml.Unmarshal(yamlData, &config)
if err != nil {
return config, err
}

if config.App.PortRange.Start == 0 {
config.App.PortRange.Start = 8000
data, _ := io.ReadAll(f)

// Create a default deployment config
c := DeploymentConfig{
App: App{
PortRange: PortRange{
Start: 8000,
End: 9000,
},
},
Caddy: CaddyConfig{
AdminAPI: "http://localhost:2019",
},
HealthCheck: HealthCheck{
TimeoutSeconds: 5,
IntervalSeconds: 5,
MaxRetries: 3,
},
}

if config.App.PortRange.End == 0 {
config.App.PortRange.End = 9000
}

if config.Caddy.AdminAPI == "" {
config.Caddy.AdminAPI = "http://localhost:2019"
}

if config.HealthCheck.TimeoutSeconds == 0 {
config.HealthCheck.TimeoutSeconds = 5
}

if config.HealthCheck.IntervalSeconds == 0 {
config.HealthCheck.IntervalSeconds = 5
}

if config.HealthCheck.MaxRetries == 0 {
config.HealthCheck.MaxRetries = 3
// Override the default config with the config file
err = yaml.Unmarshal(data, &c)
if err != nil {
return c, err
}

for i, rule := range config.Caddy.Rules {
for i, rule := range c.Caddy.Rules {
newTlsValue, err := replaceEnvVariables(rule.Tls)
if err != nil {
return config, err
return c, err
}

config.Caddy.Rules[i].Tls = newTlsValue
c.Caddy.Rules[i].Tls = newTlsValue
}

if config.App.Registry.Username != "" && config.App.Registry.Password != "" {
envValue, exists := os.LookupEnv(config.App.Registry.Password)
if c.App.Registry.Username != "" && c.App.Registry.Password != "" {
envValue, exists := os.LookupEnv(c.App.Registry.Password)
if exists {
config.App.Registry.Password = envValue
c.App.Registry.Password = envValue
}
}

return config, nil
return c, nil
}
Loading