6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-05 03:49:42 +00:00
Files
git-get/pkg/print/dump.go
Grzegorz Dlugoszewski da8f0931d0 Add a "dump" output option to git-list
It prints a list of all repos URL and is supposed to be consumed by `git get --dump` option.
2020-06-18 11:05:39 +02:00

38 lines
963 B
Go

package print
import (
"git-get/pkg/git"
"strings"
)
type DumpPrinter struct{}
// Print generates a list of repos URLs. Each line contains a URL and, if applicable, a currently checked out branch name.
// It's a way to dump all repositories managed by git-get and is supposed to be consumed by `git get --dump`.
func (p *DumpPrinter) Print(_ string, repos []*git.Repo) string {
var str strings.Builder
for i, repo := range repos {
remotes, err := repo.Remotes()
if err != nil || len(remotes) == 0 {
continue
}
// TODO: Needs work. Right now we're just assuming the first remote is the origin one and the one from which the current branch is checked out.
url := remotes[0].Config().URLs[0]
current := repo.Status.CurrentBranch
str.WriteString(url)
if current != git.StatusDetached && current != git.StatusUnknown {
str.WriteString(" " + current)
}
if i < len(repos)-1 {
str.WriteString("\n")
}
}
return str.String()
}