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

39
cmd/mg/main.go Normal file
View File

@@ -0,0 +1,39 @@
package main
import (
"flag"
"log"
"os"
"github.com/taigrr/mg/parse"
)
var jobs = flag.Int("j", 1, "number of jobs to run in parallel")
func main() {
flag.Parse()
conf, err := parse.LoadMGConfig()
if err != nil {
if os.IsNotExist(err) {
// Try to load mr config instead
mrconf, err := parse.LoadMRConfig()
if err != nil {
log.Println(err)
os.Exit(1)
}
conf = mrconf.ToMGConfig()
log.Println("migrated mrconfig to mgconfig")
err = conf.Save()
if err != nil {
log.Println(err)
os.Exit(1)
}
}
}
// fmt.Println(conf)
}
func Register() {
}

View File

@@ -2,6 +2,7 @@ package parse
import ( import (
"encoding/json" "encoding/json"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
) )
@@ -54,3 +55,26 @@ func LoadMGConfig() (MGConfig, error) {
return config, err 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 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 // LoadMRConfig loads the mrconfig file from the user's home directory
// and returns a MRConfig struct // and returns a MRConfig struct
// TODO: load aliases into map instead of hardcoded Unregister prop // TODO: load aliases into map instead of hardcoded Unregister prop