Documentation
¶
Index ¶
- type Config
- func (c Config) CheckPermission(fileName string) (bool, error)
- func (c Config) Create(fileName string) (*os.File, error)
- func (c Config) CreateParentDir(fileName string) error
- func (c Config) Exists(fileName string) bool
- func (c Config) FS() fs.FS
- func (c Config) FixPermission(fileName string) error
- func (c Config) MkdirAll() error
- func (c Config) Open(fileName string) (*os.File, error)
- func (c Config) ReadFile(fileName string) ([]byte, error)
- func (c Config) WriteFile(fileName string, data []byte) error
- type ConfigDir
- func (c ConfigDir) FS() fs.FS
- func (c ConfigDir) QueryCacheFolder() *Config
- func (c ConfigDir) QueryFolderContainsFile(fileName string) *Config
- func (c ConfigDir) QueryFolders(configType ConfigType) []*Config
- func (c ConfigDir) QueryResourceFolders() []*Config
- func (c ConfigDir) ResourceFS(fallbacks ...fs.FS) fs.FS
- type ConfigType
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Path string
Type ConfigType
// FileMode is the permission of files created by Create and WriteFile (default: 0644).
// If it is set, the permission of existing files is also changed when writing.
FileMode fs.FileMode
// DirMode is the permission of folders created by this package (default: 0755).
DirMode fs.FileMode
}
Config represents each folder
func (Config) CheckPermission ¶
CheckPermission reports whether fileName and folders between c.Path and fileName have the permission of FileMode and DirMode.
func (Config) CreateParentDir ¶
CreateParentDir creates the parent directory of fileName inside c. fileName is a relative path inside c, containing zero or more path separators.
func (Config) FixPermission ¶
FixPermission changes the permission of fileName and folders between c.Path and fileName to FileMode and DirMode.
type ConfigDir ¶
type ConfigDir struct {
VendorName string
ApplicationName string
LocalPath string
// ResourcePath is a resource folder that has the highest priority (e.g. for development
// with go run). See QueryResourceFolders.
ResourcePath string
// XDG uses XDG Base Directory Specification on macOS too
// (${XDG_CONFIG_HOME}, ${XDG_CONFIG_DIRS}, ${XDG_CACHE_HOME} and ${XDG_DATA_DIRS}).
// It is always used on Linux/BSDs and ignored on Windows.
XDG bool
// FileMode and DirMode are passed to Config. See Config.
FileMode fs.FileMode
DirMode fs.FileMode
}
ConfigDir keeps setting for querying folders.
Example ¶
Sample for reading configuration
package main
import (
"encoding/json"
"path/filepath"
"github.com/shibukawa/configdir"
)
type Config struct {
UserName string `json:"user-name"`
}
var DefaultConfig = Config{
UserName: "baron",
}
func main() {
var config Config
configDirs := configdir.New("vendor-name", "application-name")
// optional: local path has the highest priority
configDirs.LocalPath, _ = filepath.Abs(".")
folder := configDirs.QueryFolderContainsFile("setting.json")
if folder != nil {
data, _ := folder.ReadFile("setting.json")
json.Unmarshal(data, &config)
} else {
config = DefaultConfig
}
}
Output:
func (ConfigDir) FS ¶
FS returns a read-only fs.FS that looks up files in the same order as QueryFolderContainsFile (local, user, system). Each name is resolved in the first folder that contains it; directory listings are not merged.
Example ¶
Sample for reading configuration via fs.FS
package main
import (
"encoding/json"
"io/fs"
"path/filepath"
"github.com/shibukawa/configdir"
)
type Config struct {
UserName string `json:"user-name"`
}
var DefaultConfig = Config{
UserName: "baron",
}
func main() {
var config Config
configDirs := configdir.New("vendor-name", "application-name")
configDirs.LocalPath, _ = filepath.Abs(".")
data, err := fs.ReadFile(configDirs.FS(), "setting.json")
if err == nil {
json.Unmarshal(data, &config)
} else {
config = DefaultConfig
}
}
Output:
func (ConfigDir) QueryCacheFolder ¶
Example ¶
Sample for getting cache folder
package main
import (
"io"
"log"
"net/http"
"github.com/shibukawa/configdir"
)
func main() {
configDirs := configdir.New("vendor-name", "application-name")
cache := configDirs.QueryCacheFolder()
resp, err := http.Get("http://examples.com/sdk.zip")
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
cache.WriteFile("sdk.zip", body)
}
Output:
func (ConfigDir) QueryFolderContainsFile ¶
func (ConfigDir) QueryFolders ¶
func (c ConfigDir) QueryFolders(configType ConfigType) []*Config
QueryFolders returns folders in priority order (local, global, confined, system).
Global returns the global folder and the confined folder (if running in a sandbox like snap). Existing returns folders that exist and are accessible.
Example ¶
Sample for reading configuration
package main
import (
"encoding/json"
"github.com/shibukawa/configdir"
)
type Config struct {
UserName string `json:"user-name"`
}
func main() {
configDirs := configdir.New("vendor-name", "application-name")
var config Config
data, _ := json.Marshal(&config)
// Stores to local folder
folders := configDirs.QueryFolders(configdir.Local)
folders[0].WriteFile("setting.json", data)
// Stores to user folder
folders = configDirs.QueryFolders(configdir.Global)
folders[0].WriteFile("setting.json", data)
// Stores to system folder
folders = configDirs.QueryFolders(configdir.System)
folders[0].WriteFile("setting.json", data)
}
Output:
func (ConfigDir) QueryResourceFolders ¶ added in v1.1.0
QueryResourceFolders returns existing folders that contain files installed with the application (e.g. default settings, templates and assets) in priority order:
- ResourcePath (if set)
- The folder relative to the executable (symbolic links are resolved)
- ${XDG_DATA_DIRS}/<ApplicationName> (/usr/local/share and /usr/share by default) on Linux/BSDs, and on macOS when XDG is true
The folder relative to the executable is:
- macOS app bundle: X.app/Contents/MacOS/app → X.app/Contents/Resources
- Windows: the folder of app.exe
- Others: <prefix>/bin/app → <prefix>/share/<ApplicationName> (/usr, /usr/local, Homebrew, snap, Flatpak, AppImage and so on)
Unlike other folders, VendorName is not used because packages are installed to <prefix>/share/<package name> by convention. The folders are read-only. Don't write files to them.
func (ConfigDir) ResourceFS ¶ added in v1.1.0
ResourceFS returns a read-only fs.FS that resolves each name in the order of QueryResourceFolders, and then fallbacks (e.g. embed.FS bundled in the executable).
Example ¶
Sample for reading files installed with the application
package main
import (
"io/fs"
"os"
"testing/fstest"
"github.com/shibukawa/configdir"
)
func main() {
configDirs := configdir.New("vendor-name", "application-name")
// optional: highest priority (e.g. for go run)
configDirs.ResourcePath = os.Getenv("APP_RESOURCE_PATH")
// embedded files can be passed as fallback (e.g. embed.FS)
embedded := fstest.MapFS{"template.html": {Data: []byte("<html></html>")}}
data, _ := fs.ReadFile(configDirs.ResourceFS(embedded), "template.html")
_ = data
}
Output:
type ConfigType ¶
type ConfigType int
const ( System ConfigType = iota Global All Existing Local Cache // Confined is a user level folder inside a sandbox (e.g. snap's $HOME/snap/<name>/<revision>/.config). // It is returned after Global when the application runs confined. Confined // Resource is a read-only folder that contains files installed with the application. // See ConfigDir.QueryResourceFolders. Resource )