feat: added summary and improved progressbar description
This commit is contained in:
parent
20c463ad77
commit
c998fbfbdf
3 changed files with 68 additions and 17 deletions
|
|
@ -73,7 +73,8 @@ func checkoutRepositories(repositories []Repository) {
|
||||||
progressbar.OptionSetWriter(ansi.NewAnsiStdout()),
|
progressbar.OptionSetWriter(ansi.NewAnsiStdout()),
|
||||||
progressbar.OptionEnableColorCodes(true),
|
progressbar.OptionEnableColorCodes(true),
|
||||||
progressbar.OptionShowCount(),
|
progressbar.OptionShowCount(),
|
||||||
progressbar.OptionSetElapsedTime(true),
|
progressbar.OptionShowDescriptionAtLineEnd(),
|
||||||
|
progressbar.OptionSetElapsedTime(false),
|
||||||
progressbar.OptionSetPredictTime(false),
|
progressbar.OptionSetPredictTime(false),
|
||||||
progressbar.OptionSetWidth(20),
|
progressbar.OptionSetWidth(20),
|
||||||
progressbar.OptionSetDescription(barPrefix),
|
progressbar.OptionSetDescription(barPrefix),
|
||||||
|
|
@ -94,43 +95,89 @@ func checkoutRepositories(repositories []Repository) {
|
||||||
|
|
||||||
repoDestination := repoDestinationPre + repoName
|
repoDestination := repoDestinationPre + repoName
|
||||||
|
|
||||||
cloneCmd := exec.Command("git", "clone", gitlabUrl, repoDestination)
|
descriptionPrefixPre := "Cloning repository "
|
||||||
cloneOutput, err := cloneCmd.CombinedOutput()
|
descriptionPrefix := descriptionPrefixPre + repoName
|
||||||
|
bar.Describe(descriptionPrefix)
|
||||||
|
|
||||||
|
cloneOutput, err := cloneRepository(repoDestination, gitlabUrl)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
// if repo already exists, try to pull the latest changes
|
// if repo already exists, try to pull the latest changes
|
||||||
if strings.Contains(string(cloneOutput),
|
if strings.Contains(string(cloneOutput),
|
||||||
"already exists and is not an empty directory") {
|
"already exists and is not an empty directory") {
|
||||||
pullRepositories(repoDestination)
|
|
||||||
|
descriptionPrefixPre := "Pulling repository "
|
||||||
|
descriptionPrefix := descriptionPrefixPre + repoName
|
||||||
|
bar.Describe(descriptionPrefix)
|
||||||
|
|
||||||
|
_, err := pullRepositories(repoDestination)
|
||||||
|
if err != nil {
|
||||||
bar.Add(1)
|
bar.Add(1)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
log.Printf("❌ error cloning %s: %v\n%s", repoName, err, string(cloneOutput))
|
pulledCount = pulledCount + 1
|
||||||
bar.Add(1)
|
bar.Add(1)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("\n❌ error cloning %s: %v\n%s\n", repoName, err, cloneOutput)
|
||||||
|
errorCount = errorCount + 1
|
||||||
|
bar.Add(1)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
clonedCount = clonedCount + 1
|
||||||
bar.Add(1)
|
bar.Add(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// print empty line as the bar does not do that
|
// print empty line as the bar does not do that
|
||||||
fmt.Println("")
|
fmt.Println("")
|
||||||
|
|
||||||
|
// print summary
|
||||||
|
fmt.Printf("Summary:\n Cloned repositories: %v\n Pulled repositories: %v\n Errors: %v\n", clonedCount, pulledCount, errorCount)
|
||||||
}
|
}
|
||||||
|
|
||||||
func pullRepositories(repoDestination string) {
|
func cloneRepository(repoDestination string, gitlabUrl string) (string, error) {
|
||||||
pullCmd := exec.Command("git", "-C", repoDestination, "pull", "origin")
|
|
||||||
|
cloneCmd := exec.Command("git", "clone", gitlabUrl, repoDestination)
|
||||||
|
cloneOutput, err := cloneCmd.CombinedOutput()
|
||||||
|
|
||||||
|
return string(cloneOutput), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func findRemote(repoDestination string) (string, error) {
|
||||||
|
|
||||||
|
remoteCmd := exec.Command("git", "-C", repoDestination, "remote", "show")
|
||||||
|
remoteOutput, err := remoteCmd.CombinedOutput()
|
||||||
|
remote := strings.Split(strings.TrimSpace(string(remoteOutput)), "\n")[0]
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("\n❌ error finding remote for: %s\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return remote, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func pullRepositories(repoDestination string) (string, error) {
|
||||||
|
|
||||||
|
remote, err := findRemote(repoDestination)
|
||||||
|
pullCmd := exec.Command("git", "-C", repoDestination, "pull", remote)
|
||||||
pullOutput, err := pullCmd.CombinedOutput()
|
pullOutput, err := pullCmd.CombinedOutput()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
errorCount = errorCount + 1
|
||||||
if strings.Contains(string(pullOutput), "You have unstaged changes") {
|
if strings.Contains(string(pullOutput), "You have unstaged changes") {
|
||||||
pullError = append(pullError, repoDestination)
|
pullErrorMsg = append(pullErrorMsg, repoDestination)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return string(pullOutput), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func printPullerror(pullError []string) {
|
func printPullError(pullErrorMsg []string) {
|
||||||
if len(pullError) > 0 {
|
if len(pullErrorMsg) > 0 {
|
||||||
for _, repo := range pullError {
|
for _, repo := range pullErrorMsg {
|
||||||
fmt.Printf("❕%s has unstaged changes.\n", repo)
|
fmt.Printf("❕%s has unstaged changes.\n", repo)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,11 @@ import (
|
||||||
|
|
||||||
func manageArguments() {
|
func manageArguments() {
|
||||||
|
|
||||||
|
// configuration vars
|
||||||
var archivedFlag = flag.String("archived", "excluded", "to include archived repositories (any|excluded|exclusive)\nenv = GOGITLABBER_ARCHIVED\n")
|
var archivedFlag = flag.String("archived", "excluded", "to include archived repositories (any|excluded|exclusive)\nenv = GOGITLABBER_ARCHIVED\n")
|
||||||
var destinationFlag = flag.String("destination", "", "specify where to check the repositories out\n example: -destination=$HOME/repos\nenv = GOGITLABBER_DESTINATION\n")
|
var destinationFlag = flag.String("destination", "", "specify where to check the repositories out\n example: -destination=$HOME/repos\nenv = GOGITLABBER_DESTINATION\n")
|
||||||
var tokenFlag = flag.String("gitlab-api-token", "", "specify gitlab api token\n example: -gitlab-api=glpat-xxxx\nenv = GITLAB_API_TOKEN\n")
|
|
||||||
var hostFlag = flag.String("gitlab-url", "", "specify gitlab host\n example: -gitlab-url=gitlab.example.com\nenv = GITLAB_URL\n")
|
var hostFlag = flag.String("gitlab-url", "", "specify gitlab host\n example: -gitlab-url=gitlab.example.com\nenv = GITLAB_URL\n")
|
||||||
|
var tokenFlag = flag.String("gitlab-api-token", "", "specify gitlab api token\n example: -gitlab-api=glpat-xxxx\nenv = GITLAB_API_TOKEN\n")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,11 @@ var includeArchived string
|
||||||
var gitlabToken string
|
var gitlabToken string
|
||||||
var gitlabHost string
|
var gitlabHost string
|
||||||
|
|
||||||
// functional vars
|
var clonedCount int
|
||||||
var pullError []string
|
var errorCount int
|
||||||
|
var pulledCount int
|
||||||
|
var pullError int
|
||||||
|
var pullErrorMsg []string
|
||||||
|
|
||||||
type Repository struct {
|
type Repository struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
|
|
@ -31,5 +34,5 @@ func main() {
|
||||||
|
|
||||||
// manage found repositories
|
// manage found repositories
|
||||||
checkoutRepositories(repositories)
|
checkoutRepositories(repositories)
|
||||||
printPullerror(pullError)
|
printPullError(pullErrorMsg)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue