Skip to content

Commit

Permalink
fix(config): add Windows home dir path (#88)
Browse files Browse the repository at this point in the history
* fix(config): add Windows home dir path

On PowerShell / CMD the HOME environment variable is often not defined, so $HOME/.config/qbt will evaluate to C:/.config/qbt instead of C:/<username>/.config/qbt as one would expect.

* fix: make config path lookup windows compatible

---------

Co-authored-by: Ludvig Lundgren <[email protected]>
  • Loading branch information
Acamol and ludviglundgren authored Aug 19, 2024
1 parent e7d804b commit 81a4c06
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package config

import (
"errors"
"fmt"
"os"
"path/filepath"

"github.com/ludviglundgren/qbittorrent-cli/internal/domain"

Expand Down Expand Up @@ -32,18 +34,21 @@ func InitConfig() {
os.Exit(1)
}

// Search config in directories
viper.SetConfigName(".qbt")
// Search config in directories
// call multiple times to add many search paths
viper.AddConfigPath(".") // optionally look for config in the working directory
viper.AddConfigPath(home)
viper.AddConfigPath("$HOME/.config/qbt") // call multiple times to add many search paths
viper.AddConfigPath(filepath.Join(home, ".config", "qbt")) // windows path
viper.AddConfigPath("$HOME/.config/qbt")
}

if err := viper.ReadInConfig(); err != nil {
if ferr, ok := err.(*viper.ConfigFileNotFoundError); ok {
var ferr *viper.ConfigFileNotFoundError
if errors.As(err, &ferr) {
fmt.Printf("config file not found: err %q\n", ferr)
} else {
fmt.Println("Could not read config file:", err)
fmt.Printf("could not read config: err %q\n", err)
}
os.Exit(1)
}
Expand Down

0 comments on commit 81a4c06

Please sign in to comment.