Skip to content

Commit

Permalink
Merge File and Env configs
Browse files Browse the repository at this point in the history
  • Loading branch information
wyne authored and oz committed Sep 3, 2024
1 parent 783aa48 commit 31f3810
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 17 deletions.
76 changes: 76 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,79 @@ type Config struct {
Zones []*Zone
Keymaps Keymaps
}

// Function to provide default values for the Config struct
func NewDefaultConfig() Config {
return Config{
Zones: []*Zone{}, // Default to an empty slice of Zones
Keymaps: Keymaps{
PrevHour: []string{"h", "left"},
NextHour: []string{"l", "right"},
PrevDay: []string{"k", "up"},
NextDay: []string{"j", "down"},
PrevWeek: []string{"p"},
NextWeek: []string{"n"},
ToggleDate: []string{"d"},
OpenWeb: []string{"o"},
Now: []string{"t"},
},
}
}

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

// Apply config file first
fileConfig, _ := LoadConfigFile()

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

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

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

// Merge Keymaps
if len(fileConfig.Keymaps.PrevHour) > 0 {
mergedConfig.Keymaps.PrevHour = fileConfig.Keymaps.PrevHour
}

if len(fileConfig.Keymaps.NextHour) > 0 {
mergedConfig.Keymaps.NextHour = fileConfig.Keymaps.NextHour
}

if len(fileConfig.Keymaps.PrevDay) > 0 {
mergedConfig.Keymaps.PrevDay = fileConfig.Keymaps.PrevDay
}

if len(fileConfig.Keymaps.NextDay) > 0 {
mergedConfig.Keymaps.NextDay = fileConfig.Keymaps.NextDay
}

if len(fileConfig.Keymaps.PrevWeek) > 0 {
mergedConfig.Keymaps.PrevWeek = fileConfig.Keymaps.PrevWeek
}

if len(fileConfig.Keymaps.NextWeek) > 0 {
mergedConfig.Keymaps.NextWeek = fileConfig.Keymaps.NextWeek
}

if len(fileConfig.Keymaps.ToggleDate) > 0 {
mergedConfig.Keymaps.ToggleDate = fileConfig.Keymaps.ToggleDate
}

if len(fileConfig.Keymaps.OpenWeb) > 0 {
mergedConfig.Keymaps.OpenWeb = fileConfig.Keymaps.OpenWeb
}

if len(fileConfig.Keymaps.Now) > 0 {
mergedConfig.Keymaps.Now = fileConfig.Keymaps.Now
}

return mergedConfig, nil
}
10 changes: 5 additions & 5 deletions config_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"time"
)

// LoadConfig from environment
func LoadConfig(tzConfigs []string) (*Config, error) {
// LoadConfigEnv from environment
func LoadConfigEnv(tzConfigs []string) (*Config, error) {
conf := Config{
Zones: DefaultZones,
}
Expand All @@ -50,7 +50,7 @@ func LoadConfig(tzConfigs []string) (*Config, error) {

// Add zones from TZ_LIST
for i, zoneConf := range tzConfigs {
zone, err := SetupZone(time.Now(), zoneConf)
zone, err := ReadZoneFromString(time.Now(), zoneConf)
if err != nil {
return nil, err
}
Expand All @@ -61,8 +61,8 @@ func LoadConfig(tzConfigs []string) (*Config, error) {
return &conf, nil
}

// SetupZone from current time and a zoneConf string
func SetupZone(now time.Time, zoneConf string) (*Zone, error) {
// ReadZoneFromString from current time and a zoneConf string
func ReadZoneFromString(now time.Time, zoneConf string) (*Zone, error) {
names := strings.Split(zoneConf, ",")
dbName := strings.Trim(names[0], " ")
var name string
Expand Down
4 changes: 2 additions & 2 deletions config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type ConfigFileKeymaps struct {
Now []string `toml:"now"`
}

func setupZone(now time.Time, zoneConf ConfigFileZone) (*Zone, error) {
func ReadZonesFromFile(now time.Time, zoneConf ConfigFileZone) (*Zone, error) {
name := zoneConf.Name
dbName := zoneConf.ID

Expand Down Expand Up @@ -91,7 +91,7 @@ func LoadConfigFile() (*Config, error) {

// Add zones from config file
for i, zoneConf := range config.Zones {
zone, err := setupZone(time.Now(), zoneConf)
zone, err := ReadZonesFromFile(time.Now(), zoneConf)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestSetupZone(t *testing.T) {
},
}
for _, test := range tests {
_, err := SetupZone(now, test.zoneName)
_, err := ReadZoneFromString(now, test.zoneName)
if test.ok != (err == nil) {
t.Errorf("Expected %v, but got: %v", test.ok, err)
}
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestSetupZoneWithCustomNames(t *testing.T) {
},
}
for _, test := range tests {
z, err := SetupZone(now, test.zoneName)
z, err := ReadZoneFromString(now, test.zoneName)
if test.ok != (err == nil) {
t.Errorf("Expected %v, but got: %v", test.ok, err)
}
Expand Down
2 changes: 0 additions & 2 deletions example-conf.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
header = "What time is it?"

[[zones]]
id = "NZ"
name = "NZ"
Expand Down
9 changes: 3 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,11 @@ func main() {
os.Exit(0)
}

fileConfig, err := LoadConfigFile()
// envConfig, err := LoadConfig(flag.Args())
config, err := LoadConfig(flag.Args())

var initialModel = model{
zones: fileConfig.Zones,
keymaps: fileConfig.Keymaps,
zones: config.Zones,
keymaps: config.Keymaps,
clock: *NewClock(0),
showDates: false,
isMilitary: *military,
Expand All @@ -192,8 +191,6 @@ func main() {
os.Exit(2)
}

// initialModel.zones = envConfig.Zones

if *when != 0 {
initialModel.clock = *NewClock(*when)
}
Expand Down

0 comments on commit 31f3810

Please sign in to comment.