mirror of
https://github.com/grdl/git-get.git
synced 2026-02-05 01:29:42 +00:00
* Fix typo in readme * Reimplement all git methods without go-git * Rename repo pkg to git, add gitconfig methods * Improve tests for configuration reading * Rename package file to io and move RepoFinder there * Refactor printers - Remove smart printer - Decouple printers from git repos with interfaces - Update printer functions - Remove unnecessary flags - Add better remote URL detection * Update readme and go.mod * Add author to git commit in tests Otherwise tests will fail in CI. * Install git before running tests and don't use cgo * Add better error message, revert installing git * Ensure commit message is in quotes * Set up git config before running tests
75 lines
1.4 KiB
Go
75 lines
1.4 KiB
Go
package pkg
|
|
|
|
import (
|
|
"fmt"
|
|
"git-get/pkg/cfg"
|
|
"git-get/pkg/git"
|
|
"git-get/pkg/io"
|
|
"git-get/pkg/print"
|
|
"strings"
|
|
)
|
|
|
|
var repos []string
|
|
|
|
// ListCfg provides configuration for the List command.
|
|
type ListCfg struct {
|
|
Fetch bool
|
|
Output string
|
|
Root string
|
|
}
|
|
|
|
// List executes the "git list" command.
|
|
func List(c *ListCfg) error {
|
|
paths, err := io.NewRepoFinder(c.Root).Find()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// TODO: we should open, fetch and read status of each repo in separate goroutine
|
|
var repos []git.Repo
|
|
for _, path := range paths {
|
|
repo, err := git.Open(path)
|
|
if err != nil {
|
|
// TODO: how should we handle it?
|
|
continue
|
|
}
|
|
|
|
if c.Fetch {
|
|
err := repo.Fetch()
|
|
if err != nil {
|
|
// TODO: handle error
|
|
}
|
|
}
|
|
|
|
repos = append(repos, *repo)
|
|
}
|
|
|
|
switch c.Output {
|
|
case cfg.OutFlat:
|
|
printables := make([]print.Repo, len(repos))
|
|
for i := range repos {
|
|
printables[i] = &repos[i]
|
|
}
|
|
fmt.Println(print.NewFlatPrinter().Print(printables))
|
|
|
|
case cfg.OutTree:
|
|
printables := make([]print.Repo, len(repos))
|
|
for i := range repos {
|
|
printables[i] = &repos[i]
|
|
}
|
|
fmt.Println(print.NewTreePrinter().Print(c.Root, printables))
|
|
|
|
case cfg.OutDump:
|
|
printables := make([]print.DumpRepo, len(repos))
|
|
for i := range repos {
|
|
printables[i] = &repos[i]
|
|
}
|
|
fmt.Println(print.NewDumpPrinter().Print(printables))
|
|
|
|
default:
|
|
return fmt.Errorf("invalid --out flag; allowed values: [%s]", strings.Join(cfg.AllowedOut, ", "))
|
|
}
|
|
|
|
return nil
|
|
}
|