Skip to content

Commit

Permalink
[v3] Added AppDataDirectory path (#3823)
Browse files Browse the repository at this point in the history
* Added appData

* Updated changelog

* Updated doc and added error handling and fallbacks

* Updated doc

* Expose XDG methods

* Refactor Path method. Add Paths method

* Fix changelog

---------

Co-authored-by: Anshuman <anshuman@instasafe.com>
Co-authored-by: Lea Anthony <lea.anthony@gmail.com>
  • Loading branch information
3 people authored Nov 16, 2024
1 parent 0b1ef79 commit 331097f
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 5 deletions.
51 changes: 51 additions & 0 deletions mkdocs-website/docs/en/API/application.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,57 @@ API: `Show()`
app.Show()
```

### Path

API: `Path(selector Path) string`

`Path(selector Path)` returns the full path for the given path type. It provides a cross-platform way to query common application directories.

The `Path` type is an enum with the following values:
- `PathHome`: Returns the user's home directory
- `PathDataHome`: Returns the path to the user's data directory
- `PathConfigHome`: Returns the path to the user's configuration directory
- `PathStateHome`: Returns the path to the user's state directory
- `PathCacheHome`: Returns the path to the user's cache directory
- `PathRuntimeDir`: Returns the path to the user's runtime directory
- `PathDesktop`: Returns the path to the user's desktop directory
- `PathDownload`: Returns the path to the user's download directory
- `PathDocuments`: Returns the path to the user's documents directory
- `PathMusic`: Returns the path to the user's music directory
- `PathPictures`: Returns the path to the user's pictures directory
- `PathVideos`: Returns the path to the user's videos directory
- `PathTemplates`: Returns the path to the user's templates directory
- `PathPublicShare`: Returns the path to the user's public share directory

```go
// Get the data home directory path
dataHomePath := app.Path(application.PathDataHome)
fmt.Println("DataHome path:", dataHomePath)

// Output: DataHome path: /home/username/.local/share // Linux
// Output: DataHome path: /Users/username/Library/Application Support // macOS
// Output: DataHome path: C:\Users\Username\AppData\Roaming // Windows

// Get the CacheHome directory path
cacheHomePath := app.Path(application.CacheHome)
fmt.Println("CacheHome path:", cacheHomePath)

// Output: CacheHome path: /home/username/.cache // Linux
// Output: CacheHome path: /Users/username/Library/Caches // macOS
// Output: CacheHome path: C:\Users\Username\AppData\Local\Temp // Windows
```

## Paths
API: `Paths(selector Paths) []string`
`Paths(selector Path)` returns a list of paths for the given path type. It provides a cross-platform way to query common directory paths.

The `Paths` type is an enum with the following values:
- `PathsDataDirs`: Returns the list of data directories
- `PathsConfigDirs`: Returns the list of configuration directories
- `PathsCacheDirs`: Returns the list of cache directories
- `PathsRuntimeDirs`: Returns the list of runtime directories


--8<--
./docs/en/API/application_window.md
./docs/en/API/application_menu.md
Expand Down
1 change: 1 addition & 0 deletions mkdocs-website/docs/en/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- File Association support by [leaanthony](https://github.com/leaanthony) in [#3873](https://github.com/wailsapp/wails/pull/3873)
- New `wails3 generate runtime` command by [leaanthony](https://github.com/leaanthony)
- New `InitialPosition` option to specify if the window should be centered or positioned at the given X/Y location by [leaanthony](https://github.com/leaanthony) in [#3885](https://github.com/wailsapp/wails/pull/3885)
- Add `Path` & `Paths` methods to `application` package by [ansxuman](https://github.com/ansxuman) and [leaanthony](https://github.com/leaanthony) in [#3823](https://github.com/wailsapp/wails/pull/3823)

### Changed
- Taskfile refactor by [leaanthony](https://github.com/leaanthony) in [#3748](https://github.com/wailsapp/wails/pull/3748)
Expand Down
3 changes: 2 additions & 1 deletion v3/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/wailsapp/wails/v3
go 1.22.4

require (
github.com/adrg/xdg v0.5.0
github.com/atterpac/refresh v0.8.3
github.com/bep/debounce v1.2.1
github.com/ebitengine/purego v0.4.0-alpha.4
Expand Down Expand Up @@ -31,7 +32,7 @@ require (
github.com/tc-hib/winres v0.3.1
github.com/wailsapp/go-webview2 v1.0.16
github.com/wailsapp/mimetype v1.4.1
golang.org/x/sys v0.20.0
golang.org/x/sys v0.22.0
golang.org/x/term v0.20.0
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d
gopkg.in/yaml.v3 v3.0.1
Expand Down
10 changes: 6 additions & 4 deletions v3/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 h1:kkhsdkhsCv
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0=
github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA=
github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8=
github.com/adrg/xdg v0.5.0 h1:dDaZvhMXatArP1NPHhnfaQUqWBLBsmx1h1HXQdMoFCY=
github.com/adrg/xdg v0.5.0/go.mod h1:dDdY4M4DF9Rjy4kHPeNL+ilVF+p2lK8IdM9/rTSGcI4=
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
Expand Down Expand Up @@ -213,8 +215,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tc-hib/winres v0.3.1 h1:CwRjEGrKdbi5CvZ4ID+iyVhgyfatxFoizjPhzez9Io4=
github.com/tc-hib/winres v0.3.1/go.mod h1:C/JaNhH3KBvhNKVbvdlDWkbMDO9H4fKKDaN7/07SSuk=
github.com/wailsapp/go-webview2 v1.0.16 h1:wffnvnkkLvhRex/aOrA3R7FP7rkvOqL/bir1br7BekU=
Expand Down Expand Up @@ -284,8 +286,8 @@ golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210615171337-6886f2dfbf5b/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down
10 changes: 10 additions & 0 deletions v3/pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -1013,3 +1013,13 @@ func (a *App) shouldQuit() bool {
}
return true
}

// Path returns the path for the given selector
func (a *App) Path(selector Path) string {
return paths[selector]
}

// Paths returns the paths for the given selector
func (a *App) Paths(selector Paths) []string {
return pathdirs[selector]
}
123 changes: 123 additions & 0 deletions v3/pkg/application/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package application

import "github.com/adrg/xdg"

type Path int

const (
// PathHome is the user's home directory.
PathHome Path = iota

// PathDataHome defines the base directory relative to which user-specific
// data files should be stored. This directory is defined by the
// $XDG_DATA_HOME environment variable. If the variable is not set,
// a default equal to $HOME/.local/share should be used.
PathDataHome

// PathConfigHome defines the base directory relative to which user-specific
// configuration files should be written. This directory is defined by
// the $XDG_CONFIG_HOME environment variable. If the variable is
// not set, a default equal to $HOME/.config should be used.
PathConfigHome

// PathStateHome defines the base directory relative to which user-specific
// state files should be stored. This directory is defined by the
// $XDG_STATE_HOME environment variable. If the variable is not set,
// a default equal to ~/.local/state should be used.
PathStateHome

// PathCacheHome defines the base directory relative to which user-specific
// non-essential (cached) data should be written. This directory is
// defined by the $XDG_CACHE_HOME environment variable. If the variable
// is not set, a default equal to $HOME/.cache should be used.
PathCacheHome

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

// PathDesktop defines the location of the user's desktop directory.
PathDesktop

// PathDownload defines a suitable location for user downloaded files.
PathDownload

// PathDocuments defines a suitable location for user document files.
PathDocuments

// PathMusic defines a suitable location for user audio files.
PathMusic

// PathPictures defines a suitable location for user image files.
PathPictures

// PathVideos defines a suitable location for user video files.
PathVideos

// PathTemplates defines a suitable location for user template files.
PathTemplates

// PathPublicShare defines a suitable location for user shared files.
PathPublicShare
)

var paths = map[Path]string{
PathHome: xdg.Home,
PathDataHome: xdg.DataHome,
PathConfigHome: xdg.ConfigHome,
PathStateHome: xdg.StateHome,
PathCacheHome: xdg.CacheHome,
PathRuntimeDir: xdg.RuntimeDir,
PathDesktop: xdg.UserDirs.Desktop,
PathDownload: xdg.UserDirs.Download,
PathDocuments: xdg.UserDirs.Documents,
PathMusic: xdg.UserDirs.Music,
PathPictures: xdg.UserDirs.Pictures,
PathVideos: xdg.UserDirs.Videos,
PathTemplates: xdg.UserDirs.Templates,
PathPublicShare: xdg.UserDirs.PublicShare,
}

type Paths int

const (
// PathsDataDirs defines the preference-ordered set of base directories to
// search for data files in addition to the DataHome base directory.
// This set of directories is defined by the $XDG_DATA_DIRS environment
// variable. If the variable is not set, the default directories
// to be used are /usr/local/share and /usr/share, in that order. The
// DataHome directory is considered more important than any of the
// directories defined by DataDirs. Therefore, user data files should be
// written relative to the DataHome directory, if possible.
PathsDataDirs = iota

// PathsConfigDirs defines the preference-ordered set of base directories
// search for configuration files in addition to the ConfigHome base
// directory. This set of directories is defined by the $XDG_CONFIG_DIRS
// environment variable. If the variable is not set, a default equal
// to /etc/xdg should be used. The ConfigHome directory is considered
// more important than any of the directories defined by ConfigDirs.
// Therefore, user config files should be written relative to the
// ConfigHome directory, if possible.
PathsConfigDirs

// PathsFontDirs defines the common locations where font files are stored.
PathsFontDirs

// PathsApplicationDirs defines the common locations of applications.
PathsApplicationDirs
)

var pathdirs = map[Paths][]string{
PathsDataDirs: xdg.DataDirs,
PathsConfigDirs: xdg.ConfigDirs,
PathsFontDirs: xdg.FontDirs,
PathsApplicationDirs: xdg.ApplicationDirs,
}

0 comments on commit 331097f

Please sign in to comment.