Skip to content

Commit

Permalink
Move XDG user dirs environment variable constants in userdirs interna…
Browse files Browse the repository at this point in the history
…l package
  • Loading branch information
adrg committed Jul 6, 2024
1 parent c4c6b00 commit e0f2547
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 55 deletions.
34 changes: 23 additions & 11 deletions internal/userdirs/userdirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ import (
"github.com/adrg/xdg/internal/pathutil"
)

// XDG user directories environment variables.
const (
EnvDesktopDir = "XDG_DESKTOP_DIR"
EnvDownloadDir = "XDG_DOWNLOAD_DIR"
EnvDocumentsDir = "XDG_DOCUMENTS_DIR"
EnvMusicDir = "XDG_MUSIC_DIR"
EnvPicturesDir = "XDG_PICTURES_DIR"
EnvVideosDir = "XDG_VIDEOS_DIR"
EnvTemplatesDir = "XDG_TEMPLATES_DIR"
EnvPublicShareDir = "XDG_PUBLICSHARE_DIR"
)

// ParseConfigFile parses the user directories config file at the specified
// location. The returned map contains pairs consisting of the user directory
// names and their paths.
Expand All @@ -26,16 +38,7 @@ func ParseConfigFile(name string) map[string]string {
// provided reader. The returned map contains pairs consisting of the user
// directory names and their paths.
func ParseConfig(r io.Reader) map[string]string {
dirs := map[string]string{
"XDG_DESKTOP_DIR": "",
"XDG_DOWNLOAD_DIR": "",
"XDG_DOCUMENTS_DIR": "",
"XDG_MUSIC_DIR": "",
"XDG_PICTURES_DIR": "",
"XDG_VIDEOS_DIR": "",
"XDG_TEMPLATES_DIR": "",
"XDG_PUBLICSHARE_DIR": "",
}
dirs := map[string]string{}

scanner := bufio.NewScanner(r)
for scanner.Scan() {
Expand All @@ -54,7 +57,16 @@ func ParseConfig(r io.Reader) map[string]string {

// Parse key.
key := strings.TrimSpace(parts[0])
if _, ok := dirs[key]; !ok {
switch key {
case EnvDesktopDir,
EnvDownloadDir,
EnvDocumentsDir,
EnvMusicDir,
EnvPicturesDir,
EnvVideosDir,
EnvTemplatesDir,
EnvPublicShareDir:
default:
continue
}

Expand Down
18 changes: 10 additions & 8 deletions paths_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package xdg

import (
"path/filepath"

"github.com/adrg/xdg/internal/userdirs"
)

func initDirs(home string) {
Expand Down Expand Up @@ -40,12 +42,12 @@ func initBaseDirs(home string) {
}

func initUserDirs(home string) {
UserDirs.Desktop = xdgPath(envDesktopDir, filepath.Join(home, "Desktop"))
UserDirs.Download = xdgPath(envDownloadDir, filepath.Join(home, "Downloads"))
UserDirs.Documents = xdgPath(envDocumentsDir, filepath.Join(home, "Documents"))
UserDirs.Music = xdgPath(envMusicDir, filepath.Join(home, "Music"))
UserDirs.Pictures = xdgPath(envPicturesDir, filepath.Join(home, "Pictures"))
UserDirs.Videos = xdgPath(envVideosDir, filepath.Join(home, "Movies"))
UserDirs.Templates = xdgPath(envTemplatesDir, filepath.Join(home, "Templates"))
UserDirs.PublicShare = xdgPath(envPublicShareDir, filepath.Join(home, "Public"))
UserDirs.Desktop = xdgPath(userdirs.EnvDesktopDir, filepath.Join(home, "Desktop"))
UserDirs.Download = xdgPath(userdirs.EnvDownloadDir, filepath.Join(home, "Downloads"))
UserDirs.Documents = xdgPath(userdirs.EnvDocumentsDir, filepath.Join(home, "Documents"))
UserDirs.Music = xdgPath(userdirs.EnvMusicDir, filepath.Join(home, "Music"))
UserDirs.Pictures = xdgPath(userdirs.EnvPicturesDir, filepath.Join(home, "Pictures"))
UserDirs.Videos = xdgPath(userdirs.EnvVideosDir, filepath.Join(home, "Movies"))
UserDirs.Templates = xdgPath(userdirs.EnvTemplatesDir, filepath.Join(home, "Templates"))
UserDirs.PublicShare = xdgPath(userdirs.EnvPublicShareDir, filepath.Join(home, "Public"))
}
18 changes: 10 additions & 8 deletions paths_plan9.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package xdg

import (
"path/filepath"

"github.com/adrg/xdg/internal/userdirs"
)

func initDirs(home string) {
Expand Down Expand Up @@ -35,12 +37,12 @@ func initBaseDirs(home string) {
}

func initUserDirs(home string) {
UserDirs.Desktop = xdgPath(envDesktopDir, filepath.Join(home, "desktop"))
UserDirs.Download = xdgPath(envDownloadDir, filepath.Join(home, "downloads"))
UserDirs.Documents = xdgPath(envDocumentsDir, filepath.Join(home, "documents"))
UserDirs.Music = xdgPath(envMusicDir, filepath.Join(home, "music"))
UserDirs.Pictures = xdgPath(envPicturesDir, filepath.Join(home, "pictures"))
UserDirs.Videos = xdgPath(envVideosDir, filepath.Join(home, "videos"))
UserDirs.Templates = xdgPath(envTemplatesDir, filepath.Join(home, "templates"))
UserDirs.PublicShare = xdgPath(envPublicShareDir, filepath.Join(home, "public"))
UserDirs.Desktop = xdgPath(userdirs.EnvDesktopDir, filepath.Join(home, "desktop"))
UserDirs.Download = xdgPath(userdirs.EnvDownloadDir, filepath.Join(home, "downloads"))
UserDirs.Documents = xdgPath(userdirs.EnvDocumentsDir, filepath.Join(home, "documents"))
UserDirs.Music = xdgPath(userdirs.EnvMusicDir, filepath.Join(home, "music"))
UserDirs.Pictures = xdgPath(userdirs.EnvPicturesDir, filepath.Join(home, "pictures"))
UserDirs.Videos = xdgPath(userdirs.EnvVideosDir, filepath.Join(home, "videos"))
UserDirs.Templates = xdgPath(userdirs.EnvTemplatesDir, filepath.Join(home, "templates"))
UserDirs.PublicShare = xdgPath(userdirs.EnvPublicShareDir, filepath.Join(home, "public"))
}
16 changes: 8 additions & 8 deletions paths_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ func initUserDirs(home, configHome string) {
userDirsMap = map[string]string{}
}

UserDirs.Desktop = xdgPath(envDesktopDir, userDirsMap[envDesktopDir], filepath.Join(home, "Desktop"))
UserDirs.Download = xdgPath(envDownloadDir, userDirsMap[envDownloadDir], filepath.Join(home, "Downloads"))
UserDirs.Documents = xdgPath(envDocumentsDir, userDirsMap[envDocumentsDir], filepath.Join(home, "Documents"))
UserDirs.Music = xdgPath(envMusicDir, userDirsMap[envMusicDir], filepath.Join(home, "Music"))
UserDirs.Pictures = xdgPath(envPicturesDir, userDirsMap[envPicturesDir], filepath.Join(home, "Pictures"))
UserDirs.Videos = xdgPath(envVideosDir, userDirsMap[envVideosDir], filepath.Join(home, "Videos"))
UserDirs.Templates = xdgPath(envTemplatesDir, userDirsMap[envTemplatesDir], filepath.Join(home, "Templates"))
UserDirs.PublicShare = xdgPath(envPublicShareDir, userDirsMap[envPublicShareDir], filepath.Join(home, "Public"))
UserDirs.Desktop = xdgPath(userdirs.EnvDesktopDir, userDirsMap[userdirs.EnvDesktopDir], filepath.Join(home, "Desktop"))
UserDirs.Download = xdgPath(userdirs.EnvDownloadDir, userDirsMap[userdirs.EnvDownloadDir], filepath.Join(home, "Downloads"))
UserDirs.Documents = xdgPath(userdirs.EnvDocumentsDir, userDirsMap[userdirs.EnvDocumentsDir], filepath.Join(home, "Documents"))
UserDirs.Music = xdgPath(userdirs.EnvMusicDir, userDirsMap[userdirs.EnvMusicDir], filepath.Join(home, "Music"))
UserDirs.Pictures = xdgPath(userdirs.EnvPicturesDir, userDirsMap[userdirs.EnvPicturesDir], filepath.Join(home, "Pictures"))
UserDirs.Videos = xdgPath(userdirs.EnvVideosDir, userDirsMap[userdirs.EnvVideosDir], filepath.Join(home, "Videos"))
UserDirs.Templates = xdgPath(userdirs.EnvTemplatesDir, userDirsMap[userdirs.EnvTemplatesDir], filepath.Join(home, "Templates"))
UserDirs.PublicShare = xdgPath(userdirs.EnvPublicShareDir, userDirsMap[userdirs.EnvPublicShareDir], filepath.Join(home, "Public"))
}
17 changes: 9 additions & 8 deletions paths_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"path/filepath"

"github.com/adrg/xdg/internal/pathutil"
"github.com/adrg/xdg/internal/userdirs"
"golang.org/x/sys/windows"
)

Expand Down Expand Up @@ -35,14 +36,14 @@ func initBaseDirs(home string, kf *knownFolders) {
}

func initUserDirs(home string, kf *knownFolders) {
UserDirs.Desktop = xdgPath(envDesktopDir, kf.desktop)
UserDirs.Download = xdgPath(envDownloadDir, kf.downloads)
UserDirs.Documents = xdgPath(envDocumentsDir, kf.documents)
UserDirs.Music = xdgPath(envMusicDir, kf.music)
UserDirs.Pictures = xdgPath(envPicturesDir, kf.pictures)
UserDirs.Videos = xdgPath(envVideosDir, kf.videos)
UserDirs.Templates = xdgPath(envTemplatesDir, kf.templates)
UserDirs.PublicShare = xdgPath(envPublicShareDir, kf.public)
UserDirs.Desktop = xdgPath(userdirs.EnvDesktopDir, kf.desktop)
UserDirs.Download = xdgPath(userdirs.EnvDownloadDir, kf.downloads)
UserDirs.Documents = xdgPath(userdirs.EnvDocumentsDir, kf.documents)
UserDirs.Music = xdgPath(userdirs.EnvMusicDir, kf.music)
UserDirs.Pictures = xdgPath(userdirs.EnvPicturesDir, kf.pictures)
UserDirs.Videos = xdgPath(userdirs.EnvVideosDir, kf.videos)
UserDirs.Templates = xdgPath(userdirs.EnvTemplatesDir, kf.templates)
UserDirs.PublicShare = xdgPath(userdirs.EnvPublicShareDir, kf.public)
}

type knownFolders struct {
Expand Down
12 changes: 0 additions & 12 deletions user_dirs.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
package xdg

// XDG user directories environment variables.
const (
envDesktopDir = "XDG_DESKTOP_DIR"
envDownloadDir = "XDG_DOWNLOAD_DIR"
envDocumentsDir = "XDG_DOCUMENTS_DIR"
envMusicDir = "XDG_MUSIC_DIR"
envPicturesDir = "XDG_PICTURES_DIR"
envVideosDir = "XDG_VIDEOS_DIR"
envTemplatesDir = "XDG_TEMPLATES_DIR"
envPublicShareDir = "XDG_PUBLICSHARE_DIR"
)

// UserDirectories defines the locations of well known user directories.
type UserDirectories struct {
// Desktop defines the location of the user's desktop directory.
Expand Down

0 comments on commit e0f2547

Please sign in to comment.