New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: os: Add SysConfigDir()
Function
#63858
Comments
This is an interesting idea! Do you know if all of the operating systems that Go supports have a clear single answer to this question? It might help to extend the proposal by specifying exactly how it should behave on each operating system, or at least on a subset of operating systems you're familiar with.
|
The UserConfigDir proposal for for cross-reference #29960 I also like the idea, to me it sounds like just the right amount of indirection for a functionality like this, if we start adding too many constraints on how a user should be using it we might make it less useful overall. Maybe smth along these lines would work for a SysConfigDir: // SysConfigDir returns the default root directory to use for system-wide
// configuration data. Applications should use this directory for storing
// configuration files that are accessible system-wide.
//
// On Unix systems, it returns /etc.
// On Darwin, it returns /Library/Preferences.
// On Windows, it returns %ProgramData%.
// On Plan 9, it returns /lib.
//
// If the location cannot be determined, it will return an error.
func SysConfigDir() (string, error) {
var dir string
switch runtime.GOOS {
case "windows":
dir = os.Getenv("ProgramData")
if dir == "" {
return "", errors.New("%ProgramData% is not defined")
}
case "darwin":
dir = "/Library/Preferences"
case "plan9":
dir = "/lib"
default: // Unix
dir = "/etc"
}
return dir, nil
} |
@mauri870 I've contemplated using the |
You can try using the CSIDL_COMMON_APPDATA constant in Windows with the package main
import (
"fmt"
"golang.org/x/sys/windows"
)
func main() {
path, err := windows.KnownFolderPath(windows.FOLDERID_ProgramData, 0)
if err is not nil {
fmt.Println(err)
return
}
fmt.Println(path)
} But, using an external package here could make things more complicated. |
The description for FOLDERID_ProgramData sounds dead center to what we need, not sure about pulling x/sys/windows in os tho, I think we need a more lightweight solution. Either way it seems like every platform has a folder dedicated to it, we just have to sort out how to get to it. Let's wait to see what folks have to say about the proposal. |
Abstract
This proposal suggests adding a new function,
SysConfigDir()
, to theos
package in the Go standard library. This function will help Go applications find the default directory for system-wide configuration data in a consistent way across different operating systems.Background
Right now,
os
package has a function calledUserConfigDir()
that's good for getting the directory for user-specific configuration data. But there's no similar function for getting the directory for system-wide configuration data. Many Go apps need this to store configuration files that should be shared among all users on a system.Proposal
I propose adding a new function,
SysConfigDir()
, to theos
package. This function will return the directory path for system-wide configuration data. It will work the same way on all supported operating systems, just likeUserConfigDir()
. The way it works will be different depending on the operating system, following the conventions of that system.Rationale
Here's why we need to add
SysConfigDir()
:Consistency: Having a standard way to get the system-wide configuration directory makes things simpler for developers. It keeps things the same no matter which operating system you're on.
Best Practices: It encourages developers to follow the best practices for managing configuration. It guides them to use the right directories and avoid problems like hardcoded paths or conflicts.
User-Friendly: Just like
UserConfigDir()
,SysConfigDir()
gives developers an easy way to find system-wide configuration directories, even on systems with different ways of doing things.Compatibility
Adding
SysConfigDir()
won't mess up existing code. It's a new feature that we're adding to theos
package, so it doesn't affect anything that's already there.Implementation
Here's how we'll make
SysConfigDir()
work:The text was updated successfully, but these errors were encountered: