Skip to content

Commit

Permalink
config saves in ~/.config on mac/linux, appdata\local for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
giuseppe-g-gelardi committed Nov 26, 2023
1 parent 4a9ec9c commit b211bee
Showing 1 changed file with 37 additions and 11 deletions.
48 changes: 37 additions & 11 deletions config/cfg_yml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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",
Expand All @@ -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")
Expand Down

0 comments on commit b211bee

Please sign in to comment.