mirror of
https://github.com/taigrr/mg.git
synced 2026-04-02 03:28:42 -07:00
add register command
This commit is contained in:
31
cmd/mg/cmd/common.go
Normal file
31
cmd/mg/cmd/common.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/taigrr/mg/parse"
|
||||
)
|
||||
|
||||
func GetConfig() parse.MGConfig {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
return conf
|
||||
}
|
||||
@@ -5,13 +5,15 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
git "github.com/go-git/go-git/v5"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var registerCmd = &cobra.Command{
|
||||
Use: "register",
|
||||
Short: "add current path to list of repos",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
Run: func(_ *cobra.Command, args []string) {
|
||||
conf := GetConfig()
|
||||
path, err := os.Getwd()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
@@ -21,6 +23,45 @@ var registerCmd = &cobra.Command{
|
||||
path = args[0]
|
||||
}
|
||||
fmt.Printf("register called for %s\n", path)
|
||||
r, err := git.PlainOpenWithOptions(path, &(git.PlainOpenOptions{DetectDotGit: true}))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
remotes, err := r.Remotes()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if len(remotes) == 0 {
|
||||
log.Println("no remotes found")
|
||||
os.Exit(1)
|
||||
}
|
||||
remote := remotes[0]
|
||||
urls := remote.Config().URLs
|
||||
if len(urls) == 0 {
|
||||
log.Println("no urls found for remote")
|
||||
os.Exit(1)
|
||||
}
|
||||
url := urls[0]
|
||||
newPath, err := r.Worktree()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
path = newPath.Filesystem.Root()
|
||||
for _, v := range conf.Repos {
|
||||
if v.Path == path {
|
||||
log.Println("repo already registered")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
conf.AddRepo(path, url)
|
||||
err = conf.Save()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -1,31 +1,9 @@
|
||||
package main
|
||||
|
||||
import "github.com/taigrr/mg/cmd/mg/cmd"
|
||||
import (
|
||||
"github.com/taigrr/mg/cmd/mg/cmd"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 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)
|
||||
cmd.Execute()
|
||||
}
|
||||
|
||||
func Register() {
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user