File: userdirs.go

package info (click to toggle)
golang-github-rkoesters-xdg 0.0~git20181125.edd15b8-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 220 kB
  • sloc: makefile: 5
file content (121 lines) | stat: -rw-r--r-- 2,580 bytes parent folder | download
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Package userdirs provides easy access to "well known" user
// directories. For more information, see:
// https://www.freedesktop.org/wiki/Software/xdg-user-dirs/
package userdirs

import (
	"github.com/rkoesters/xdg/basedir"
	"github.com/rkoesters/xdg/keyfile"
	"io"
	"os"
	"path/filepath"
	"strings"
)

// The default set of userdirs. Most people will only need to use this.
var (
	Desktop     string
	Documents   string
	Download    string
	Music       string
	Pictures    string
	PublicShare string
	Templates   string
	Videos      string
)

func init() {
	f, err := os.Open(filepath.Join(basedir.ConfigHome, "user-dirs.dirs"))
	if err != nil {
		return
	}
	defer f.Close()

	dirs, err := New(f)
	if err != nil {
		return
	}

	Desktop = dirs.Desktop
	Documents = dirs.Documents
	Download = dirs.Download
	Music = dirs.Music
	Pictures = dirs.Pictures
	PublicShare = dirs.PublicShare
	Templates = dirs.Templates
	Videos = dirs.Videos
}

// UserDirs is a set of user directories that are common in graphical
// environments.
type UserDirs struct {
	Desktop     string
	Documents   string
	Download    string
	Music       string
	Pictures    string
	PublicShare string
	Templates   string
	Videos      string
}

// New creates a new UserDirs struct by reading from the given
// io.Reader.
func New(r io.Reader) (*UserDirs, error) {
	kf, err := keyfile.New(r)
	if err != nil {
		return nil, err
	}

	dirs := new(UserDirs)

	dirs.Desktop, err = parse(kf.String("", "XDG_DESKTOP_DIR"))
	if err != nil {
		return nil, err
	}
	dirs.Documents, err = parse(kf.String("", "XDG_DOCUMENTS_DIR"))
	if err != nil {
		return nil, err
	}
	dirs.Download, err = parse(kf.String("", "XDG_DOWNLOAD_DIR"))
	if err != nil {
		return nil, err
	}
	dirs.Music, err = parse(kf.String("", "XDG_MUSIC_DIR"))
	if err != nil {
		return nil, err
	}
	dirs.Pictures, err = parse(kf.String("", "XDG_PICTURES_DIR"))
	if err != nil {
		return nil, err
	}
	dirs.PublicShare, err = parse(kf.String("", "XDG_PUBLICSHARE_DIR"))
	if err != nil {
		return nil, err
	}
	dirs.Templates, err = parse(kf.String("", "XDG_TEMPLATES_DIR"))
	if err != nil {
		return nil, err
	}
	dirs.Videos, err = parse(kf.String("", "XDG_VIDEOS_DIR"))
	if err != nil {
		return nil, err
	}

	return dirs, nil
}

// parse takes a given string and returns it as a path.
func parse(s string, err error) (string, error) {
	if err != nil {
		return "", err
	}
	s = strings.Trim(s, "\"")
	if strings.HasPrefix(s, "$HOME") {
		s = filepath.Join(basedir.Home, strings.TrimPrefix(s, "$HOME"))
	}
	if s == "" {
		s = basedir.Home
	}
	return s, nil
}