pax_global_header00006660000000000000000000000064140501572630014515gustar00rootroot0000000000000052 comment=5a2e03f8263afeb4a5d053b2bbf8bf714ccde1b9 go-xdg-6.0.0/000077500000000000000000000000001405015726300127055ustar00rootroot00000000000000go-xdg-6.0.0/.github/000077500000000000000000000000001405015726300142455ustar00rootroot00000000000000go-xdg-6.0.0/.github/workflows/000077500000000000000000000000001405015726300163025ustar00rootroot00000000000000go-xdg-6.0.0/.github/workflows/main.yml000066400000000000000000000012531405015726300177520ustar00rootroot00000000000000name: Test on: pull_request: push: branches: - master tags: - v* jobs: test: runs-on: ubuntu-latest steps: - name: Set up Go uses: actions/setup-go@v2 with: go-version: 1.16.0 - name: Cache Go modules uses: actions/cache@v2 with: path: ~/go/pkg/mod key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-go- - name: Checkout uses: actions/checkout@v2 - name: Build run: go build ./... - name: Test run: go test ./... - name: Lint uses: golangci/golangci-lint-action@v2 with: version: v1.40.1 go-xdg-6.0.0/.golangci.yml000066400000000000000000000023451405015726300152750ustar00rootroot00000000000000linters: enable: - asciicheck - bodyclose - deadcode - depguard - dogsled - dupl - durationcheck - errcheck - errorlint - exhaustive - exportloopref - forbidigo - forcetypeassert - gochecknoglobals - gocognit - goconst - gocritic - gocyclo - godot - godox - goerr113 - gofmt - gofumpt - goheader - goimports - gomoddirectives - gomodguard - goprintffuncname - gosec - gosimple - govet - ifshort - importas - ineffassign - makezero - misspell - nakedret - nilerr - noctx - nolintlint - prealloc - predeclared - promlinter - revive - rowserrcheck - sqlclosecheck - staticcheck - structcheck - stylecheck - tagliatelle - thelper - typecheck - unconvert - unparam - unused - varcheck - wastedassign - whitespace disable: - cyclop - exhaustivestruct - funlen - gci - gochecknoinits - gomnd - lll - nestif - nlreturn - paralleltest - testpackage - tparallel - wrapcheck - wsl linters-settings: goimports: local-prefixes: github.com/twpayne/go-xdg misspell: locale: US issues: exclude-rules: - linters: - goerr113 text: "do not define dynamic errors, use wrapped static errors instead" go-xdg-6.0.0/LICENSE000066400000000000000000000020641405015726300137140ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2018 Tom Payne 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. go-xdg-6.0.0/README.md000066400000000000000000000003451405015726300141660ustar00rootroot00000000000000# go-xdg [![PkgGoDev](https://pkg.go.dev/badge/github.com/twpayne/go-xdg)](https://pkg.go.dev/github.com/twpayne/go-xdg) Package `xdg` provides functions related to [freedesktop.org](https://freedesktop.org/). ## License MIT go-xdg-6.0.0/basedirectoryspecification.go000066400000000000000000000071511405015726300206400ustar00rootroot00000000000000package xdg import ( "errors" "io/fs" "os" "path/filepath" "strings" ) // A BaseDirectorySpecification represents an XDG Base Directory Specification // configuration. See // https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html. type BaseDirectorySpecification struct { ConfigHome string ConfigDirs []string DataHome string DataDirs []string CacheHome string RuntimeDir string } // A GetenvFunc is a function that gets an environment variable, like os.Getenv. type GetenvFunc func(string) string // NewBaseDirectorySpecification returns a new BaseDirectorySpecification, // configured from the user's environment variables. func NewBaseDirectorySpecification() (*BaseDirectorySpecification, error) { homeDir, err := os.UserHomeDir() if err != nil { return nil, err } return NewTestBaseDirectorySpecification(homeDir, os.Getenv), nil } // NewTestBaseDirectorySpecification returns a new BaseDirectorySpecification // using homeDir and the getenv function. getenv can be nil, in which case // default values are returned. func NewTestBaseDirectorySpecification(homeDir string, getenv GetenvFunc) *BaseDirectorySpecification { if getenv == nil { getenv = func(string) string { return "" } } defaultConfigHome := filepath.Join(homeDir, ".config") configHome := firstNonEmpty(getenv("XDG_CONFIG_HOME"), defaultConfigHome) defaultConfigDirs := filepath.Join("/", "etc", "xdg") configDirs := append([]string{configHome}, filepath.SplitList(firstNonEmpty(getenv("XDG_CONFIG_DIRS"), defaultConfigDirs))...) defaultDataHome := filepath.Join(homeDir, ".local", "share") dataHome := firstNonEmpty(getenv("XDG_DATA_HOME"), defaultDataHome) defaultDataDirs := strings.Join([]string{ filepath.Join("/", "usr", "local", "share"), filepath.Join("/", "usr", "share"), }, string(filepath.ListSeparator)) dataDirs := append([]string{dataHome}, filepath.SplitList(firstNonEmpty(getenv("XDG_DATA_DIRS"), defaultDataDirs))...) defaultCacheHome := filepath.Join(homeDir, ".cache") cacheHome := firstNonEmpty(getenv("XDG_CACHE_HOME"), defaultCacheHome) runtimeDir := getenv("XDG_RUNTIME_DIR") return &BaseDirectorySpecification{ ConfigHome: configHome, ConfigDirs: configDirs, DataHome: dataHome, DataDirs: dataDirs, CacheHome: cacheHome, RuntimeDir: runtimeDir, } } // OpenConfigFile opens the first configuration file with the given name found, // its full path, and any error. If no file can be found, the error will be // fs.ErrNotExist. // // The file is opened with the open argument. If open is nil, os.Open is used. func (b *BaseDirectorySpecification) OpenConfigFile(fsys fs.FS, nameComponents ...string) (fs.File, string, error) { return openFile(fsys, nameComponents, b.ConfigDirs) } // OpenDataFile opens the first data file with the given name found, its full // path, and any error. If no file can be found, the error will be // fs.ErrNotExist. // // The file is opened with the open argument. If open is nil, os.Open is used. func (b *BaseDirectorySpecification) OpenDataFile(fsys fs.FS, nameComponents ...string) (fs.File, string, error) { return openFile(fsys, nameComponents, b.DataDirs) } func firstNonEmpty(ss ...string) string { for _, s := range ss { if s != "" { return s } } return "" } func openFile(fsys fs.FS, nameComponents, dirs []string) (fs.File, string, error) { for _, dir := range dirs { path := filepath.Join(append([]string{dir}, nameComponents...)...) f, err := fsys.Open(path) switch { case err == nil: return f, path, nil case errors.Is(err, fs.ErrNotExist): continue default: return nil, path, err } } return nil, "", fs.ErrNotExist } go-xdg-6.0.0/basedirectoryspecification_test.go000066400000000000000000000162561405015726300217050ustar00rootroot00000000000000package xdg import ( "io/fs" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/twpayne/go-vfs/v3/vfst" ) func TestNewBaseDirectorySpecification(t *testing.T) { for _, tc := range []struct { name string getenv GetenvFunc expected *BaseDirectorySpecification }{ { name: "default", expected: &BaseDirectorySpecification{ ConfigHome: "/home/user/.config", ConfigDirs: []string{"/home/user/.config", "/etc/xdg"}, DataHome: "/home/user/.local/share", DataDirs: []string{"/home/user/.local/share", "/usr/local/share", "/usr/share"}, CacheHome: "/home/user/.cache", RuntimeDir: "", }, }, { name: "config_home", getenv: makeGetenvFunc(map[string]string{ "XDG_CONFIG_HOME": "/my/user/config", }), expected: &BaseDirectorySpecification{ ConfigHome: "/my/user/config", ConfigDirs: []string{"/my/user/config", "/etc/xdg"}, DataHome: "/home/user/.local/share", DataDirs: []string{"/home/user/.local/share", "/usr/local/share", "/usr/share"}, CacheHome: "/home/user/.cache", RuntimeDir: "", }, }, { name: "config_dirs", getenv: makeGetenvFunc(map[string]string{ "XDG_CONFIG_DIRS": "/config/dir/1:/config/dir/2", }), expected: &BaseDirectorySpecification{ ConfigHome: "/home/user/.config", ConfigDirs: []string{"/home/user/.config", "/config/dir/1", "/config/dir/2"}, DataHome: "/home/user/.local/share", DataDirs: []string{"/home/user/.local/share", "/usr/local/share", "/usr/share"}, CacheHome: "/home/user/.cache", RuntimeDir: "", }, }, { name: "data_home", getenv: makeGetenvFunc(map[string]string{ "XDG_DATA_HOME": "/my/user/data", }), expected: &BaseDirectorySpecification{ ConfigHome: "/home/user/.config", ConfigDirs: []string{"/home/user/.config", "/etc/xdg"}, DataHome: "/my/user/data", DataDirs: []string{"/my/user/data", "/usr/local/share", "/usr/share"}, CacheHome: "/home/user/.cache", RuntimeDir: "", }, }, { name: "data_dirs", getenv: makeGetenvFunc(map[string]string{ "XDG_DATA_DIRS": "/data/dir/1:/data/dir/2", }), expected: &BaseDirectorySpecification{ ConfigHome: "/home/user/.config", ConfigDirs: []string{"/home/user/.config", "/etc/xdg"}, DataHome: "/home/user/.local/share", DataDirs: []string{"/home/user/.local/share", "/data/dir/1", "/data/dir/2"}, CacheHome: "/home/user/.cache", RuntimeDir: "", }, }, { name: "cache_home", getenv: makeGetenvFunc(map[string]string{ "XDG_CACHE_HOME": "/my/user/cache", }), expected: &BaseDirectorySpecification{ ConfigHome: "/home/user/.config", ConfigDirs: []string{"/home/user/.config", "/etc/xdg"}, DataHome: "/home/user/.local/share", DataDirs: []string{"/home/user/.local/share", "/usr/local/share", "/usr/share"}, CacheHome: "/my/user/cache", RuntimeDir: "", }, }, { name: "runtime_dir", getenv: makeGetenvFunc(map[string]string{ "XDG_RUNTIME_DIR": "/my/user/runtime", }), expected: &BaseDirectorySpecification{ ConfigHome: "/home/user/.config", ConfigDirs: []string{"/home/user/.config", "/etc/xdg"}, DataHome: "/home/user/.local/share", DataDirs: []string{"/home/user/.local/share", "/usr/local/share", "/usr/share"}, CacheHome: "/home/user/.cache", RuntimeDir: "/my/user/runtime", }, }, { name: "all", getenv: makeGetenvFunc(map[string]string{ "XDG_CONFIG_HOME": "/my/user/config", "XDG_CONFIG_DIRS": "/config/dir/1:/config/dir/2", "XDG_DATA_HOME": "/my/user/data", "XDG_DATA_DIRS": "/data/dir/1:/data/dir/2", "XDG_CACHE_HOME": "/my/user/cache", "XDG_RUNTIME_DIR": "/my/user/runtime", }), expected: &BaseDirectorySpecification{ ConfigHome: "/my/user/config", ConfigDirs: []string{"/my/user/config", "/config/dir/1", "/config/dir/2"}, DataHome: "/my/user/data", DataDirs: []string{"/my/user/data", "/data/dir/1", "/data/dir/2"}, CacheHome: "/my/user/cache", RuntimeDir: "/my/user/runtime", }, }, } { t.Run(tc.name, func(t *testing.T) { actual := NewTestBaseDirectorySpecification("/home/user", tc.getenv) assert.Equal(t, tc.expected, actual) }) } } func TestOpenConfigFile(t *testing.T) { for _, tc := range []struct { name string root interface{} expectedName string expectedErr error }{ { name: "first_dir", root: map[string]interface{}{ "/home/user/.config/go-xdg.conf": "# contents of first go-xdg.conf\n", "/etc/xdg/go-xdg.conf": "# contents of second go-xdg.conf\n", }, expectedName: "/home/user/.config/go-xdg.conf", }, { name: "second_dir", root: map[string]interface{}{ "/etc/xdg/go-xdg.conf": "# contents of second go-xdg.conf\n", }, expectedName: "/etc/xdg/go-xdg.conf", }, { name: "not_found", root: map[string]interface{}{}, expectedErr: fs.ErrNotExist, }, } { t.Run(tc.name, func(t *testing.T) { xdg := NewTestBaseDirectorySpecification("/home/user", nil) fileSystem, cleanup, err := vfst.NewTestFS(tc.root) defer cleanup() require.NoError(t, err) actualFile, actualName, err := xdg.OpenConfigFile(fileSystem, "go-xdg.conf") if err == nil { defer func() { assert.NoError(t, actualFile.Close()) }() } if tc.expectedErr == nil { assert.NoError(t, err) assert.Equal(t, tc.expectedName, actualName) } else { assert.Equal(t, tc.expectedErr, err) } }) } } func TestOpenDataFile(t *testing.T) { for _, tc := range []struct { name string root interface{} expectedName string expectedErr error }{ { name: "first_dir", root: map[string]interface{}{ "/home/user/.local/share/go-xdg.dat": "# contents of first go-xdg.dat\n", "/usr/local/share/go-xdg.dat": "# contents of second go-xdg.dat\n", "/usr/share/go-xdg.dat": "# contents of third go-xdg.dat\n", }, expectedName: "/home/user/.local/share/go-xdg.dat", }, { name: "second_dir", root: map[string]interface{}{ "/usr/local/share/go-xdg.dat": "# contents of second go-xdg.dat\n", "/usr/share/go-xdg.dat": "# contents of third go-xdg.dat\n", }, expectedName: "/usr/local/share/go-xdg.dat", }, { name: "third_dir", root: map[string]interface{}{ "/usr/share/go-xdg.dat": "# contents of third go-xdg.dat\n", }, expectedName: "/usr/share/go-xdg.dat", }, { name: "not_found", root: map[string]interface{}{}, expectedErr: fs.ErrNotExist, }, } { t.Run(tc.name, func(t *testing.T) { xdg := NewTestBaseDirectorySpecification("/home/user", nil) fileSystem, cleanup, err := vfst.NewTestFS(tc.root) defer cleanup() require.NoError(t, err) actualFile, actualName, err := xdg.OpenDataFile(fileSystem, "go-xdg.dat") if err == nil { defer func() { assert.NoError(t, actualFile.Close()) }() } if tc.expectedErr == nil { assert.NoError(t, err) assert.Equal(t, tc.expectedName, actualName) } else { assert.Equal(t, tc.expectedErr, err) } }) } } func makeGetenvFunc(env map[string]string) GetenvFunc { return func(key string) string { return env[key] } } go-xdg-6.0.0/go.mod000066400000000000000000000002031405015726300140060ustar00rootroot00000000000000module github.com/twpayne/go-xdg/v6 go 1.16 require ( github.com/stretchr/testify v1.7.0 github.com/twpayne/go-vfs/v3 v3.0.0 ) go-xdg-6.0.0/go.sum000066400000000000000000000031021405015726300140340ustar00rootroot00000000000000github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/twpayne/go-vfs/v3 v3.0.0 h1:rMFBISZVhSowKeX1BxL8utM64V7CuUw9/rfekPdS7to= github.com/twpayne/go-vfs/v3 v3.0.0/go.mod h1:JKfJtOC57Wqo7xYxFRKSqdXwyj3AhfyRrDxSE/XiAVA= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c h1:VwygUrnw9jn88c4u8GD3rZQbqrP/tgas88tPUbBxQrk= golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= go-xdg-6.0.0/open.go000066400000000000000000000002361405015726300141760ustar00rootroot00000000000000package xdg import "os/exec" // Open opens fileOrURL with xdg-open. func Open(fileOrURL string) error { return exec.Command("xdg-open", fileOrURL).Run() } go-xdg-6.0.0/settings.go000066400000000000000000000031421405015726300150740ustar00rootroot00000000000000package xdg import ( "fmt" "os/exec" "strings" ) const ( settingsCmdName = "xdg-settings" // DefaultURLSchemeHandlerProperty is the default URL scheme handler property. DefaultURLSchemeHandlerProperty = "default-url-scheme-handler" // DefaultWebBrowserProperty is the default web browser property. DefaultWebBrowserProperty = "default-web-browser" ) // A Setting is a setting. type Setting struct { Property string SubProperty string } // Check checks that value of s is value. See // https://portland.freedesktop.org/doc/xdg-settings.html. func (s Setting) Check(value string) (bool, error) { args := []string{"check", s.Property} if s.SubProperty != "" { args = append(args, s.SubProperty) } args = append(args, value) output, err := exec.Command(settingsCmdName, args...).Output() if err != nil { return false, err } o := strings.TrimSpace(string(output)) switch o { case "yes": return true, nil case "no": return false, nil default: return false, fmt.Errorf(`%s %s: expected "yes" or "no", got %q`, settingsCmdName, strings.Join(args, " "), o) } } // Get gets the value of s. func (s Setting) Get() (string, error) { args := []string{"get", s.Property} if s.SubProperty != "" { args = append(args, s.SubProperty) } output, err := exec.Command(settingsCmdName, args...).Output() return strings.TrimSpace(string(output)), err } // Set sets s to value. func (s Setting) Set(value string) error { args := []string{"set", s.Property} if s.SubProperty != "" { args = append(args, s.SubProperty) } args = append(args, value) return exec.Command(settingsCmdName, args...).Run() } go-xdg-6.0.0/settings_example_test.go000066400000000000000000000013241405015726300176460ustar00rootroot00000000000000package xdg_test import ( "fmt" xdg "github.com/twpayne/go-xdg/v6" ) func ExampleSetting_Check() { setting := xdg.Setting{ Property: xdg.DefaultWebBrowserProperty, } isGoogleChrome, err := setting.Check("google-chrome.desktop") if err != nil { panic(err) } fmt.Println(isGoogleChrome) } func ExampleSetting_Get() { setting := xdg.Setting{ Property: xdg.DefaultURLSchemeHandlerProperty, SubProperty: "http", } value, err := setting.Get() if err != nil { panic(err) } fmt.Println(value) } func ExampleSetting_Set() { setting := xdg.Setting{ Property: xdg.DefaultURLSchemeHandlerProperty, SubProperty: "http", } if err := setting.Set("firefox.desktop"); err != nil { panic(err) } } go-xdg-6.0.0/xdg.go000066400000000000000000000001121405015726300140100ustar00rootroot00000000000000// Package xdg provides functions related to freedesktop.org. package xdg