Skip to content

Commit

Permalink
Merge env and file zone config
Browse files Browse the repository at this point in the history
  • Loading branch information
wyne authored and oz committed Sep 3, 2024
1 parent 88dec44 commit ae15551
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
34 changes: 26 additions & 8 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
**/
package main

import (
"fmt"
"time"
)

// Keymaps represents the key mappings in the TOML file
type Keymaps struct {
PrevHour []string
Expand All @@ -38,7 +43,7 @@ type Config struct {
// Function to provide default values for the Config struct
func NewDefaultConfig() Config {
return Config{
Zones: []*Zone{}, // Default to an empty slice of Zones
Zones: DefaultZones,
Keymaps: Keymaps{
PrevHour: []string{"h", "left"},
NextHour: []string{"l", "right"},
Expand All @@ -54,26 +59,39 @@ func NewDefaultConfig() Config {
}

func LoadConfig(tzConfigs []string) (Config, error) {

// Apply config file first
fileConfig, _ := LoadConfigFile()
fileConfig, fileError := LoadConfigFile()
if fileError != nil {
panic(fileError)
}

// Override with env var config
envConfig, _ := LoadConfigEnv(tzConfigs)

// Merge configs, with envConfig taking precedence
mergedConfig := NewDefaultConfig()

var zones []*Zone

// Setup with Local time zone
localZoneName, _ := time.Now().Zone()
zones = append(zones, &Zone{
Name: fmt.Sprintf("(%s) Local", localZoneName),
DbName: localZoneName,
})

// Merge Zones
if len(envConfig.Zones) > 0 {
mergedConfig.Zones = envConfig.Zones
zones = append(zones, envConfig.Zones...)
} else if len(fileConfig.Zones) > 0 {
mergedConfig.Zones = fileConfig.Zones
zones = append(zones, fileConfig.Zones...)
}

logger.Printf("File config: %s", fileConfig.Zones)
logger.Printf("Env config: %s", envConfig.Zones)
logger.Printf("Merged config: %s", mergedConfig.Zones)
mergedConfig.Zones = zones

logger.Printf("File zones: %s", fileConfig.Zones)
logger.Printf("Env zones: %s", envConfig.Zones)
logger.Printf("Merged zones: %s", mergedConfig.Zones)

// Merge Keymaps
if len(fileConfig.Keymaps.PrevHour) > 0 {
Expand Down
15 changes: 3 additions & 12 deletions config_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ import (

// LoadConfigEnv from environment
func LoadConfigEnv(tzConfigs []string) (*Config, error) {
conf := Config{
Zones: DefaultZones,
}
conf := Config{}

if len(tzConfigs) == 0 {
tzList := os.Getenv("TZ_LIST")
Expand All @@ -39,22 +37,15 @@ func LoadConfigEnv(tzConfigs []string) (*Config, error) {
return &conf, nil
}
}
zones := make([]*Zone, len(tzConfigs)+1)

// Setup with Local time zone
localZoneName, _ := time.Now().Zone()
zones[0] = &Zone{
Name: fmt.Sprintf("(%s) Local", localZoneName),
DbName: localZoneName,
}
zones := make([]*Zone, len(tzConfigs))

// Add zones from TZ_LIST
for i, zoneConf := range tzConfigs {
zone, err := ReadZoneFromString(time.Now(), zoneConf)
if err != nil {
return nil, err
}
zones[i+1] = zone
zones[i] = zone
}
conf.Zones = zones

Expand Down
15 changes: 3 additions & 12 deletions config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ func ReadZonesFromFile(now time.Time, zoneConf ConfigFileZone) (*Zone, error) {
}

func LoadConfigFile() (*Config, error) {
conf := Config{
Zones: DefaultZones,
}
conf := Config{}

// Expand the ~ to the home directory
homeDir, err := os.UserHomeDir()
Expand All @@ -85,22 +83,15 @@ func LoadConfigFile() (*Config, error) {
panic(err)
}

zones := make([]*Zone, len(config.Zones)+1)

// Setup with Local time zone
localZoneName, _ := time.Now().Zone()
zones[0] = &Zone{
Name: fmt.Sprintf("(%s) Local", localZoneName),
DbName: localZoneName,
}
zones := make([]*Zone, len(config.Zones))

// Add zones from config file
for i, zoneConf := range config.Zones {
zone, err := ReadZonesFromFile(time.Now(), zoneConf)
if err != nil {
return nil, err
}
zones[i+1] = zone
zones[i] = zone
}

conf.Zones = zones
Expand Down

0 comments on commit ae15551

Please sign in to comment.