Skip to content

Commit

Permalink
config_file.go: Parameterise filename for unit testing (fix oz#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnd-au committed Oct 25, 2024
1 parent 2ccf17f commit 57ea447
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
12 changes: 10 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,17 @@ var DefaultKeymaps = Keymaps{
Quit: []string{"q", "ctrl+c", "esc"},
}

func LoadConfig(tzConfigs []string) (*Config, error) {
func LoadDefaultConfig(tzConfigs []string) (*Config, error) {
fileName, fileError := DefaultConfigFile()
if fileError != nil {
return nil, fmt.Errorf("File error: %w", fileError)
}
return LoadConfig(*fileName, tzConfigs)
}

func LoadConfig(tomlFile string, tzConfigs []string) (*Config, error) {
// Apply config file first
fileConfig, fileError := LoadConfigFile()
fileConfig, fileError := LoadConfigFile(tomlFile)
if fileError != nil {
return nil, fmt.Errorf("File error: %w", fileError)
}
Expand Down
12 changes: 8 additions & 4 deletions config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,20 @@ func ReadZonesFromFile(now time.Time, zoneConf ConfigFileZone) (*Zone, error) {
}, nil
}

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

func DefaultConfigFile() (*string, error) {
// Return early if we can't find a home dir.
homeDir, err := os.UserHomeDir()
if err != nil {
return &conf, nil
return nil, err
}

configFilePath := filepath.Join(homeDir, ".config", "tz", "conf.toml")
return &configFilePath, nil
}

func LoadConfigFile(configFilePath string) (*Config, error) {
conf := Config{}

configFile, err := os.ReadFile(configFilePath)
if err != nil {
// Ignore unreadable config file.
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func main() {
os.Exit(0)
}

config, err := LoadConfig(flag.Args())
config, err := LoadDefaultConfig(flag.Args())
if err != nil {
fmt.Fprintf(os.Stderr, "Config error: %s\n", err)
os.Exit(2)
Expand Down

0 comments on commit 57ea447

Please sign in to comment.