Skip to content

Commit

Permalink
Move UserDirectories struct in userdirs package
Browse files Browse the repository at this point in the history
  • Loading branch information
adrg committed Jul 8, 2024
1 parent e72f914 commit 27a1554
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 100 deletions.
83 changes: 83 additions & 0 deletions internal/userdirs/config_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//go:build aix || dragonfly || freebsd || (js && wasm) || nacl || linux || netbsd || openbsd || solaris

package userdirs

import (
"bufio"
"io"
"os"
"strings"

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

// 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. An empty map is returned if an error is encountered.
func ParseConfigFile(name string) map[string]string {
f, err := os.Open(name)
if err != nil {
return map[string]string{}
}
defer f.Close()

return ParseConfig(f)
}

// ParseConfig parses the user directories config file contained in the provided
// reader. The returned map contains pairs consisting of the user directory
// names and their paths. An empty map is returned if an error is encountered.
func ParseConfig(r io.Reader) map[string]string {
dirs := map[string]string{}

scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 || line[0] == '#' {
continue
}
if !strings.HasPrefix(line, "XDG_") {
continue
}

parts := strings.Split(line, "=")
if len(parts) < 2 {
continue
}

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

// Parse value.
runes := []rune(strings.TrimSpace(parts[1]))

lenRunes := len(runes)
if lenRunes <= 2 || runes[0] != '"' {
continue
}

for i := 1; i < lenRunes; i++ {
if runes[i] == '"' {
dirs[key] = pathutil.ExpandHome(string(runes[1:i]))
break
}
}
}
if err := scanner.Err(); err != nil {
return dirs
}

return dirs
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build aix || darwin || dragonfly || freebsd || (js && wasm) || nacl || linux || netbsd || openbsd || solaris
//go:build aix || dragonfly || freebsd || (js && wasm) || nacl || linux || netbsd || openbsd || solaris

package userdirs_test

Expand Down
89 changes: 18 additions & 71 deletions internal/userdirs/userdirs.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
package userdirs

import (
"bufio"
"io"
"os"
"strings"

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

// XDG user directories environment variables.
const (
EnvDesktopDir = "XDG_DESKTOP_DIR"
Expand All @@ -21,73 +12,29 @@ const (
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. An empty map is returned if an error is encountered.
func ParseConfigFile(name string) map[string]string {
f, err := os.Open(name)
if err != nil {
return map[string]string{}
}
defer f.Close()

return ParseConfig(f)
}

// ParseConfig parses the user directories config file contained in the provided
// reader. The returned map contains pairs consisting of the user directory
// names and their paths. An empty map is returned if an error is encountered.
func ParseConfig(r io.Reader) map[string]string {
dirs := map[string]string{}
// Directories defines the locations of well known user directories.
type Directories struct {
// Desktop defines the location of the user's desktop directory.
Desktop string

scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) == 0 || line[0] == '#' {
continue
}
if !strings.HasPrefix(line, "XDG_") {
continue
}
// Download defines a suitable location for user downloaded files.
Download string

parts := strings.Split(line, "=")
if len(parts) < 2 {
continue
}
// Documents defines a suitable location for user document files.
Documents string

// Parse key.
key := strings.TrimSpace(parts[0])
switch key {
case EnvDesktopDir,
EnvDownloadDir,
EnvDocumentsDir,
EnvMusicDir,
EnvPicturesDir,
EnvVideosDir,
EnvTemplatesDir,
EnvPublicShareDir:
default:
continue
}
// Music defines a suitable location for user audio files.
Music string

// Parse value.
runes := []rune(strings.TrimSpace(parts[1]))
// Pictures defines a suitable location for user image files.
Pictures string

lenRunes := len(runes)
if lenRunes <= 2 || runes[0] != '"' {
continue
}
// VideosDir defines a suitable location for user video files.
Videos string

for i := 1; i < lenRunes; i++ {
if runes[i] == '"' {
dirs[key] = pathutil.ExpandHome(string(runes[1:i]))
break
}
}
}
if err := scanner.Err(); err != nil {
return dirs
}
// Templates defines a suitable location for user template files.
Templates string

return dirs
// PublicShare defines a suitable location for user shared files.
PublicShare string
}
28 changes: 0 additions & 28 deletions user_dirs.go

This file was deleted.

4 changes: 4 additions & 0 deletions xdg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package xdg

import (
"github.com/adrg/xdg/internal/pathutil"
"github.com/adrg/xdg/internal/userdirs"
)

// UserDirectories defines the locations of well known user directories.
type UserDirectories = userdirs.Directories

var (
// Home contains the path of the user's home directory.
Home string
Expand Down

0 comments on commit 27a1554

Please sign in to comment.