Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add experimental Plan 9 support #15

Merged
merged 8 commits into from
Mar 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ jobs:
build:
strategy:
matrix:
go: ['1.14']
go: ['1.16']
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
93 changes: 37 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@ applications should use the XDG defined locations instead of hardcoding paths.

The package also includes the locations of well known [user directories](https://wiki.archlinux.org/index.php/XDG_user_directories)
and an implementation of the [state directory](https://wiki.debian.org/XDGBaseDirectorySpecification#Proposal:_STATE_directory) proposal.
Windows, macOS and most flavors of Unix are supported.
Most flavors of Unix, Windows, macOS and Plan 9 are supported.

Full documentation can be found at: https://pkg.go.dev/github.com/adrg/xdg.

@@ -58,75 +58,56 @@ present in the environment.

#### XDG Base Directory

| | Unix | macOS | Windows |
| :-------------- | :---------------------------------- | :------------------------------------------------------------------------------------ | :-------------------------------------- |
| XDG_DATA_HOME | `~/.local/share` | `~/Library/Application Support` | `%LOCALAPPDATA%` |
| XDG_DATA_DIRS | `/usr/local/share`<br/>`/usr/share` | `/Library/Application Support` | `%APPDATA%\Roaming`<br/>`%PROGRAMDATA%` |
| XDG_CONFIG_HOME | `~/.config` | `~/Library/Application Support` | `%LOCALAPPDATA%` |
| XDG_CONFIG_DIRS | `/etc/xdg` | `~/Library/Preferences`<br/>`/Library/Application Support`<br/>`/Library/Preferences` | `%PROGRAMDATA%` |
| XDG_CACHE_HOME | `~/.cache` | `~/Library/Caches` | `%LOCALAPPDATA%\cache` |
| XDG_RUNTIME_DIR | `/run/user/UID` | `~/Library/Application Support` | `%LOCALAPPDATA%` |
| | Unix | macOS | Windows | Plan 9 |
| :--------------------------------------------- | :---------------------------------------------------- | :--------------------------------------------------------------------------------------------------------------- | :-------------------------------------------------------- | :------------------------- |
| <kbd><b><samp>XDG_DATA_HOME</samp></b></kbd> | <kbd>~/.local/share</kbd> | <kbd>~/Library/Application Support</kbd> | <kbd>%LOCALAPPDATA%</kbd> | <kbd>$home/lib</kbd> |
| <kbd><b><samp>XDG_DATA_DIRS</samp></b></kbd> | <kbd>/usr/local/share</kbd><br/><kbd>/usr/share</kbd> | <kbd>/Library/Application Support</kbd> | <kbd>%APPDATA%\Roaming</kbd><br/><kbd>%PROGRAMDATA%</kbd> | <kbd>/lib</kbd> |
| <kbd><b><samp>XDG_CONFIG_HOME</samp></b></kbd> | <kbd>~/.config</kbd> | <kbd>~/Library/Application Support</kbd> | <kbd>%LOCALAPPDATA%</kbd> | <kbd>$home/lib</kbd> |
| <kbd><b><samp>XDG_CONFIG_DIRS</samp></b></kbd> | <kbd>/etc/xdg</kbd> | <kbd>~/Library/Preferences</kbd><br/><kbd>/Library/Application Support</kbd><br/><kbd>/Library/Preferences</kbd> | <kbd>%PROGRAMDATA%</kbd> | <kbd>/lib</kbd> |
| <kbd><b><samp>XDG_CACHE_HOME</samp></b></kbd> | <kbd>~/.cache</kbd> | <kbd>~/Library/Caches</kbd> | <kbd>%LOCALAPPDATA%\cache</kbd> | <kbd>$home/lib/cache</kbd> |
| <kbd><b><samp>XDG_RUNTIME_DIR</samp></b></kbd> | <kbd>/run/user/UID</kbd> | <kbd>~/Library/Application Support</kbd> | <kbd>%LOCALAPPDATA%</kbd> | <kbd>/tmp</kbd> |

#### XDG user directories

| | Unix | macOS | Windows |
| :------------------ | :------------ | :------------ | :------------------------ |
| XDG_DESKTOP_DIR | `~/Desktop` | `~/Desktop` | `%USERPROFILE%/Desktop` |
| XDG_DOWNLOAD_DIR | `~/Downloads` | `~/Downloads` | `%USERPROFILE%/Downloads` |
| XDG_DOCUMENTS_DIR | `~/Documents` | `~/Documents` | `%USERPROFILE%/Documents` |
| XDG_MUSIC_DIR | `~/Music` | `~/Music` | `%USERPROFILE%/Music` |
| XDG_PICTURES_DIR | `~/Pictures` | `~/Pictures` | `%USERPROFILE%/Pictures` |
| XDG_VIDEOS_DIR | `~/Videos` | `~/Movies` | `%USERPROFILE%/Videos` |
| XDG_TEMPLATES_DIR | `~/Templates` | `~/Templates` | `%USERPROFILE%/Templates` |
| XDG_PUBLICSHARE_DIR | `~/Public` | `~/Public` | `%PUBLIC%` |
| | Unix | macOS | Windows | Plan 9 |
| :------------------------------------------------- | :--------------------- | :--------------------- | :--------------------------------- | :------------------------- |
| <kbd><b><samp>XDG_DESKTOP_DIR</samp></b></kbd> | <kbd>~/Desktop</kbd> | <kbd>~/Desktop</kbd> | <kbd>%USERPROFILE%\Desktop</kbd> | <kbd>$home/desktop</kbd> |
| <kbd><b><samp>XDG_DOWNLOAD_DIR</samp></b></kbd> | <kbd>~/Downloads</kbd> | <kbd>~/Downloads</kbd> | <kbd>%USERPROFILE%\Downloads</kbd> | <kbd>$home/downloads</kbd> |
| <kbd><b><samp>XDG_DOCUMENTS_DIR</samp></b></kbd> | <kbd>~/Documents</kbd> | <kbd>~/Documents</kbd> | <kbd>%USERPROFILE%\Documents</kbd> | <kbd>$home/documents</kbd> |
| <kbd><b><samp>XDG_MUSIC_DIR</samp></b></kbd> | <kbd>~/Music</kbd> | <kbd>~/Music</kbd> | <kbd>%USERPROFILE%\Music</kbd> | <kbd>$home/music</kbd> |
| <kbd><b><samp>XDG_PICTURES_DIR</samp></b></kbd> | <kbd>~/Pictures</kbd> | <kbd>~/Pictures</kbd> | <kbd>%USERPROFILE%\Pictures</kbd> | <kbd>$home/pictures</kbd> |
| <kbd><b><samp>XDG_VIDEOS_DIR</samp></b></kbd> | <kbd>~/Videos</kbd> | <kbd>~/Movies</kbd> | <kbd>%USERPROFILE%\Videos</kbd> | <kbd>$home/videos</kbd> |
| <kbd><b><samp>XDG_TEMPLATES_DIR</samp></b></kbd> | <kbd>~/Templates</kbd> | <kbd>~/Templates</kbd> | <kbd>%USERPROFILE%\Templates</kbd> | <kbd>$home/templates</kbd> |
| <kbd><b><samp>XDG_PUBLICSHARE_DIR</samp></b></kbd> | <kbd>~/Public</kbd> | <kbd>~/Public</kbd> | <kbd>%PUBLIC%</kbd> | <kbd>$home/public</kbd> |

#### Non-standard directories

State directory

```
Unix
• ~/.local/state
macOS
• ~/Library/Application Support
Windows
• %LOCALAPPDATA%
```
| | Unix | macOS | Windows | Plan 9 |
| :-------------------------------------------- | :------------------------ | :--------------------------------------- | :------------------------ | :------------------------- |
| <kbd><b><samp>XDG_STATE_HOME</samp></b></kbd> | <kbd>~/.local/state</kbd> | <kbd>~/Library/Application Support</kbd> | <kbd>%LOCALAPPDATA%</kbd> | <kbd>$home/lib/state</kbd> |

Application directories

```
Unix
• $XDG_DATA_HOME/applications
• ~/.local/share/applications
• /usr/local/share/applications
• /usr/share/applications
• $XDG_DATA_DIRS/applications
macOS
• /Applications
Windows
• %APPDATA%\Roaming\Microsoft\Windows\Start Menu\Programs
```
| Unix | macOS | Windows | Plan 9 |
| :--------------------------------------- | :----------------------- | :----------------------------------------------------------------- | :-------------------- |
| <kbd>$XDG_DATA_HOME/applications</kbd> | <kbd>/Applications</kbd> | <kbd>%APPDATA%\Roaming\Microsoft\Windows\Start Menu\Programs</kbd> | <kbd>$home/bin</kbd> |
| <kbd>~/.local/share/applications</kbd> | | | <kbd>/bin</kbd> |
| <kbd>/usr/local/share/applications</kbd> | | | |
| <kbd>/usr/share/applications</kbd> | | | |
| <kbd>$XDG_DATA_DIRS/applications</kbd> | | | |

Font directories

```
Unix
• $XDG_DATA_HOME/fonts
• ~/.fonts
• ~/.local/share/fonts
• /usr/local/share/fonts
• /usr/share/fonts
• $XDG_DATA_DIRS/fonts
macOS
• ~/Library/Fonts
• /Library/Fonts
• /System/Library/Fonts
• /Network/Library/Fonts
Windows
• %windir%\Fonts
• %LOCALAPPDATA%\Microsoft\Windows\Fonts
```
| Unix | macOS | Windows | Plan 9 |
| :-------------------------------- | :-------------------------------- | :------------------------------------------------ | :------------------------ |
| <kbd>$XDG_DATA_HOME/fonts</kbd> | <kbd>~/Library/Fonts</kbd> | <kbd>%windir%\Fonts</kbd> | <kbd>$home/lib/font</kbd> |
| <kbd>~/.fonts</kbd> | <kbd>/Library/Fonts</kbd> | <kbd>%LOCALAPPDATA%\Microsoft\Windows\Fonts</kbd> | <kbd>/lib/font</kbd> |
| <kbd>~/.local/share/fonts</kbd> | <kbd>/System/Library/Fonts</kbd> | | |
| <kbd>/usr/local/share/fonts</kbd> | <kbd>/Network/Library/Fonts</kbd> | | |
| <kbd>/usr/share/fonts</kbd> | | | |
| <kbd>$XDG_DATA_DIRS/fonts</kbd> | | | |

## Usage

11 changes: 11 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
coverage:
status:
project:
default:
target: 90%
threshold: 1%
patch:
default:
target: 100%
ignore:
- "paths_plan9.go"
44 changes: 44 additions & 0 deletions paths_plan9.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// +build plan9

package xdg

import (
"path/filepath"
)

func initBaseDirs(home string) {
homeLibDir := filepath.Join(home, "lib")
rootLibDir := "/lib"

// Initialize base directories.
baseDirs.dataHome = xdgPath(envDataHome, homeLibDir)
baseDirs.data = xdgPaths(envDataDirs, rootLibDir)
baseDirs.configHome = xdgPath(envConfigHome, homeLibDir)
baseDirs.config = xdgPaths(envConfigDirs, rootLibDir)
baseDirs.cacheHome = xdgPath(envCacheHome, filepath.Join(homeLibDir, "cache"))
baseDirs.runtime = xdgPath(envRuntimeDir, "/tmp")

// Initialize non-standard directories.
baseDirs.stateHome = xdgPath(envStateHome, filepath.Join(homeLibDir, "state"))

baseDirs.applications = []string{
filepath.Join(home, "bin"),
"/bin",
}

baseDirs.fonts = []string{
filepath.Join(homeLibDir, "font"),
"/lib/font",
}
}

func initUserDirs(home string) {
UserDirs.Desktop = xdgPath(envDesktopDir, filepath.Join(home, "desktop"))
UserDirs.Download = xdgPath(envDownloadDir, filepath.Join(home, "downloads"))
UserDirs.Documents = xdgPath(envDocumentsDir, filepath.Join(home, "documents"))
UserDirs.Music = xdgPath(envMusicDir, filepath.Join(home, "music"))
UserDirs.Pictures = xdgPath(envPicturesDir, filepath.Join(home, "pictures"))
UserDirs.Videos = xdgPath(envVideosDir, filepath.Join(home, "videos"))
UserDirs.Templates = xdgPath(envTemplatesDir, filepath.Join(home, "templates"))
UserDirs.PublicShare = xdgPath(envPublicShareDir, filepath.Join(home, "public"))
}
219 changes: 219 additions & 0 deletions paths_plan9_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
// +build plan9

package xdg_test

import (
"path/filepath"
"testing"

"github.com/adrg/xdg"
)

func TestDefaultBaseDirs(t *testing.T) {
home := xdg.Home

testDirs(t,
&envSample{
name: "XDG_DATA_HOME",
expected: filepath.Join(home, "lib"),
actual: &xdg.DataHome,
},
&envSample{
name: "XDG_DATA_DIRS",
expected: []string{"/lib"},
actual: &xdg.DataDirs,
},
&envSample{
name: "XDG_CONFIG_HOME",
expected: filepath.Join(home, "lib"),
actual: &xdg.ConfigHome,
},
&envSample{
name: "XDG_CONFIG_DIRS",
expected: []string{"/lib"},
actual: &xdg.ConfigDirs,
},
&envSample{
name: "XDG_CACHE_HOME",
expected: filepath.Join(home, "lib", "cache"),
actual: &xdg.CacheHome,
},
&envSample{
name: "XDG_RUNTIME_DIR",
expected: "/tmp",
actual: &xdg.RuntimeDir,
},
&envSample{
name: "XDG_STATE_HOME",
expected: filepath.Join(home, "lib", "state"),
actual: &xdg.StateHome,
},
&envSample{
name: "XDG_APPLICATION_DIRS",
expected: []string{
filepath.Join(home, "bin"),
"/bin",
},
actual: &xdg.ApplicationDirs,
},
&envSample{
name: "XDG_FONT_DIRS",
expected: []string{
filepath.Join(home, "lib", "font"),
"/lib/font",
},
actual: &xdg.FontDirs,
},
)
}

func TestCustomBaseDirs(t *testing.T) {
homeLib := filepath.Join(xdg.Home, "lib")

testDirs(t,
&envSample{
name: "XDG_DATA_HOME",
value: filepath.Join(homeLib, "data"),
expected: filepath.Join(homeLib, "data"),
actual: &xdg.DataHome,
},
&envSample{
name: "XDG_DATA_DIRS",
value: "/lib/data",
expected: []string{"/lib/data"},
actual: &xdg.DataDirs,
},
&envSample{
name: "XDG_CONFIG_HOME",
value: filepath.Join(homeLib, "config"),
expected: filepath.Join(homeLib, "config"),
actual: &xdg.ConfigHome,
},
&envSample{
name: "XDG_CONFIG_DIRS",
value: "/lib/config",
expected: []string{"/lib/config"},
actual: &xdg.ConfigDirs,
},
&envSample{
name: "XDG_CACHE_HOME",
value: homeLib,
expected: homeLib,
actual: &xdg.CacheHome,
},
&envSample{
name: "XDG_RUNTIME_DIR",
value: filepath.Join(homeLib, "runtime"),
expected: filepath.Join(homeLib, "runtime"),
actual: &xdg.RuntimeDir,
},
&envSample{
name: "XDG_STATE_HOME",
value: homeLib,
expected: homeLib,
actual: &xdg.StateHome,
},
)
}

func TestDefaultUserDirs(t *testing.T) {
home := xdg.Home

testDirs(t,
&envSample{
name: "XDG_DESKTOP_DIR",
expected: filepath.Join(home, "desktop"),
actual: &xdg.UserDirs.Desktop,
},
&envSample{
name: "XDG_DOWNLOAD_DIR",
expected: filepath.Join(home, "downloads"),
actual: &xdg.UserDirs.Download,
},
&envSample{
name: "XDG_DOCUMENTS_DIR",
expected: filepath.Join(home, "documents"),
actual: &xdg.UserDirs.Documents,
},
&envSample{
name: "XDG_MUSIC_DIR",
expected: filepath.Join(home, "music"),
actual: &xdg.UserDirs.Music,
},
&envSample{
name: "XDG_PICTURES_DIR",
expected: filepath.Join(home, "pictures"),
actual: &xdg.UserDirs.Pictures,
},
&envSample{
name: "XDG_VIDEOS_DIR",
expected: filepath.Join(home, "videos"),
actual: &xdg.UserDirs.Videos,
},
&envSample{
name: "XDG_TEMPLATES_DIR",
expected: filepath.Join(home, "templates"),
actual: &xdg.UserDirs.Templates,
},
&envSample{
name: "XDG_PUBLICSHARE_DIR",
expected: filepath.Join(home, "public"),
actual: &xdg.UserDirs.PublicShare,
},
)
}

func TestCustomUserDirs(t *testing.T) {
homeLib := filepath.Join(xdg.Home, "lib")

testDirs(t,
&envSample{
name: "XDG_DESKTOP_DIR",
value: filepath.Join(homeLib, "desktop"),
expected: filepath.Join(homeLib, "desktop"),
actual: &xdg.UserDirs.Desktop,
},
&envSample{
name: "XDG_DOWNLOAD_DIR",
value: filepath.Join(homeLib, "downloads"),
expected: filepath.Join(homeLib, "downloads"),
actual: &xdg.UserDirs.Download,
},
&envSample{
name: "XDG_DOCUMENTS_DIR",
value: filepath.Join(homeLib, "documents"),
expected: filepath.Join(homeLib, "documents"),
actual: &xdg.UserDirs.Documents,
},
&envSample{
name: "XDG_MUSIC_DIR",
value: filepath.Join(homeLib, "music"),
expected: filepath.Join(homeLib, "music"),
actual: &xdg.UserDirs.Music,
},
&envSample{
name: "XDG_PICTURES_DIR",
value: filepath.Join(homeLib, "pictures"),
expected: filepath.Join(homeLib, "pictures"),
actual: &xdg.UserDirs.Pictures,
},
&envSample{
name: "XDG_VIDEOS_DIR",
value: filepath.Join(homeLib, "videos"),
expected: filepath.Join(homeLib, "videos"),
actual: &xdg.UserDirs.Videos,
},
&envSample{
name: "XDG_TEMPLATES_DIR",
value: filepath.Join(homeLib, "templates"),
expected: filepath.Join(homeLib, "templates"),
actual: &xdg.UserDirs.Templates,
},
&envSample{
name: "XDG_PUBLICSHARE_DIR",
value: filepath.Join(homeLib, "public"),
expected: filepath.Join(homeLib, "public"),
actual: &xdg.UserDirs.PublicShare,
},
)
}