From 5707b8bb069f107335f4b1258375eb9ae869b1c3 Mon Sep 17 00:00:00 2001 From: Simon Cornet Date: Tue, 25 Feb 2025 13:08:04 +0100 Subject: [PATCH] feat: prepare for other git services --- cmd/gogitlabber/git.go | 52 ++++++++++++++++++++++++++++++++ cmd/gogitlabber/gitlab.go | 62 +++++---------------------------------- cmd/gogitlabber/main.go | 2 +- 3 files changed, 61 insertions(+), 55 deletions(-) diff --git a/cmd/gogitlabber/git.go b/cmd/gogitlabber/git.go index ea439c0..9fca9e1 100644 --- a/cmd/gogitlabber/git.go +++ b/cmd/gogitlabber/git.go @@ -6,6 +6,58 @@ import ( "strings" ) +func checkoutRepositories(repositories []Repository) { + + for _, repo := range repositories { + + // create clone + repoName := string(repo.PathWithNamespace) + url := getGitlabURL(gitlabToken, gitlabHost, repoName) + + // create repository destination + repoDestination := repoDestinationPre + repoName + + // create and update bar description + descriptionPrefixPre := "Cloning repository " + descriptionPrefix := descriptionPrefixPre + repoName + " ..." + bar.Describe(descriptionPrefix) + + // clone the repo + cloneOutput, err := cloneRepository(repoDestination, url) + + if err != nil { + + // if repo already exists, try to pull the latest changes + if strings.Contains(string(cloneOutput), + "already exists and is not an empty directory") { + + descriptionPrefixPre := "Pulling repository " + descriptionPrefix := descriptionPrefixPre + repoName + " ..." + bar.Describe(descriptionPrefix) + + _, err := pullRepositories(repoDestination) + if err != nil { + continue + } + + pulledCount = pulledCount + 1 + continue + } + + // in case cloning failed and the directory does not exist + // print the clone error and continue + log.Printf("\n❌ error cloning %s: %v\n%s\n", repoName, err, cloneOutput) + errorCount = errorCount + 1 + bar.Add(1) + continue + } + + // finish the clone + clonedCount = clonedCount + 1 + bar.Add(1) + } +} + func cloneRepository(repoDestination string, gitlabUrl string) (string, error) { cloneCmd := exec.Command("git", "clone", gitlabUrl, repoDestination) diff --git a/cmd/gogitlabber/gitlab.go b/cmd/gogitlabber/gitlab.go index 8a0f7d5..736df63 100644 --- a/cmd/gogitlabber/gitlab.go +++ b/cmd/gogitlabber/gitlab.go @@ -3,12 +3,10 @@ package main import ( "encoding/json" "fmt" - "log" "net/http" - "strings" ) -func fetchRepositories() ([]Repository, error) { +func fetchRepositoriesGitlab() ([]Repository, error) { // default options membership := "membership=true" @@ -55,57 +53,13 @@ func fetchRepositories() ([]Repository, error) { return repositories, nil } -func checkoutRepositories(repositories []Repository) { +func getGitlabURL(gitlabToken string, gitlabHost string, repoName string) (string) { - for _, repo := range repositories { + // make gitlab url + url := fmt.Sprintf("https://gitlab-token:%s@%s/%s.git", + gitlabToken, + gitlabHost, + repoName) - // create clone gitlab url - repoName := string(repo.PathWithNamespace) - gitlabUrl := fmt.Sprintf("https://gitlab-token:%s@%s/%s.git", - gitlabToken, - gitlabHost, - repoName) - - // create repository destination - repoDestination := repoDestinationPre + repoName - - // create and update bar description - descriptionPrefixPre := "Cloning repository " - descriptionPrefix := descriptionPrefixPre + repoName + " ..." - bar.Describe(descriptionPrefix) - - // clone the repo - cloneOutput, err := cloneRepository(repoDestination, gitlabUrl) - - if err != nil { - - // if repo already exists, try to pull the latest changes - if strings.Contains(string(cloneOutput), - "already exists and is not an empty directory") { - - descriptionPrefixPre := "Pulling repository " - descriptionPrefix := descriptionPrefixPre + repoName + " ..." - bar.Describe(descriptionPrefix) - - _, err := pullRepositories(repoDestination) - if err != nil { - continue - } - - pulledCount = pulledCount + 1 - continue - } - - // in case cloning failed and the directory does not exist - // print the clone error and continue - log.Printf("\n❌ error cloning %s: %v\n%s\n", repoName, err, cloneOutput) - errorCount = errorCount + 1 - bar.Add(1) - continue - } - - // finish the clone - clonedCount = clonedCount + 1 - bar.Add(1) - } + return url } diff --git a/cmd/gogitlabber/main.go b/cmd/gogitlabber/main.go index 9112f01..124e3bf 100644 --- a/cmd/gogitlabber/main.go +++ b/cmd/gogitlabber/main.go @@ -29,7 +29,7 @@ func main() { verifyGitAvailable() // fetch repository information from gitlab - repositories, err := fetchRepositories() + repositories, err := fetchRepositoriesGitlab() if err != nil { log.Fatalf("Error fetching repositories: %v", err) }