Skip to content

Commit 1865e84

Browse files
committed
Improve code documentation
1 parent a6370af commit 1865e84

File tree

2 files changed

+47
-41
lines changed

2 files changed

+47
-41
lines changed

base_dirs.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ func (bd baseDirectories) cacheFile(relPath string) (string, error) {
3939
return createPath(relPath, []string{bd.cacheHome})
4040
}
4141

42-
func (bd baseDirectories) stateFile(relPath string) (string, error) {
43-
return createPath(relPath, []string{bd.stateHome})
44-
}
45-
4642
func (bd baseDirectories) runtimeFile(relPath string) (string, error) {
4743
fi, err := os.Lstat(bd.runtime)
4844
if err != nil {
@@ -67,6 +63,10 @@ func (bd baseDirectories) runtimeFile(relPath string) (string, error) {
6763
return createPath(relPath, []string{bd.runtime})
6864
}
6965

66+
func (bd baseDirectories) stateFile(relPath string) (string, error) {
67+
return createPath(relPath, []string{bd.stateHome})
68+
}
69+
7070
func (bd baseDirectories) searchDataFile(relPath string) (string, error) {
7171
return searchFile(relPath, append([]string{bd.dataHome}, bd.data...))
7272
}

xdg.go

+43-37
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ flavors of Unix.
1212
1313
For more information regarding the XDG user directories see:
1414
https://wiki.archlinux.org/index.php/XDG_user_directories
15+
16+
For more information regarding the XDG state directory proposal see:
17+
https://wiki.debian.org/XDGBaseDirectorySpecification#Proposal:_STATE_directory
18+
https://lists.freedesktop.org/archives/xdg/2016-December/013803.html
1519
*/
1620
package xdg
1721

@@ -21,14 +25,14 @@ var (
2125

2226
// DataHome defines the base directory relative to which user-specific
2327
// data files should be stored. This directory is defined by the
24-
// environment variable $XDG_DATA_HOME. If this variable is not set,
28+
// $XDG_DATA_HOME environment variable. If the variable is not set,
2529
// a default equal to $HOME/.local/share should be used.
2630
DataHome string
2731

2832
// DataDirs defines the preference-ordered set of base directories to
2933
// search for data files in addition to the DataHome base directory.
30-
// This set of directories is defined by the environment variable
31-
// $XDG_DATA_DIRS. If this variable is not set, the default directories
34+
// This set of directories is defined by the $XDG_DATA_DIRS environment
35+
// variable. If the variable is not set, the default directories
3236
// to be used are /usr/local/share and /usr/share, in that order. The
3337
// DataHome directory is considered more important than any of the
3438
// directories defined by DataDirs. Therefore, user data files should be
@@ -37,43 +41,43 @@ var (
3741

3842
// ConfigHome defines the base directory relative to which user-specific
3943
// configuration files should be written. This directory is defined by
40-
// the environment variable $XDG_CONFIG_HOME. If this variable is not
44+
// the $XDG_CONFIG_HOME environment variable. If the variable is not
4145
// not set, a default equal to $HOME/.config should be used.
4246
ConfigHome string
4347

4448
// ConfigDirs defines the preference-ordered set of base directories to
4549
// search for configuration files in addition to the ConfigHome base
46-
// directory. This set of directories is defined by the environment
47-
// variable $XDG_CONFIG_DIRS. If this variable is not set, a default
48-
// equal to /etc/xdg should be used. The ConfigHome directory is
49-
// considered more important than any of the directories defined by
50-
// ConfigDirs. Therefore, user config files should be written
51-
// relative to the ConfigHome directory, if possible.
50+
// directory. This set of directories is defined by the $XDG_CONFIG_DIRS
51+
// environment variable. If the variable is not set, a default equal
52+
// to /etc/xdg should be used. The ConfigHome directory is considered
53+
// more important than any of the directories defined by ConfigDirs.
54+
// Therefore, user config files should be written relative to the
55+
// ConfigHome directory, if possible.
5256
ConfigDirs []string
5357

54-
// StateHome defines the base directory relative to which user-specific
55-
// data (volatile) files should be stored. This directory is defined by the
56-
// environment variable $XDG_STATE_HOME. If this variable is not set,
57-
// a default equal to ~/.local/state should be used.
58-
StateHome string
59-
6058
// CacheHome defines the base directory relative to which user-specific
6159
// non-essential (cached) data should be written. This directory is
62-
// defined by the environment variable $XDG_CACHE_HOME. If this variable
60+
// defined by the $XDG_CACHE_HOME environment variable. If the variable
6361
// is not set, a default equal to $HOME/.cache should be used.
6462
CacheHome string
6563

6664
// RuntimeDir defines the base directory relative to which user-specific
6765
// non-essential runtime files and other file objects (such as sockets,
6866
// named pipes, etc.) should be stored. This directory is defined by the
69-
// environment variable $XDG_RUNTIME_DIR. If this variable is not set,
67+
// $XDG_RUNTIME_DIR environment variable. If the variable is not set,
7068
// applications should fall back to a replacement directory with similar
7169
// capabilities. Applications should use this directory for communication
7270
// and synchronization purposes and should not place larger files in it,
7371
// since it might reside in runtime memory and cannot necessarily be
7472
// swapped out to disk.
7573
RuntimeDir string
7674

75+
// StateHome defines the base directory relative to which user-specific
76+
// volatile data files should be stored. This directory is defined by
77+
// the non-standard $XDG_STATE_HOME environment variable. If the variable
78+
// is not set, a default equal to ~/.local/state should be used.
79+
StateHome string
80+
7781
// UserDirs defines the locations of well known user directories.
7882
UserDirs UserDirectories
7983

@@ -102,9 +106,9 @@ func Reload() {
102106
ConfigDirs = baseDirs.config
103107
CacheHome = baseDirs.cacheHome
104108
RuntimeDir = baseDirs.runtime
109+
StateHome = baseDirs.stateHome
105110
FontDirs = baseDirs.fonts
106111
ApplicationDirs = baseDirs.applications
107-
StateHome = baseDirs.stateHome
108112

109113
// Initialize user directories.
110114
initUserDirs(Home)
@@ -120,16 +124,6 @@ func DataFile(relPath string) (string, error) {
120124
return baseDirs.dataFile(relPath)
121125
}
122126

123-
// StateFile returns a suitable location for the specified data (volatile) file.
124-
// The relPath parameter must contain the name of the data file, and
125-
// optionally, a set of parent directories (e.g. appname/app.data).
126-
// If the specified directories do not exist, they will be created relative
127-
// to the base data directory. On failure, an error containing the
128-
// attempted paths is returned.
129-
func StateFile(relPath string) (string, error) {
130-
return baseDirs.stateFile(relPath)
131-
}
132-
133127
// ConfigFile returns a suitable location for the specified config file.
134128
// The relPath parameter must contain the name of the config file, and
135129
// optionally, a set of parent directories (e.g. appname/app.yaml).
@@ -160,6 +154,18 @@ func RuntimeFile(relPath string) (string, error) {
160154
return baseDirs.runtimeFile(relPath)
161155
}
162156

157+
// StateFile returns a suitable location for the specified state file. State
158+
// files are usually volatile data files, not suitable to be stored relative
159+
// to the $XDG_DATA_HOME directory.
160+
// The relPath parameter must contain the name of the state file, and
161+
// optionally, a set of parent directories (e.g. appname/app.state).
162+
// If the specified directories do not exist, they will be created relative
163+
// to the base state directory. On failure, an error containing the
164+
// attempted paths is returned.
165+
func StateFile(relPath string) (string, error) {
166+
return baseDirs.stateFile(relPath)
167+
}
168+
163169
// SearchDataFile searches for specified file in the data search paths.
164170
// The relPath parameter must contain the name of the data file, and
165171
// optionally, a set of parent directories (e.g. appname/app.data). If the
@@ -184,14 +190,6 @@ func SearchCacheFile(relPath string) (string, error) {
184190
return baseDirs.searchCacheFile(relPath)
185191
}
186192

187-
// SearchStateFile searches for the specified file in the state search path.
188-
// The relPath parameter must contain the name of the state file, and
189-
// optionally, a set of parent directories (e.g. appname/app.state). If the
190-
// file cannot be found, an error specifying the searched path is returned.
191-
func SearchStateFile(relPath string) (string, error) {
192-
return baseDirs.searchStateFile(relPath)
193-
}
194-
195193
// SearchRuntimeFile searches for the specified file in the runtime search path.
196194
// The relPath parameter must contain the name of the runtime file, and
197195
// optionally, a set of parent directories (e.g. appname/app.pid). If the
@@ -200,6 +198,14 @@ func SearchRuntimeFile(relPath string) (string, error) {
200198
return baseDirs.searchRuntimeFile(relPath)
201199
}
202200

201+
// SearchStateFile searches for the specified file in the state search path.
202+
// The relPath parameter must contain the name of the state file, and
203+
// optionally, a set of parent directories (e.g. appname/app.state). If the
204+
// file cannot be found, an error specifying the searched path is returned.
205+
func SearchStateFile(relPath string) (string, error) {
206+
return baseDirs.searchStateFile(relPath)
207+
}
208+
203209
func init() {
204210
Reload()
205211
}

0 commit comments

Comments
 (0)