6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-08 10:29:18 +00:00

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.
This commit is contained in:
Grzegorz Dlugoszewski
2020-06-18 11:05:39 +02:00
parent edf60064a8
commit da8f0931d0
4 changed files with 50 additions and 12 deletions

View File

@@ -23,13 +23,13 @@ var cmd = &cobra.Command{
func init() { func init() {
cmd.PersistentFlags().StringP(cfg.KeyReposRoot, "r", "", "repos root") cmd.PersistentFlags().StringP(cfg.KeyReposRoot, "r", "", "repos root")
cmd.PersistentFlags().StringP(cfg.KeyPrivateKey, "p", "", "SSH private key path") cmd.PersistentFlags().StringP(cfg.KeyPrivateKey, "p", "", "SSH private key path")
cmd.PersistentFlags().StringP(cfg.KeyBundle, "u", "", "Bundle file path") cmd.PersistentFlags().StringP(cfg.KeyDump, "d", "", "Dump file path")
cmd.PersistentFlags().StringP(cfg.KeyBranch, "b", cfg.DefBranch, "Branch (or tag) to checkout after cloning") cmd.PersistentFlags().StringP(cfg.KeyBranch, "b", cfg.DefBranch, "Branch (or tag) to checkout after cloning")
viper.BindPFlag(cfg.KeyReposRoot, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot)) viper.BindPFlag(cfg.KeyReposRoot, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot))
viper.BindPFlag(cfg.KeyPrivateKey, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot)) viper.BindPFlag(cfg.KeyPrivateKey, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot))
viper.BindPFlag(cfg.KeyBundle, cmd.PersistentFlags().Lookup(cfg.KeyBundle)) viper.BindPFlag(cfg.KeyDump, cmd.PersistentFlags().Lookup(cfg.KeyDump))
viper.BindPFlag(cfg.KeyBranch, cmd.PersistentFlags().Lookup(cfg.KeyBranch)) viper.BindPFlag(cfg.KeyBranch, cmd.PersistentFlags().Lookup(cfg.KeyBranch))
} }
@@ -38,7 +38,7 @@ func Run(cmd *cobra.Command, args []string) {
root := viper.GetString(cfg.KeyReposRoot) root := viper.GetString(cfg.KeyReposRoot)
if bundle := viper.GetString(cfg.KeyBundle); bundle != "" { if bundle := viper.GetString(cfg.KeyDump); bundle != "" {
opts, err := path.ParseBundleFile(bundle) opts, err := path.ParseBundleFile(bundle)
exitIfError(err) exitIfError(err)

View File

@@ -22,14 +22,12 @@ var cmd = &cobra.Command{
func init() { func init() {
cmd.PersistentFlags().StringP(cfg.KeyReposRoot, "r", "", "repos root") cmd.PersistentFlags().StringP(cfg.KeyReposRoot, "r", "", "repos root")
cmd.PersistentFlags().StringP(cfg.KeyPrivateKey, "p", "", "SSH private key path") cmd.PersistentFlags().StringP(cfg.KeyPrivateKey, "p", "", "SSH private key path")
cmd.PersistentFlags().StringP(cfg.KeyBundle, "u", "", "Bundle file path")
cmd.PersistentFlags().BoolP(cfg.KeyFetch, "f", false, "Fetch from remotes when listing repositories") cmd.PersistentFlags().BoolP(cfg.KeyFetch, "f", false, "Fetch from remotes when listing repositories")
cmd.PersistentFlags().StringP(cfg.KeyOutput, "o", cfg.DefOutput, "output format.") cmd.PersistentFlags().StringP(cfg.KeyOutput, "o", cfg.DefOutput, "output format.")
viper.BindPFlag(cfg.KeyReposRoot, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot)) viper.BindPFlag(cfg.KeyReposRoot, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot))
viper.BindPFlag(cfg.KeyPrivateKey, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot)) viper.BindPFlag(cfg.KeyPrivateKey, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot))
viper.BindPFlag(cfg.KeyBundle, cmd.PersistentFlags().Lookup(cfg.KeyBundle))
viper.BindPFlag(cfg.KeyFetch, cmd.PersistentFlags().Lookup(cfg.KeyFetch)) viper.BindPFlag(cfg.KeyFetch, cmd.PersistentFlags().Lookup(cfg.KeyFetch))
viper.BindPFlag(cfg.KeyOutput, cmd.PersistentFlags().Lookup(cfg.KeyOutput)) viper.BindPFlag(cfg.KeyOutput, cmd.PersistentFlags().Lookup(cfg.KeyOutput))
} }
@@ -50,12 +48,14 @@ func Run(cmd *cobra.Command, args []string) {
switch viper.GetString(cfg.KeyOutput) { switch viper.GetString(cfg.KeyOutput) {
case cfg.OutFlat: case cfg.OutFlat:
printer = &print.FlatPrinter{} printer = &print.FlatPrinter{}
case cfg.OutSimple: case cfg.OutTree:
printer = &print.SimpleTreePrinter{} printer = &print.SimpleTreePrinter{}
case cfg.OutSmart: case cfg.OutSmart:
printer = &print.SmartTreePrinter{} printer = &print.SmartTreePrinter{}
case cfg.OutDump:
printer = &print.DumpPrinter{}
default: default:
err = fmt.Errorf("invalid --out flag; allowed values: %v", []string{cfg.OutFlat, cfg.OutSimple, cfg.OutSmart}) err = fmt.Errorf("invalid --out flag; allowed values: %v", []string{cfg.OutFlat, cfg.OutTree, cfg.OutSmart})
} }
exitIfError(err) exitIfError(err)

View File

@@ -18,12 +18,12 @@ const GitgetPrefix = "gitget"
const ( const (
KeyBranch = "branch" KeyBranch = "branch"
DefBranch = "master" DefBranch = "master"
KeyBundle = "bundle" KeyDump = "dump"
KeyDefaultHost = "defaultHost" KeyDefaultHost = "defaultHost"
DefDefaultHost = "github.com" DefDefaultHost = "github.com"
KeyFetch = "fetch" KeyFetch = "fetch"
KeyOutput = "out" KeyOutput = "out"
DefOutput = OutSimple DefOutput = OutTree
KeyPrivateKey = "privateKey" KeyPrivateKey = "privateKey"
DefPrivateKey = "id_rsa" DefPrivateKey = "id_rsa"
KeyReposRoot = "reposRoot" KeyReposRoot = "reposRoot"
@@ -32,9 +32,10 @@ const (
// Allowed values for the --out flag // Allowed values for the --out flag
const ( const (
OutFlat = "flat" OutDump = "dump"
OutSmart = "smart" OutFlat = "flat"
OutSimple = "simple" OutTree = "tree"
OutSmart = "smart"
) )
// Version metadata set by ldflags during the build. // Version metadata set by ldflags during the build.

37
pkg/print/dump.go Normal file
View File

@@ -0,0 +1,37 @@
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()
}