From da8f0931d0ae837b8e1acd50d0443345312fe7eb Mon Sep 17 00:00:00 2001 From: Grzegorz Dlugoszewski Date: Thu, 18 Jun 2020 11:05:39 +0200 Subject: [PATCH] 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. --- cmd/get/main.go | 6 +++--- cmd/list/main.go | 8 ++++---- pkg/cfg/config.go | 11 ++++++----- pkg/print/dump.go | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 pkg/print/dump.go diff --git a/cmd/get/main.go b/cmd/get/main.go index f38396e..e874d96 100644 --- a/cmd/get/main.go +++ b/cmd/get/main.go @@ -23,13 +23,13 @@ var cmd = &cobra.Command{ func init() { cmd.PersistentFlags().StringP(cfg.KeyReposRoot, "r", "", "repos root") 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") viper.BindPFlag(cfg.KeyReposRoot, 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)) } @@ -38,7 +38,7 @@ func Run(cmd *cobra.Command, args []string) { root := viper.GetString(cfg.KeyReposRoot) - if bundle := viper.GetString(cfg.KeyBundle); bundle != "" { + if bundle := viper.GetString(cfg.KeyDump); bundle != "" { opts, err := path.ParseBundleFile(bundle) exitIfError(err) diff --git a/cmd/list/main.go b/cmd/list/main.go index 6f459d5..06dd723 100644 --- a/cmd/list/main.go +++ b/cmd/list/main.go @@ -22,14 +22,12 @@ var cmd = &cobra.Command{ func init() { cmd.PersistentFlags().StringP(cfg.KeyReposRoot, "r", "", "repos root") 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().StringP(cfg.KeyOutput, "o", cfg.DefOutput, "output format.") viper.BindPFlag(cfg.KeyReposRoot, 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.KeyOutput, cmd.PersistentFlags().Lookup(cfg.KeyOutput)) } @@ -50,12 +48,14 @@ func Run(cmd *cobra.Command, args []string) { switch viper.GetString(cfg.KeyOutput) { case cfg.OutFlat: printer = &print.FlatPrinter{} - case cfg.OutSimple: + case cfg.OutTree: printer = &print.SimpleTreePrinter{} case cfg.OutSmart: printer = &print.SmartTreePrinter{} + case cfg.OutDump: + printer = &print.DumpPrinter{} 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) diff --git a/pkg/cfg/config.go b/pkg/cfg/config.go index 499fa6f..6edd346 100644 --- a/pkg/cfg/config.go +++ b/pkg/cfg/config.go @@ -18,12 +18,12 @@ const GitgetPrefix = "gitget" const ( KeyBranch = "branch" DefBranch = "master" - KeyBundle = "bundle" + KeyDump = "dump" KeyDefaultHost = "defaultHost" DefDefaultHost = "github.com" KeyFetch = "fetch" KeyOutput = "out" - DefOutput = OutSimple + DefOutput = OutTree KeyPrivateKey = "privateKey" DefPrivateKey = "id_rsa" KeyReposRoot = "reposRoot" @@ -32,9 +32,10 @@ const ( // Allowed values for the --out flag const ( - OutFlat = "flat" - OutSmart = "smart" - OutSimple = "simple" + OutDump = "dump" + OutFlat = "flat" + OutTree = "tree" + OutSmart = "smart" ) // Version metadata set by ldflags during the build. diff --git a/pkg/print/dump.go b/pkg/print/dump.go new file mode 100644 index 0000000..d2ebc67 --- /dev/null +++ b/pkg/print/dump.go @@ -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() +}