From 3f81ebfb0d879b7d88b6facc9446ec44865eca89 Mon Sep 17 00:00:00 2001 From: Simon Cornet Date: Fri, 28 Feb 2025 14:34:37 +0100 Subject: [PATCH] style: use linter to style the code --- cmd/gogitlabber/git.go | 57 ++++++++++++++++---------------- cmd/gogitlabber/gitlab.go | 11 ------ cmd/gogitlabber/input.go | 2 +- cmd/gogitlabber/main.go | 4 +-- cmd/gogitlabber/prerequisites.go | 4 +-- 5 files changed, 33 insertions(+), 45 deletions(-) diff --git a/cmd/gogitlabber/git.go b/cmd/gogitlabber/git.go index fa59bcf..5510e59 100644 --- a/cmd/gogitlabber/git.go +++ b/cmd/gogitlabber/git.go @@ -10,9 +10,8 @@ import ( func checkoutRepositories(repositories []Repository) { for _, repo := range repositories { - // create clone + // get repository name repoName := string(repo.PathWithNamespace) - url := getGitlabURL(gitlabToken, gitlabHost, repoName) // create repository destination repoDestination := repoDestinationPre + repoName @@ -23,15 +22,18 @@ func checkoutRepositories(repositories []Repository) { bar.Describe(descriptionPrefix) // clone the repo - cloneRepository := func(repoDestination string, gitlabUrl string) (string, error) { - cloneCmd := exec.Command("git", "clone", gitlabUrl, repoDestination) - cloneOutput, err := cloneCmd.CombinedOutput() + cloneRepository := func(repoDestination string, url string) (string, error) { + cloneCmd := exec.Command("git", "clone", url, repoDestination) + cloneOutput, err := cloneCmd.CombinedOutput() - return string(cloneOutput), err - } + return string(cloneOutput), err + } + + // make gitlab url + url := fmt.Sprintf("https://gitlab-token:%s@%s/%s.git", gitlabToken, gitlabHost, repoName) cloneOutput, err := cloneRepository(repoDestination, url) - // try to pull if clone didnt work + // try to pull if clone didnt work if err != nil { // if repo already exists, try to pull the latest changes @@ -59,27 +61,25 @@ func checkoutRepositories(repositories []Repository) { func pullRepository(repoName string, repoDestination string) { - // update the progress bar - descriptionPrefixPre := "Pulling repository " - descriptionPrefix := descriptionPrefixPre + repoName + " ..." - bar.Describe(descriptionPrefix) + // update the progress bar + descriptionPrefixPre := "Pulling repository " + descriptionPrefix := descriptionPrefixPre + repoName + " ..." + bar.Describe(descriptionPrefix) + // find remote + findRemote := func(repoDestination string) (string, error) { + remoteCmd := exec.Command("git", "-C", repoDestination, "remote", "show") + remoteOutput, err := remoteCmd.CombinedOutput() + if err != nil { + return "", fmt.Errorf("finding remote: %v", err) + } - // find remote - findRemote := func(repoDestination string) (string, error) { - remoteCmd := exec.Command("git", "-C", repoDestination, "remote", "show") - remoteOutput, err := remoteCmd.CombinedOutput() - if err != nil { - return "", fmt.Errorf("finding remote: %v", err) - } - - remote := strings.Split(strings.TrimSpace(string(remoteOutput)), "\n")[0] - return remote, nil - } + remote := strings.Split(strings.TrimSpace(string(remoteOutput)), "\n")[0] + return remote, nil + } remote, _ := findRemote(repoDestination) - - // pull repository + // pull repository pullCmd := exec.Command("git", "-C", repoDestination, "pull", remote) pullOutput, err := pullCmd.CombinedOutput() @@ -88,11 +88,10 @@ func pullRepository(repoName string, repoDestination string) { if strings.Contains(string(pullOutput), "You have unstaged changes") { pullErrorMsg = append(pullErrorMsg, repoDestination) } else { - log.Printf("pull error: %v", err) - } + log.Printf("pull error: %v", err) + } } - - // update the progress bar + // update the progress bar bar.Add(1) } diff --git a/cmd/gogitlabber/gitlab.go b/cmd/gogitlabber/gitlab.go index 649f4e7..08bef3e 100644 --- a/cmd/gogitlabber/gitlab.go +++ b/cmd/gogitlabber/gitlab.go @@ -56,14 +56,3 @@ func fetchRepositoriesGitlab() ([]Repository, error) { return repositories, nil } - -func getGitlabURL(gitlabToken string, gitlabHost string, repoName string) (string) { - - // make gitlab url - url := fmt.Sprintf("https://gitlab-token:%s@%s/%s.git", - gitlabToken, - gitlabHost, - repoName) - - return url -} diff --git a/cmd/gogitlabber/input.go b/cmd/gogitlabber/input.go index 3998c5f..46efc1d 100644 --- a/cmd/gogitlabber/input.go +++ b/cmd/gogitlabber/input.go @@ -10,7 +10,7 @@ import ( func manageArguments() { // configuration vars - var archivedFlag = flag.String("archived", "excluded", "To include archived repositories (any|excluded|exclusive)\n example: -archived=any\nenv = GOGITLABBER_ARCHIVED\n") + var archivedFlag = flag.String("archived", "excluded", "To include archived repositories (any|excluded|exclusive)\n example: -archived=any\nenv = GOGITLABBER_ARCHIVED\n") var destinationFlag = flag.String("destination", "$HOME/Documents", "Specify where to check the repositories out\n example: -destination=$HOME/repos\nenv = GOGITLABBER_DESTINATION\n") var hostFlag = flag.String("gitlab-url", "gitlab.com", "Specify GitLab host\n example: -gitlab-url=gitlab.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") diff --git a/cmd/gogitlabber/main.go b/cmd/gogitlabber/main.go index 124e3bf..59e51c3 100644 --- a/cmd/gogitlabber/main.go +++ b/cmd/gogitlabber/main.go @@ -25,8 +25,8 @@ func main() { // manage all argument magic manageArguments() - // check for git - verifyGitAvailable() + // check for git + verifyGitAvailable() // fetch repository information from gitlab repositories, err := fetchRepositoriesGitlab() diff --git a/cmd/gogitlabber/prerequisites.go b/cmd/gogitlabber/prerequisites.go index 64f0924..d1af01d 100644 --- a/cmd/gogitlabber/prerequisites.go +++ b/cmd/gogitlabber/prerequisites.go @@ -1,7 +1,7 @@ package main import ( - "log" + "log" "os" "os/exec" ) @@ -9,7 +9,7 @@ import ( func verifyGitAvailable() { _, err := exec.LookPath("git") if err != nil { - log.Fatalf("Error: could not find git in path") + log.Fatalf("Error: could not find git in path") os.Exit(1) } }