Skip to content

Commit

Permalink
feat: try to load config file from current directory first
Browse files Browse the repository at this point in the history
ref #12
  • Loading branch information
rszyma committed Mar 22, 2024
1 parent 1b83023 commit 9a57630
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ Works on Windows and Linux.

Default config file will be autogenerated for you on the first run.
You can access it from from: `Click Tray Icon > Configure`.

Config file name is `config.toml`.

On Linux, the config folder location is `~/.config/kanata-tray`.
On Windows, it's `C:\Users\<YourUsername>\AppData\Roaming\kanata-tray`

Alternatively, you can place your config file in the same folder as kanata-tray executable,
and it will be have higher priority than the global config in user folder.

### Examples

An example of customized configuration file:
Expand Down
29 changes: 22 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,38 @@ func main() {
}

func mainImpl() error {
configFolder := configdir.LocalConfig("kanata-tray")
fmt.Printf("kanata-tray config folder: %s\n", configFolder)
configFileName := "config.toml"
var configFile string
var configFolder string

// Create folder. No-op if exists.
err := configdir.MakePath(configFolder)
// First try reading config.toml from the folder where kanata-tray is located.
exePath, err := os.Executable()
if err != nil {
return fmt.Errorf("failed to create folder: %v", err)
fmt.Println("Failed attempt to read config.toml from kanata-tray folder", err)
}
localConfigFolder := filepath.Dir(exePath)
localConfigFile := filepath.Join(localConfigFolder, configFileName)
if _, err := os.Stat(localConfigFile); os.IsNotExist(err) {
configFolder = configdir.LocalConfig("kanata-tray")
configFile = filepath.Join(configFolder, configFileName)
// Create folder. No-op if exists.
err = configdir.MakePath(configFolder)
if err != nil {
return fmt.Errorf("failed to create folder: %v", err)
}
} else {
configFolder = localConfigFolder
configFile = localConfigFile
}

fmt.Printf("kanata-tray config folder: %s\n", configFolder)

// Make sure "icons" folder exists too.
err = configdir.MakePath(filepath.Join(configFolder, "icons"))
if err != nil {
return fmt.Errorf("failed to create folder: %v", err)
}

configFile := filepath.Join(configFolder, "config.toml")

cfg, err := config.ReadConfigOrCreateIfNotExist(configFile)
if err != nil {
return fmt.Errorf("loading config failed: %v", err)
Expand Down

0 comments on commit 9a57630

Please sign in to comment.