pax_global_header00006660000000000000000000000064136460370220014515gustar00rootroot0000000000000052 comment=b3dd0812c19915798badf4aa79198fdc3313fb41 golang-github-kyoh86-xdg-1.2.0/000077500000000000000000000000001364603702200161525ustar00rootroot00000000000000golang-github-kyoh86-xdg-1.2.0/.github/000077500000000000000000000000001364603702200175125ustar00rootroot00000000000000golang-github-kyoh86-xdg-1.2.0/.github/workflows/000077500000000000000000000000001364603702200215475ustar00rootroot00000000000000golang-github-kyoh86-xdg-1.2.0/.github/workflows/review.yml000066400000000000000000000004021364603702200235670ustar00rootroot00000000000000name: Review on: [pull_request] jobs: lint: runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - uses: reviewdog/action-golangci-lint@v1 with: level: info github_token: ${{ secrets.GITHUB_TOKEN }} golang-github-kyoh86-xdg-1.2.0/.github/workflows/test.yml000066400000000000000000000017201364603702200232510ustar00rootroot00000000000000name: Test on: push: branches: '*' jobs: test: runs-on: ${{ matrix.os }}-latest strategy: matrix: os: [ubuntu, windows, macos] steps: - uses: actions/setup-go@v1 with: go-version: 1.13.x - uses: actions/checkout@v1 - name: get dependencies run: go get -v -t -d ./... - name: test race run: go test -v -race ./... lint-and-cover: runs-on: ubuntu-latest steps: - uses: actions/setup-go@v1 with: go-version: 1.13.x - uses: actions/checkout@v1 - name: get dependencies run: go get -v -t -d ./... - name: vendoring run: go mod vendor # - uses: kyoh86/action-golangci-lint@v1 - name: take coverage run: go test -coverprofile=coverage.txt -covermode=count ./... - uses: codecov/codecov-action@v1.0.3 with: token: ${{ secrets.CODECOV_TOKEN }} file: coverage.txt golang-github-kyoh86-xdg-1.2.0/.gitignore000066400000000000000000000003001364603702200201330ustar00rootroot00000000000000# Binaries for programs and plugins *.exe *.exe~ *.dll *.so *.dylib # Test binary, build with `go test -c` *.test # Output of the go coverage tool, specifically when used with LiteIDE *.out golang-github-kyoh86-xdg-1.2.0/.golangci.yml000066400000000000000000000000611364603702200205330ustar00rootroot00000000000000linters: enable: - unparam - scopelint golang-github-kyoh86-xdg-1.2.0/LICENSE000066400000000000000000000020721364603702200171600ustar00rootroot00000000000000MIT License Copyright (c) 2017 kyoh86 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. golang-github-kyoh86-xdg-1.2.0/Makefile000066400000000000000000000003551364603702200176150ustar00rootroot00000000000000.PHONY: gen lint test sample VERSION := `git vertag get` COMMIT := `git rev-parse HEAD` gen: go generate ./... lint: gen golangci-lint run test: lint go test -v --race ./... sample: go run -tags=sample ./cmd/xdg-sample/main.go golang-github-kyoh86-xdg-1.2.0/README.md000066400000000000000000000011651364603702200174340ustar00rootroot00000000000000# xdg Light weight helper functions in golang to get config, data and cache files according to the XDG Base Directory Specification [![Go Report Card](https://goreportcard.com/badge/github.com/kyoh86/xdg)](https://goreportcard.com/report/github.com/kyoh86/xdg) [![Coverage Status](https://img.shields.io/codecov/c/github/kyoh86/xdg.svg)](https://codecov.io/gh/kyoh86/xdg) ## Install ``` go get github.com/kyoh86/xdg ``` # LICENSE [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg)](http://www.opensource.org/licenses/MIT) This is distributed under the [MIT License](http://www.opensource.org/licenses/MIT). golang-github-kyoh86-xdg-1.2.0/cache.go000066400000000000000000000005571364603702200175530ustar00rootroot00000000000000package xdg const ( // CacheHomeEnv is the name of the environment variable holding a cache directory path. CacheHomeEnv = "XDG_CACHE_HOME" ) // FindCacheFile finds a file from the XDG data directory. // If one cannot be found, an error `ErrNotFound` be returned. func FindCacheFile(rel ...string) (string, error) { return findFile([]string{CacheHome()}, rel) } golang-github-kyoh86-xdg-1.2.0/cache_darwin.go000066400000000000000000000003101364603702200211020ustar00rootroot00000000000000// +build darwin package xdg import ( "os" ) // CacheHome returns a XDG cache directory (XDG_CACHE_HOME). func CacheHome() string { return altHome(os.Getenv(CacheHomeEnv), "Library", "Caches") } golang-github-kyoh86-xdg-1.2.0/cache_other.go000066400000000000000000000003021364603702200207400ustar00rootroot00000000000000// +build !windows,!darwin package xdg import "os" // CacheHome returns a XDG cache directory (XDG_CACHE_HOME). func CacheHome() string { return altHome(os.Getenv(CacheHomeEnv), ".cache") } golang-github-kyoh86-xdg-1.2.0/cache_win.go000066400000000000000000000003721364603702200204230ustar00rootroot00000000000000// +build windows package xdg import ( "os" "path/filepath" ) // CacheHome returns a XDG cache directory (XDG_CACHE_HOME). func CacheHome() string { return alternate(os.Getenv(CacheHomeEnv), filepath.Join(os.Getenv("LOCALAPPDATA"), "cache")) } golang-github-kyoh86-xdg-1.2.0/cmd/000077500000000000000000000000001364603702200167155ustar00rootroot00000000000000golang-github-kyoh86-xdg-1.2.0/cmd/xdg-sample/000077500000000000000000000000001364603702200207565ustar00rootroot00000000000000golang-github-kyoh86-xdg-1.2.0/cmd/xdg-sample/main.go000066400000000000000000000031251364603702200222320ustar00rootroot00000000000000// +build sample package main import ( "fmt" "os" "strings" "github.com/kyoh86/xdg" ) func main() { fmt.Println("ConfigHome() will get a user level configuration directory path: ", xdg.ConfigHome()) os.Setenv(xdg.ConfigHomeEnv, "/.config") fmt.Println("...And `XDG_CONFIG_HOME` envar will be supported: ", xdg.ConfigHome()) fmt.Println(strings.Repeat("-", 48)) fmt.Println("ConfigDirs() will get system level configuration directories: ", xdg.ConfigDirs()) os.Setenv(xdg.ConfigDirsEnv, "/.config:/.config2") fmt.Println("...And `XDG_CONFIG_DIRS` envar will be able to change it:", xdg.ConfigDirs()) fmt.Println(strings.Repeat("-", 48)) fmt.Println("DataHome() will get a user level data directory path: ", xdg.DataHome()) os.Setenv(xdg.DataHomeEnv, "/.data") fmt.Println("...And `XDG_DATA_HOME` envar will be supported: ", xdg.DataHome()) fmt.Println(strings.Repeat("-", 48)) fmt.Println("DataDirs() will get system level data directories: ", xdg.DataDirs()) os.Setenv(xdg.DataDirsEnv, "/.data:/.data2") fmt.Println("...And `XDG_DATA_DIRS` envar will be able to change it:", xdg.DataDirs()) fmt.Println(strings.Repeat("-", 48)) fmt.Println("CacheHome() will get a cache directory path: ", xdg.CacheHome()) os.Setenv(xdg.CacheHomeEnv, "/.cache") fmt.Println("...And `XDG_CACHE_HOME` envar will be supported: ", xdg.CacheHome()) fmt.Println(strings.Repeat("-", 48)) fmt.Println("RuntimeDir() will get a runtime directory path: ", xdg.RuntimeDir()) os.Setenv(xdg.RuntimeDirEnv, "/.runtime") fmt.Println("...And `XDG_RUNTIME_DIR` envar will be supported: ", xdg.RuntimeDir()) } golang-github-kyoh86-xdg-1.2.0/config.go000066400000000000000000000014451364603702200177520ustar00rootroot00000000000000package xdg const ( // ConfigHomeEnv is the name of the environment variable holding a user configuration directory path. ConfigHomeEnv = "XDG_CONFIG_HOME" // ConfigDirsEnv is the name of the environment variable holding system configuration directory paths. ConfigDirsEnv = "XDG_CONFIG_DIRS" ) // AllConfigDirs returns all XDG configuration directories. func AllConfigDirs() []string { var dirs []string // XDG_CONFIG_HOME if home := ConfigHome(); home != "" { dirs = append(dirs, home) } // XDG_CONFIG_DIRS dirs = append(dirs, ConfigDirs()...) return dirs } // FindConfigFile finds a file from the XDG configuration directory. // If one cannot be found, an error `ErrNotFound` be returned. func FindConfigFile(rel ...string) (string, error) { return findFile(AllConfigDirs(), rel) } golang-github-kyoh86-xdg-1.2.0/config_darwin.go000066400000000000000000000010221364603702200213050ustar00rootroot00000000000000// +build darwin package xdg import ( "os" "path/filepath" ) // ConfigHome returns a user XDG configuration directory (XDG_CONFIG_HOME). func ConfigHome() string { return altHome(os.Getenv(ConfigHomeEnv), "Library", "Preferences") } // ConfigDirs returns system XDG data directories (XDG_CONFIG_DIRS). func ConfigDirs() []string { // XDG_CONFIG_DIRS xdgDirs := filepath.SplitList(os.Getenv(ConfigDirsEnv)) if len(xdgDirs) != 0 { return xdgDirs } return []string{ filepath.Join("/", "Library", "Preferences"), } } golang-github-kyoh86-xdg-1.2.0/config_other.go000066400000000000000000000010121364603702200211410ustar00rootroot00000000000000// +build !windows,!darwin package xdg import ( "os" "path/filepath" ) // ConfigHome returns a user XDG configuration directory (XDG_CONFIG_HOME). func ConfigHome() string { return altHome(os.Getenv(ConfigHomeEnv), ".config") } // ConfigDirs returns system XDG configuration directories (XDG_CONFIG_DIRS). func ConfigDirs() []string { // XDG_CONFIG_DIRS xdgDirs := filepath.SplitList(os.Getenv(ConfigDirsEnv)) if len(xdgDirs) != 0 { return xdgDirs } return []string{ filepath.Join("/", "etc", "xdg"), } } golang-github-kyoh86-xdg-1.2.0/config_win.go000066400000000000000000000010131364603702200206160ustar00rootroot00000000000000// +build windows package xdg import ( "os" "path/filepath" ) // ConfigHome returns a user XDG configuration directory (XDG_CONFIG_HOME). func ConfigHome() string { return alternate(os.Getenv(ConfigHomeEnv), os.Getenv("LOCALAPPDATA")) } // ConfigDirs returns system XDG configuration directories (XDG_CONFIG_DIRS). func ConfigDirs() []string { // XDG_CONFIG_DIRS xdgDirs := filepath.SplitList(os.Getenv(ConfigDirsEnv)) if len(xdgDirs) != 0 { return xdgDirs } return []string{ os.Getenv("PROGRAMDATA"), } } golang-github-kyoh86-xdg-1.2.0/data.go000066400000000000000000000013431364603702200174130ustar00rootroot00000000000000package xdg const ( // DataHomeEnv is the name of the environment variable holding a user data directory path. DataHomeEnv = "XDG_DATA_HOME" // DataDirsEnv is the name of the environment variable holding system data directory paths. DataDirsEnv = "XDG_DATA_DIRS" ) // AllDataDirs returns all XDG data directories. func AllDataDirs() []string { var dirs []string // XDG_DATA_HOME if home := DataHome(); home != "" { dirs = append(dirs, home) } // XDG_DATA_DIRS dirs = append(dirs, DataDirs()...) return dirs } // FindDataFile finds a file from the XDG data directory. // If one cannot be found, an error `ErrNotFound` be returned. func FindDataFile(rel ...string) (string, error) { return findFile(AllDataDirs(), rel) } golang-github-kyoh86-xdg-1.2.0/data_darwin.go000066400000000000000000000010071364603702200207540ustar00rootroot00000000000000// +build darwin package xdg import ( "os" "path/filepath" ) // DataHome returns a user XDG data directory (XDG_DATA_HOME). func DataHome() string { return altHome(os.Getenv(DataHomeEnv), "Library", "Application Support") } // DataDirs returns system XDG data directories (XDG_DATA_DIRS). func DataDirs() []string { // XDG_DATA_DIRS xdgDirs := filepath.SplitList(os.Getenv(DataDirsEnv)) if len(xdgDirs) != 0 { return xdgDirs } return []string{ filepath.Join("/", "Library", "Application Support"), } } golang-github-kyoh86-xdg-1.2.0/data_other.go000066400000000000000000000010371364603702200206140ustar00rootroot00000000000000// +build !windows,!darwin package xdg import ( "os" "path/filepath" ) // DataHome returns a user XDG data directory (XDG_DATA_HOME). func DataHome() string { return altHome(os.Getenv(DataHomeEnv), ".local", "share") } // DataDirs returns system XDG data directories (XDG_DATA_DIRS). func DataDirs() []string { // XDG_DATA_DIRS xdgDirs := filepath.SplitList(os.Getenv(DataDirsEnv)) if len(xdgDirs) != 0 { return xdgDirs } return []string{ filepath.Join("/", "usr", "local", "share"), filepath.Join("/", "usr", "share"), } } golang-github-kyoh86-xdg-1.2.0/data_win.go000066400000000000000000000010311364603702200202620ustar00rootroot00000000000000// +build windows package xdg import ( "os" "path/filepath" ) // DataHome returns a user XDG data directory (XDG_DATA_HOME). func DataHome() string { return alternate(os.Getenv(DataHomeEnv), os.Getenv("LOCALAPPDATA")) } // DataDirs returns system XDG data directories (XDG_DATA_DIRS). func DataDirs() []string { // XDG_DATA_DIRS xdgDirs := filepath.SplitList(os.Getenv(DataDirsEnv)) if len(xdgDirs) != 0 { return xdgDirs } return []string{ filepath.Join(os.Getenv("APPDATA"), "Roaming"), os.Getenv("PROGRAMDATA"), } } golang-github-kyoh86-xdg-1.2.0/doc.go000066400000000000000000000002341364603702200172450ustar00rootroot00000000000000/* Package xdg Light weight helper functions in golang to get config, data and cache files according to the XDG Base Directory Specification */ package xdg golang-github-kyoh86-xdg-1.2.0/go.mod000066400000000000000000000000461364603702200172600ustar00rootroot00000000000000module github.com/kyoh86/xdg go 1.12 golang-github-kyoh86-xdg-1.2.0/path_test.go000066400000000000000000000005471364603702200205020ustar00rootroot00000000000000package xdg import "testing" func TestPath(t *testing.T) { t.Log(CacheHome()) t.Log(DataHome()) t.Log(DataDirs()) t.Log(ConfigHome()) t.Log(ConfigDirs()) t.Log(RuntimeDir()) t.Log(DesktopDir()) t.Log(DownloadDir()) t.Log(DocumentsDir()) t.Log(MusicDir()) t.Log(PicturesDir()) t.Log(VideosDir()) t.Log(TemplatesDir()) t.Log(PublicShareDir()) } golang-github-kyoh86-xdg-1.2.0/runtime.go000066400000000000000000000005741364603702200201720ustar00rootroot00000000000000package xdg const ( // RuntimeDirEnv is the name of the environment variable holding a runtime directory path. RuntimeDirEnv = "XDG_RUNTIME_DIR" ) // FindRuntimeFile finds a file from the XDG runtime directory. // If one cannot be found, an error `ErrNotFound` be returned. func FindRuntimeFile(rel ...string) (string, error) { return findFile([]string{RuntimeDir()}, rel) } golang-github-kyoh86-xdg-1.2.0/runtime_darwin.go000066400000000000000000000003331364603702200215270ustar00rootroot00000000000000// +build darwin package xdg import ( "os" ) // RuntimeDir returns XDG runtime directory. func RuntimeDir() string { // XDG_RUNTIME_DIR return altHome(os.Getenv(RuntimeDirEnv), "Library", "Application Support") } golang-github-kyoh86-xdg-1.2.0/runtime_other.go000066400000000000000000000004471364603702200213720ustar00rootroot00000000000000// +build !windows,!darwin package xdg import ( "os" "path/filepath" "strconv" ) // RuntimeDir returns XDG runtime directory. func RuntimeDir() string { // XDG_RUNTIME_DIR return alternate( os.Getenv(RuntimeDirEnv), filepath.Join("/", "run", "user", strconv.Itoa(os.Getuid())), ) } golang-github-kyoh86-xdg-1.2.0/runtime_win.go000066400000000000000000000003271364603702200210430ustar00rootroot00000000000000// +build windows package xdg import ( "os" ) // RuntimeDir returns XDG runtime directory. func RuntimeDir() string { // XDG_RUNTIME_DIR return alternate(os.Getenv(RuntimeDirEnv), os.Getenv("LOCALAPPDATA")) } golang-github-kyoh86-xdg-1.2.0/user.go000066400000000000000000000021241364603702200174560ustar00rootroot00000000000000package xdg const ( // DesktopDirEnv is the name of the environment variable holding a user desktop directory path. DesktopDirEnv = "XDG_DESKTOP_DIR" // DownloadDirEnv is the name of the environment variable holding a user download directory path. DownloadDirEnv = "XDG_DOWNLOAD_DIR" // DocumentsDirEnv is the name of the environment variable holding a user documents directory path. DocumentsDirEnv = "XDG_DOCUMENTS_DIR" // MusicDirEnv is the name of the environment variable holding a user music directory path. MusicDirEnv = "XDG_MUSIC_DIR" // PicturesDirEnv is the name of the environment variable holding a user pictures directory path. PicturesDirEnv = "XDG_PICTURES_DIR" // VideosDirEnv is the name of the environment variable holding a user videos directory path. VideosDirEnv = "XDG_VIDEOS_DIR" // TemplatesDirEnv is the name of the environment variable holding a user templates directory path. TemplatesDirEnv = "XDG_TEMPLATES_DIR" // PublicShareDirEnv is the name of the environment variable holding a user public share directory path. PublicShareDirEnv = "XDG_PUBLICSHARE_DIR" ) golang-github-kyoh86-xdg-1.2.0/user_darwin.go000066400000000000000000000013361364603702200210260ustar00rootroot00000000000000// +build darwin package xdg import "os" func DesktopDir() string { return altHome(os.Getenv(DesktopDirEnv), "Desktop") } func DownloadDir() string { return altHome(os.Getenv(DownloadDirEnv), "Downloads") } func DocumentsDir() string { return altHome(os.Getenv(DocumentsDirEnv), "Documents") } func MusicDir() string { return altHome(os.Getenv(MusicDirEnv), "Music") } func PicturesDir() string { return altHome(os.Getenv(PicturesDirEnv), "Pictures") } func VideosDir() string { return altHome(os.Getenv(VideosDirEnv), "Movies") } func TemplatesDir() string { return altHome(os.Getenv(TemplatesDirEnv), "Templates") } func PublicShareDir() string { return altHome(os.Getenv(PublicShareDirEnv), "Public") } golang-github-kyoh86-xdg-1.2.0/user_others.go000066400000000000000000000013501364603702200210420ustar00rootroot00000000000000// +build !windows,!darwin package xdg import "os" func DesktopDir() string { return altHome(os.Getenv(DesktopDirEnv), "Desktop") } func DownloadDir() string { return altHome(os.Getenv(DownloadDirEnv), "Downloads") } func DocumentsDir() string { return altHome(os.Getenv(DocumentsDirEnv), "Documents") } func MusicDir() string { return altHome(os.Getenv(MusicDirEnv), "Music") } func PicturesDir() string { return altHome(os.Getenv(PicturesDirEnv), "Pictures") } func VideosDir() string { return altHome(os.Getenv(VideosDirEnv), "Videos") } func TemplatesDir() string { return altHome(os.Getenv(TemplatesDirEnv), "Templates") } func PublicShareDir() string { return altHome(os.Getenv(PublicShareDirEnv), "Public") } golang-github-kyoh86-xdg-1.2.0/user_win.go000066400000000000000000000014351364603702200203370ustar00rootroot00000000000000// +build windows package xdg import "os" func DesktopDir() string { return altUserProfile(os.Getenv(DesktopDirEnv), "Desktop") } func DownloadDir() string { return altUserProfile(os.Getenv(DownloadDirEnv), "Downloads") } func DocumentsDir() string { return altUserProfile(os.Getenv(DocumentsDirEnv), "Documents") } func MusicDir() string { return altUserProfile(os.Getenv(MusicDirEnv), "Music") } func PicturesDir() string { return altUserProfile(os.Getenv(PicturesDirEnv), "Pictures") } func VideosDir() string { return altUserProfile(os.Getenv(VideosDirEnv), "Videos") } func TemplatesDir() string { return altUserProfile(os.Getenv(TemplatesDirEnv), "Templates") } func PublicShareDir() string { return alternate(os.Getenv(PublicShareDirEnv), os.Getenv("PUBLIC")) } golang-github-kyoh86-xdg-1.2.0/xdg.go000066400000000000000000000021441364603702200172640ustar00rootroot00000000000000package xdg import ( "errors" "os" "path/filepath" ) // ErrNotFound indicates that a file cannot be found in any directory. var ErrNotFound = errors.New("not found") func alternate(str, alt string) string { if str == "" { return alt } return str } func altUserProfile(path string, suffix ...string) string { if path != "" { return path } home := os.Getenv("USERPROFILE") if home != "" { return filepath.Join(append([]string{home}, suffix...)...) } return "" } func altHome(path string, suffix ...string) string { if path != "" { return path } home := os.Getenv("HOME") if home != "" { return filepath.Join(append([]string{home}, suffix...)...) } return "" } func findFile(paths []string, rel []string) (string, error) { for _, dir := range paths { if !filepath.IsAbs(dir) { // XDG Base Directory Specification supports only Absolute paths continue } fpath := filepath.Join(append([]string{dir}, rel...)...) if isFile(fpath) { return fpath, nil } } return "", ErrNotFound } func isFile(p string) bool { stat, err := os.Stat(p) return err == nil && !stat.IsDir() }