-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move UserDirectories struct in userdirs package
- Loading branch information
Showing
5 changed files
with
106 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
2 changes: 1 addition & 1 deletion
2
internal/userdirs/userdirs_unix_test.go → internal/userdirs/config_unix_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters