mirror of
https://github.com/taigrr/mg.git
synced 2026-04-02 03:28:42 -07:00
initial commit
This commit is contained in:
17
cmd/mrpaths.go
Normal file
17
cmd/mrpaths.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/taigrr/mg/parse"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
mrconf, err := parse.LoadMRConfig()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
for _, path := range mrconf.GetRepoPaths() {
|
||||||
|
fmt.Println(path)
|
||||||
|
}
|
||||||
|
}
|
||||||
92
parse/myrepos.go
Normal file
92
parse/myrepos.go
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
package parse
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type MRConfig struct {
|
||||||
|
Unregister string
|
||||||
|
GC string
|
||||||
|
Repos []Repo
|
||||||
|
}
|
||||||
|
type Repo struct {
|
||||||
|
Path string
|
||||||
|
Checkout string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m MRConfig) GetRepoPaths() []string {
|
||||||
|
paths := []string{}
|
||||||
|
for _, r := range m.Repos {
|
||||||
|
paths = append(paths, r.Path)
|
||||||
|
}
|
||||||
|
return paths
|
||||||
|
}
|
||||||
|
|
||||||
|
func LoadMRConfig() (MRConfig, error) {
|
||||||
|
home, err := os.UserHomeDir()
|
||||||
|
if err != nil {
|
||||||
|
return MRConfig{}, err
|
||||||
|
}
|
||||||
|
mrconfPath := filepath.Join(home, ".mrconfig")
|
||||||
|
s, err := os.Stat(mrconfPath)
|
||||||
|
if err != nil {
|
||||||
|
return MRConfig{}, err
|
||||||
|
}
|
||||||
|
if s.IsDir() {
|
||||||
|
return MRConfig{}, errors.New("expected mrconfig file but got a directory")
|
||||||
|
}
|
||||||
|
f, err := ioutil.ReadFile(mrconfPath)
|
||||||
|
if err != nil {
|
||||||
|
return MRConfig{}, err
|
||||||
|
}
|
||||||
|
text := string(f)
|
||||||
|
lines := strings.Split(text, "\n")
|
||||||
|
config := MRConfig{}
|
||||||
|
|
||||||
|
length := -1
|
||||||
|
mode := "default"
|
||||||
|
for n, line := range lines {
|
||||||
|
if line == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if line == "[DEFAULT]" {
|
||||||
|
mode = "default"
|
||||||
|
continue
|
||||||
|
} else if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
|
||||||
|
length++
|
||||||
|
path := strings.Trim(line, "[]")
|
||||||
|
if !strings.HasPrefix(path, "/") {
|
||||||
|
path = filepath.Join(home, path)
|
||||||
|
}
|
||||||
|
mode = "repo"
|
||||||
|
config.Repos = append(config.Repos, Repo{Path: path})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
split := strings.SplitN(line, " = ", 2)
|
||||||
|
if len(split) != 2 {
|
||||||
|
return MRConfig{}, fmt.Errorf("unexpected argument on line %d: %s", n, line)
|
||||||
|
}
|
||||||
|
switch mode {
|
||||||
|
case "repo":
|
||||||
|
if split[0] != "checkout" {
|
||||||
|
return MRConfig{}, fmt.Errorf("unexpected argument on line %d: %s", n, line)
|
||||||
|
}
|
||||||
|
config.Repos[length].Checkout = split[1]
|
||||||
|
case "default":
|
||||||
|
switch split[0] {
|
||||||
|
case "unregister":
|
||||||
|
config.Unregister = split[1]
|
||||||
|
case "git_gc":
|
||||||
|
config.GC = split[1]
|
||||||
|
default:
|
||||||
|
return MRConfig{}, fmt.Errorf("unexpected argument on line %d: %s", n, line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return config, nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user