forked from golang/config
Change ConfigDir to ConfigFile
This commit is contained in:
@@ -9,10 +9,10 @@ import (
|
||||
)
|
||||
|
||||
type Config[T any] struct {
|
||||
Name string
|
||||
Filename string
|
||||
ConfigDir *string
|
||||
Config T
|
||||
Name string
|
||||
Filename string
|
||||
ConfigFile *string
|
||||
Config T
|
||||
}
|
||||
|
||||
func getConfigDir(n string) string {
|
||||
@@ -33,17 +33,17 @@ func getConfigDir(n string) string {
|
||||
|
||||
func (c *Config[T]) Get() (bool, error) {
|
||||
config_dir := getConfigDir(c.Name)
|
||||
c.ConfigDir = &config_dir
|
||||
|
||||
err := os.MkdirAll(config_dir, 0755)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("Could not create config directory: %v", err)
|
||||
}
|
||||
|
||||
config_path := filepath.Join(config_dir, c.Filename+".toml")
|
||||
configFile := filepath.Join(config_dir, c.Filename+".toml")
|
||||
c.ConfigFile = &configFile
|
||||
|
||||
// open file, creating it if empty
|
||||
config_file, err := os.OpenFile(config_path, os.O_RDWR|os.O_CREATE, 0755)
|
||||
config_file, err := os.OpenFile(*c.ConfigFile, os.O_RDWR|os.O_CREATE, 0755)
|
||||
defer config_file.Close()
|
||||
|
||||
if err != nil {
|
||||
|
||||
+7
-7
@@ -35,11 +35,11 @@ func Test_getConfigDir_XDG_CONFIG_HOME(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func Test_ConfigGetConfigDir_HOME(t *testing.T) {
|
||||
func Test_ConfigGetConfigFile_HOME(t *testing.T) {
|
||||
os.Unsetenv("XDG_CONFIG_HOME")
|
||||
os.Setenv("HOME", "/tmp")
|
||||
|
||||
expected_config_dir := "/tmp/.config/test"
|
||||
expected_config_file := "/tmp/.config/test/test.toml"
|
||||
|
||||
config := Config[LocalConfig]{
|
||||
Name: "test",
|
||||
@@ -54,8 +54,8 @@ func Test_ConfigGetConfigDir_HOME(t *testing.T) {
|
||||
t.Errorf("Failed to get config: %v", err)
|
||||
}
|
||||
|
||||
if expected_config_dir != *config.ConfigDir {
|
||||
t.Errorf("config.ConfigDir is %v, %v expected", *config.ConfigDir, expected_config_dir)
|
||||
if expected_config_file != *config.ConfigFile {
|
||||
t.Errorf("config.ConfigFile is %v, %v expected", *config.ConfigFile, expected_config_file)
|
||||
} else {
|
||||
t.Log("Success!")
|
||||
}
|
||||
@@ -64,7 +64,7 @@ func Test_ConfigGetConfigDir_HOME(t *testing.T) {
|
||||
func Test_ConfigGetConfigDir_XDG_CONFIG_HOME(t *testing.T) {
|
||||
os.Setenv("XDG_CONFIG_HOME", "/tmp/")
|
||||
|
||||
expected_config_dir := "/tmp/test"
|
||||
expected_config_dir := "/tmp/test/test.toml"
|
||||
|
||||
config := Config[LocalConfig]{
|
||||
Name: "test",
|
||||
@@ -79,8 +79,8 @@ func Test_ConfigGetConfigDir_XDG_CONFIG_HOME(t *testing.T) {
|
||||
t.Errorf("Failed to get config: %v", err)
|
||||
}
|
||||
|
||||
if expected_config_dir != *config.ConfigDir {
|
||||
t.Errorf("config.ConfigDir is %v, %v expected", *config.ConfigDir, expected_config_dir)
|
||||
if expected_config_dir != *config.ConfigFile {
|
||||
t.Errorf("config.ConfigFile is %v, %v expected", *config.ConfigFile, expected_config_dir)
|
||||
} else {
|
||||
t.Log("Success!")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user