From b211beee817893a39267f8a18a6daa8351010070 Mon Sep 17 00:00:00 2001 From: giuseppe-g-gelardi Date: Sat, 25 Nov 2023 19:44:09 -0500 Subject: [PATCH] config saves in ~/.config on mac/linux, appdata\local for windows --- config/cfg_yml.go | 48 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/config/cfg_yml.go b/config/cfg_yml.go index cc71230..018e8b2 100644 --- a/config/cfg_yml.go +++ b/config/cfg_yml.go @@ -2,20 +2,16 @@ package config import ( "errors" + "fmt" "os" + "os/user" + "path/filepath" + "runtime" "time" "gopkg.in/yaml.v3" ) -// type EditorCfg string - -// const ( -// vsCode EditorCfg = "vscode" -// vim EditorCfg = "vim" -// neovim EditorCfg = "nvim" -// ) - type Config struct { AccessToken string `json:"access_token" yaml:"access_token"` Editor string `json:"editor" yaml:"editor"` @@ -24,13 +20,19 @@ type Config struct { } type CfgManager struct { - ConfigFileName string - DefaultConfig Config + ConfigFileName string + DefaultConfig Config } +/* + change the location of the config file. + - for linux and mac it is ~/.config/session_config.yaml + - for windows it is %APPDATA%\local\session_config.yaml ??? +*/ + func NewCfgManager() *CfgManager { return &CfgManager{ - ConfigFileName: "session_config.yaml", + ConfigFileName: getConfigFileLocation(), // ~/.config/session_config.yaml || %APPDATA%\local\session_config.yaml DefaultConfig: Config{ AccessToken: "", Editor: "vscode", @@ -40,6 +42,30 @@ func NewCfgManager() *CfgManager { } } +func getConfigFileLocation() string { + var configDir string + + // Get user's home directory + usr, err := user.Current() + if err != nil { + fmt.Println("Error getting user's home directory:", err) + return "" + } + + // Determine config directory based on operating system + switch runtime.GOOS { + case "windows": + configDir = filepath.Join(os.Getenv("APPDATA"), "local") + case "linux", "darwin": + configDir = filepath.Join(usr.HomeDir, ".config") + default: + fmt.Println("Unsupported operating system") + return "" + } + + return filepath.Join(configDir, "session_config.yaml") +} + func (cm *CfgManager) GetConfig(rcDepth int) (*Config, error) { if rcDepth > 5 { return nil, errors.New("unable to find config file")