add mgconfig conversion to lib

This commit is contained in:
2023-05-25 15:07:06 -07:00
parent 93a21b8c61
commit 9a4f8023d8
4 changed files with 77 additions and 0 deletions

View File

@@ -2,6 +2,7 @@ package parse
import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
)
@@ -54,3 +55,26 @@ func LoadMGConfig() (MGConfig, error) {
return config, err
}
func (m MGConfig) Save() error {
mgConf := os.Getenv("MGCONFIG")
if mgConf == "" {
confDir := os.Getenv("XDG_CONFIG_HOME")
if confDir == "" {
home, err := os.UserHomeDir()
if err != nil {
return err
}
confDir = filepath.Join(home, ".config")
if _, err := os.Stat(confDir); err != nil {
return err
}
}
mgConf = filepath.Join(confDir, "mgconfig")
}
b, err := json.MarshalIndent(m, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(mgConf, b, 0o644)
}

View File

@@ -29,6 +29,20 @@ func (m MRConfig) GetRepoPaths() []string {
return paths
}
func (m MRConfig) ToMGConfig() MGConfig {
mgconf := MGConfig{
Repos: []Repo{},
Aliases: m.Aliases,
}
for _, r := range m.Repos {
mgconf.Repos = append(mgconf.Repos, Repo{
Path: r.Path,
Aliases: r.Aliases,
})
}
return mgconf
}
// LoadMRConfig loads the mrconfig file from the user's home directory
// and returns a MRConfig struct
// TODO: load aliases into map instead of hardcoded Unregister prop