configpath
package is designed to serve as an Operating System (OS) - independent way to determine appropriate paths
for configuration folders and files. It supports different paths depending on the OS (Unix, Darwin, Windows) and follows
the conventions specific to each system.
The package provides ConfigPath
type which is a structure containing all the necessary path details both on a user and
system level. This type comprises two strings - Vendor
and Application
, these are used in constructing the
generated paths.
package main
import "github.com/FirinKinuo/configpath"
func main() {
configPath := configpath.ConfigPath{
Vendor: "TestVendor",
Application: "TestApplication",
}
// /home/user/TestVendor/TestApplication/test.conf
userConfigPath := configPath.UserFile("test.conf")
// /etc/TestVendor/TestApplication/test.conf
systemConfigPath := configPath.SystemFile("test.conf")
}
On Linux systems, the configpath package uses the conventions generally expected.
System level configuration files are typically stored in the /etc
directory.
Example: /etc/vendor/application
For user level configurations, typically a .config directory under the user's home directory is used.
Example: /home/user/.config/vendor/application
Please note that if the HOME environment variable isn't set, the package will default to using system level configuration paths, which may lead to potential issues. The package does not create directories or files, but only helps in constructing the appropriate paths. Always ensure to handle necessary rights and checks when your program interacts with these paths.
On Windows, the configpath package respects the Windows specific conventions for storing user specific and system level configurations
The system level configuration files are typically stored in the ProgramData directory.
Example: C:\ProgramData\Vendor\Application
For user specific configurations, the AppData directory within the user's profile directory is typically used.
Example: C:\Users\User\AppData\Roaming\Vendor\Application
On macOS (Darwin), the configpath package follows the macOS specific conventions.
System level configuration files are typically stored in the /Library directory.
Example: /Library/Vendor/Application
For user level configurations, typically a .config directory under the user's home directory is used.
Example: /Users/user/Vendor/Application
In those rare instances where the operating system is not detected as one of the three major ones (Windows, Unix/Linux, or macOS), the configpath package defaults to specific values.
The system configuration files are stored in the /etc directory, similar to Unix conventions.
Example: /etc/vendor/application
User specific configuration files are expected in the current execution directory of the concerning application. This is achieved by using a relative path "./".
Example: ./vendor/application
This makes configpath flexible and applicable even on unconventional or lesser-known OS setups.
While configpath is designed to provide an OS-agnostic way to handle paths, there may be times when you want to use a custom path as the base for your application's configuration files.
package main
import "github.com/FirinKinuo/configpath"
func main() {
cp := configpath.ConfigPath{
Application: "TestApplication",
CustomFolderPath: "/custom/path",
}
cp.CustomFolderFile("test.conf") // /custom/path/TestApplication/test.conf
}
Copyright (c) Firin Kinuo
Licensed under MIT License