mirror of
https://github.com/taigrr/mg.git
synced 2026-04-02 03:28:42 -07:00
fix conflict
This commit is contained in:
93
cmd/mg/cmd/clone.go
Normal file
93
cmd/mg/cmd/clone.go
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
git "github.com/go-git/go-git/v5"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"github.com/taigrr/mg/parse"
|
||||||
|
)
|
||||||
|
|
||||||
|
// pullCmd represents the pull command
|
||||||
|
var (
|
||||||
|
cloneCmd = &cobra.Command{
|
||||||
|
Use: "clone",
|
||||||
|
Short: "ensure all repos defined in the config are cloned",
|
||||||
|
Run: func(_ *cobra.Command, args []string) {
|
||||||
|
type RepoError struct {
|
||||||
|
Error error
|
||||||
|
Repo string
|
||||||
|
}
|
||||||
|
if jobs < 1 {
|
||||||
|
log.Println("jobs must be greater than 0")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
conf := GetConfig()
|
||||||
|
if len(args) > 0 {
|
||||||
|
log.Println("too many arguments")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
repoChan := make(chan parse.Repo, len(conf.Repos))
|
||||||
|
errs := []RepoError{}
|
||||||
|
alreadyUpToDate := 0
|
||||||
|
mutex := sync.Mutex{}
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
|
wg.Add(len(conf.Repos))
|
||||||
|
for i := 0; i < jobs; i++ {
|
||||||
|
go func() {
|
||||||
|
for repo := range repoChan {
|
||||||
|
_, err := git.PlainOpenWithOptions(repo.Remote, &(git.PlainOpenOptions{DetectDotGit: true}))
|
||||||
|
if err == nil {
|
||||||
|
log.Printf("already cloned: %s\n", repo.Path)
|
||||||
|
} else if err == git.ErrRepositoryNotExists {
|
||||||
|
log.Printf("attempting clone: %s\n", repo)
|
||||||
|
_, err := git.PlainClone(repo.Path, false, &git.CloneOptions{
|
||||||
|
URL: repo.Remote,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
mutex.Lock()
|
||||||
|
errs = append(errs, RepoError{Error: err, Repo: repo.Path})
|
||||||
|
mutex.Unlock()
|
||||||
|
log.Printf("clone failed for %s: %v\n", repo.Path, err)
|
||||||
|
wg.Done()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
fmt.Printf("successfully cloned %s\n", repo)
|
||||||
|
wg.Done()
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
mutex.Lock()
|
||||||
|
errs = append(errs, RepoError{Error: err, Repo: repo.Path})
|
||||||
|
mutex.Unlock()
|
||||||
|
log.Printf("clone failed for %s: %v\n", repo, err)
|
||||||
|
wg.Done()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
for _, repo := range conf.Repos {
|
||||||
|
repoChan <- repo
|
||||||
|
}
|
||||||
|
close(repoChan)
|
||||||
|
wg.Wait()
|
||||||
|
for _, err := range errs {
|
||||||
|
log.Printf("error pulling %s: %s\n", err.Repo, err.Error)
|
||||||
|
}
|
||||||
|
lenErrs := len(errs)
|
||||||
|
fmt.Println()
|
||||||
|
fmt.Printf("successfully pulled %d/%d repos\n", len(conf.Repos)-lenErrs, len(conf.Repos))
|
||||||
|
fmt.Printf("%d repos already up to date\n", alreadyUpToDate)
|
||||||
|
fmt.Printf("failed to pull %d/%d repos\n", lenErrs, len(conf.Repos))
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(pullCmd)
|
||||||
|
pullCmd.Flags().IntVarP(&jobs, "jobs", "j", 1, "number of jobs to run in parallel")
|
||||||
|
}
|
||||||
@@ -3,7 +3,6 @@ package parse
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
@@ -138,5 +137,5 @@ func (m MGConfig) Save() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return ioutil.WriteFile(mgConf, b, 0o644)
|
return os.WriteFile(mgConf, b, 0o644)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package parse
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -60,7 +59,7 @@ func LoadMRConfig() (MRConfig, error) {
|
|||||||
if s.IsDir() {
|
if s.IsDir() {
|
||||||
return MRConfig{}, errors.New("expected mrconfig file but got a directory")
|
return MRConfig{}, errors.New("expected mrconfig file but got a directory")
|
||||||
}
|
}
|
||||||
f, err := ioutil.ReadFile(mrconfPath)
|
f, err := os.ReadFile(mrconfPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return MRConfig{}, err
|
return MRConfig{}, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user