-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles_linux.go
More file actions
56 lines (49 loc) · 1.15 KB
/
files_linux.go
File metadata and controls
56 lines (49 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
)
func desktopDir() string {
home, err := os.UserHomeDir()
if err != nil {
fmt.Fprintf(os.Stderr, "error: cannot determine home directory: %s\n", err)
os.Exit(1)
}
// XDG_DESKTOP_DIR env var takes precedence.
if dir := os.Getenv("XDG_DESKTOP_DIR"); dir != "" {
return dir
}
// Parse ~/.config/user-dirs.dirs for XDG_DESKTOP_DIR.
if dir := readXDGDesktopDir(home); dir != "" {
return dir
}
return filepath.Join(home, "Desktop")
}
// readXDGDesktopDir parses the XDG user-dirs.dirs file and returns the
// value of XDG_DESKTOP_DIR, or "" if not found.
func readXDGDesktopDir(home string) string {
path := filepath.Join(home, ".config", "user-dirs.dirs")
f, err := os.Open(path)
if err != nil {
return ""
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if !strings.HasPrefix(line, "XDG_DESKTOP_DIR") {
continue
}
parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 {
continue
}
val := strings.Trim(parts[1], "\"")
val = strings.ReplaceAll(val, "$HOME", home)
return val
}
return ""
}