How to Use a Go Tool to Clone GitLab Projects via Token
This guide explains how to obtain a GitLab access token, download a Go-based cloning tool, configure its parameters, and run it to automatically fetch all groups and projects from a GitLab instance, with full source code examples for customization.
First, request a personal access token from GitLab; this token grants access to all authorized groups and projects.
Next, download the Go tool from https://gitee.com/whoamiy/golang-git-tools . Modify the default values in main.go (GitLab host, token, and download directory) or pass them as command‑line arguments, then compile the program into a binary for script use.
The tool will iterate through all groups, retrieve project information via the GitLab API, and clone each repository to the specified local path using the provided token for authentication.
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/go-git/go-git/v5"
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
"io"
"net/http"
"os"
"strconv"
"strings"
)
var (
gitlab_host string
gitlab_token string
code_path string
)
func init() {
flag.StringVar(&gitlab_host, "host", "https://git.renzhikeji.com", "gitlab_host 请设置")
flag.StringVar(&gitlab_token, "token", "eJAfTmyyyVYxxxtH5jsse", "gitlab_token 请设置")
flag.StringVar(&code_path, "path", "/Users/renzhikeji/logs", "path 代码下载路径请设置")
}
func main() {
flag.Parse()
groups, err := allGroups()
if err != nil {
panic(err)
}
for _, group := range groups {
marshal, _ := json.Marshal(&group)
Info("=========项目组信息=================")
fmt.Printf("%s", marshal)
fmt.Println()
}
Info("=========项目信息收集中,请稍等=================")
allProjectInfos, err := allProjectInfo(groups)
if err != nil {
panic(err)
}
Info("=========项目信息=================")
Info("项目总数: ", len(allProjectInfos))
for _, info := range allProjectInfos {
dir := strings.Join([]string{code_path, info.Name}, "/")
Info("git clone %s %s ......................", info.HTTPURLToRepo, dir)
_, err := git.PlainClone(dir, false, &git.CloneOptions{
Auth: &githttp.BasicAuth{Username: "abc123", Password: gitlab_token},
URL: info.HTTPURLToRepo,
Progress: os.Stdout,
})
Error(err)
}
}
// ... (additional type definitions and helper functions omitted for brevity)The source code defines data structures for GitLab groups and projects, functions to request group and project lists via HTTP, and utility functions for logging and error handling.
Cognitive Technology Team
Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.