Skip to content

Commit

Permalink
Merge pull request #140 from mattn/windows
Browse files Browse the repository at this point in the history
Support Windows paths and separators
  • Loading branch information
ephemeralriggs authored Nov 10, 2024
2 parents 0cbbf0d + bbe13c6 commit f288654
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
8 changes: 4 additions & 4 deletions index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package index
import (
"bufio"
"os"
"path"
"path/filepath"
"time"

"github.com/ambientsound/pms/console"
Expand Down Expand Up @@ -49,8 +49,8 @@ func New(basePath string) (*Index, error) {

i := &Index{}
i.path = basePath
i.indexPath = path.Join(i.path, "index")
i.statePath = path.Join(i.path, "state")
i.indexPath = filepath.Join(i.path, "index")
i.statePath = filepath.Join(i.path, "state")

// Try to stat the Bleve index path. If it does not exist, create it.
if _, err := os.Stat(i.indexPath); err != nil {
Expand Down Expand Up @@ -123,7 +123,7 @@ func open(path string) (bleve.Index, error) {
// server should be stored.
func Path(host, port string) string {
cacheDir := xdg.CacheDirectory()
return path.Join(cacheDir, host, port)
return filepath.Join(cacheDir, host, port)
}

// SetVersion writes the MPD library version to the state file.
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"

"github.com/ambientsound/pms/console"
Expand Down Expand Up @@ -110,7 +110,7 @@ func main() {
// Source configuration files from all XDG standard directories.
configDirs := xdg.ConfigDirectories()
for _, dir := range configDirs {
path := path.Join(dir, "pms.conf")
path := filepath.Join(dir, "pms.conf")
p.Message("Reading configuration file '%s'.", path)
err = p.SourceConfigFile(path)
if err != nil {
Expand Down
19 changes: 13 additions & 6 deletions xdg/xdg.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@ package xdg

import (
"os"
"path"
"path/filepath"
"runtime"
"strings"
)

// appendPmsDirectory adds "pms" to a directory tree.
func appendPmsDirectory(dir string) string {
return path.Join(dir, "pms")
return filepath.Join(dir, "pms")
}

// ConfigDirectories returns a list of configuration directories. The least
// important directory is listed first.
func ConfigDirectories() []string {
if runtime.GOOS == "windows" {
if dir, err := os.UserConfigDir(); err == nil {
return []string{appendPmsDirectory(dir)}
}
}

dirs := make([]string, 0)

// $XDG_CONFIG_DIRS defines the preference-ordered set of base directories
Expand All @@ -30,7 +37,7 @@ func ConfigDirectories() []string {
}

// Add entries from $XDG_CONFIG_DIRS to directory list.
configDirs := strings.Split(xdgConfigDirs, ":")
configDirs := strings.Split(xdgConfigDirs, string(os.PathListSeparator))
for i := len(configDirs) - 1; i >= 0; i-- {
if len(configDirs[i]) > 0 {
dir := appendPmsDirectory(configDirs[i])
Expand All @@ -43,7 +50,7 @@ func ConfigDirectories() []string {
// either not set or empty, a default equal to $HOME/.config should be used.
xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")
if len(xdgConfigHome) == 0 {
xdgConfigHome = path.Join(os.Getenv("HOME"), ".config")
xdgConfigHome = filepath.Join(os.Getenv("HOME"), ".config")
}
dir := appendPmsDirectory(xdgConfigHome)

Expand All @@ -60,8 +67,8 @@ func CacheDirectory() string {
// either not set or empty, a default equal to $HOME/.cache should be used.
xdgCacheHome := os.Getenv("XDG_CACHE_HOME")
if len(xdgCacheHome) == 0 {
xdgCacheHome = path.Join(os.Getenv("HOME"), ".cache")
xdgCacheHome = filepath.Join(os.Getenv("HOME"), ".cache")
}

return path.Join(xdgCacheHome, "pms")
return filepath.Join(xdgCacheHome, "pms")
}

0 comments on commit f288654

Please sign in to comment.