feat: improved logging
This commit is contained in:
parent
336cd74fa4
commit
5207524381
3 changed files with 26 additions and 4 deletions
|
|
@ -36,6 +36,9 @@ func checkoutRepositories(repositories []Repository, concurrency int) {
|
||||||
repoName := string(repo.PathWithNamespace)
|
repoName := string(repo.PathWithNamespace)
|
||||||
repoDestination := repoDestinationPre + repoName
|
repoDestination := repoDestinationPre + repoName
|
||||||
|
|
||||||
|
// log activity
|
||||||
|
logPrint("Starting on repository: "+repoName, nil)
|
||||||
|
|
||||||
// make gitlab url
|
// make gitlab url
|
||||||
url := fmt.Sprintf("https://gitlab-token:%s@%s/%s.git", gitlabToken, gitlabHost, repoName)
|
url := fmt.Sprintf("https://gitlab-token:%s@%s/%s.git", gitlabToken, gitlabHost, repoName)
|
||||||
|
|
||||||
|
|
@ -43,6 +46,7 @@ func checkoutRepositories(repositories []Repository, concurrency int) {
|
||||||
checkRepo := func(repoDestination string) string {
|
checkRepo := func(repoDestination string) string {
|
||||||
checkCmd := exec.Command("git", "-C", repoDestination, "remote", "-v")
|
checkCmd := exec.Command("git", "-C", repoDestination, "remote", "-v")
|
||||||
checkOutput, _ := checkCmd.CombinedOutput()
|
checkOutput, _ := checkCmd.CombinedOutput()
|
||||||
|
logPrint("Checking status for repository: "+repoName, nil)
|
||||||
|
|
||||||
return string(checkOutput)
|
return string(checkOutput)
|
||||||
}
|
}
|
||||||
|
|
@ -53,10 +57,14 @@ func checkoutRepositories(repositories []Repository, concurrency int) {
|
||||||
switch {
|
switch {
|
||||||
case strings.Contains(string(repoStatus), "No such file or directory"):
|
case strings.Contains(string(repoStatus), "No such file or directory"):
|
||||||
|
|
||||||
|
// log activity
|
||||||
|
logPrint("Decided to clone repository: "+repoName, nil)
|
||||||
|
|
||||||
// clone the repo
|
// clone the repo
|
||||||
cloneRepository := func(repoDestination string, url string) (string, error) {
|
cloneRepository := func(repoDestination string, url string) (string, error) {
|
||||||
cloneCmd := exec.Command("git", "clone", url, repoDestination)
|
cloneCmd := exec.Command("git", "clone", url, repoDestination)
|
||||||
cloneOutput, err := cloneCmd.CombinedOutput()
|
cloneOutput, err := cloneCmd.CombinedOutput()
|
||||||
|
logPrint("Cloning repository: "+repoName+" to "+repoDestination, nil)
|
||||||
|
|
||||||
return string(cloneOutput), err
|
return string(cloneOutput), err
|
||||||
}
|
}
|
||||||
|
|
@ -79,6 +87,7 @@ func checkoutRepositories(repositories []Repository, concurrency int) {
|
||||||
|
|
||||||
// pull the latest
|
// pull the latest
|
||||||
case strings.Contains(string(repoStatus), url):
|
case strings.Contains(string(repoStatus), url):
|
||||||
|
logPrint("Decided to pull repository: "+repoName, nil)
|
||||||
pullRepository(repoName, repoDestination)
|
pullRepository(repoName, repoDestination)
|
||||||
if !verbose {
|
if !verbose {
|
||||||
descriptionPrefixPre := "Pulling repository "
|
descriptionPrefixPre := "Pulling repository "
|
||||||
|
|
@ -88,7 +97,7 @@ func checkoutRepositories(repositories []Repository, concurrency int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
logPrint("ERROR: decided not to clone or pull repository"+repoName, nil)
|
logPrint("ERROR: decided not to clone or pull repository: "+repoName, nil)
|
||||||
logPrint("ERROR: this is why: "+repoStatus, nil)
|
logPrint("ERROR: this is why: "+repoStatus, nil)
|
||||||
|
|
||||||
// set a lock, increment counters and unlock
|
// set a lock, increment counters and unlock
|
||||||
|
|
@ -108,6 +117,9 @@ func checkoutRepositories(repositories []Repository, concurrency int) {
|
||||||
|
|
||||||
func pullRepository(repoName string, repoDestination string) {
|
func pullRepository(repoName string, repoDestination string) {
|
||||||
|
|
||||||
|
// log activity
|
||||||
|
logPrint("Pulling repository: "+repoName+" at "+repoDestination, nil)
|
||||||
|
|
||||||
// find remote
|
// find remote
|
||||||
findRemote := func(repoDestination string) (string, error) {
|
findRemote := func(repoDestination string) (string, error) {
|
||||||
remoteCmd := exec.Command("git", "-C", repoDestination, "remote", "show")
|
remoteCmd := exec.Command("git", "-C", repoDestination, "remote", "show")
|
||||||
|
|
@ -116,7 +128,9 @@ func pullRepository(repoName string, repoDestination string) {
|
||||||
return "", fmt.Errorf("finding remote: %v\n", err)
|
return "", fmt.Errorf("finding remote: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logPrint("Finding remote for repository: "+repoName+" at "+repoDestination, nil)
|
||||||
remote := strings.Split(strings.TrimSpace(string(remoteOutput)), "\n")[0]
|
remote := strings.Split(strings.TrimSpace(string(remoteOutput)), "\n")[0]
|
||||||
|
logPrint("Found remote; "+remote+" for repository: "+repoName+" at "+repoDestination, nil)
|
||||||
return remote, nil
|
return remote, nil
|
||||||
}
|
}
|
||||||
remote, _ := findRemote(repoDestination)
|
remote, _ := findRemote(repoDestination)
|
||||||
|
|
@ -141,9 +155,13 @@ func pullRepository(repoName string, repoDestination string) {
|
||||||
switch {
|
switch {
|
||||||
case strings.Contains(string(pullOutput), "You have unstaged changes"):
|
case strings.Contains(string(pullOutput), "You have unstaged changes"):
|
||||||
pullErrorMsg = append(pullErrorMsg, repoDestination)
|
pullErrorMsg = append(pullErrorMsg, repoDestination)
|
||||||
|
logPrint("Found unstaged changes for repository: "+repoName+" at "+repoDestination, nil)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
logPrint("ERROR: pulling "+repoName, nil)
|
logPrint("ERROR: pulling "+repoName, nil)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// log activity
|
||||||
|
logPrint("Pulled repository: "+repoName+" at "+repoDestination, nil)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,13 +27,16 @@ func fetchRepositoriesGitlab() ([]Repository, error) {
|
||||||
url := fmt.Sprintf("https://%s/api/v4/projects?%s&%s&%s%s",
|
url := fmt.Sprintf("https://%s/api/v4/projects?%s&%s&%s%s",
|
||||||
gitlabHost, membership, order, perpage, archived)
|
gitlabHost, membership, order, perpage, archived)
|
||||||
|
|
||||||
|
logPrint("Creating API request", nil)
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("ERROR: creating request: %v\n", err)
|
return nil, fmt.Errorf("ERROR: creating request: %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logPrint("Adding PRIVATE-TOKEN header to API request", nil)
|
||||||
req.Header.Set("PRIVATE-TOKEN", gitlabToken)
|
req.Header.Set("PRIVATE-TOKEN", gitlabToken)
|
||||||
|
|
||||||
|
logPrint("Making request", nil)
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -45,6 +48,7 @@ func fetchRepositoriesGitlab() ([]Repository, error) {
|
||||||
return nil, fmt.Errorf("ERROR: API request failed with status: %d\n", resp.StatusCode)
|
return nil, fmt.Errorf("ERROR: API request failed with status: %d\n", resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logPrint("Decoding JSON response", nil)
|
||||||
var repositories []Repository
|
var repositories []Repository
|
||||||
if err := json.NewDecoder(resp.Body).Decode(&repositories); err != nil {
|
if err := json.NewDecoder(resp.Body).Decode(&repositories); err != nil {
|
||||||
return nil, fmt.Errorf("ERROR: decoding response: %v\n", err)
|
return nil, fmt.Errorf("ERROR: decoding response: %v\n", err)
|
||||||
|
|
@ -54,5 +58,6 @@ func fetchRepositoriesGitlab() ([]Repository, error) {
|
||||||
return repositories, fmt.Errorf("ERROR: no repositories found\n")
|
return repositories, fmt.Errorf("ERROR: no repositories found\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logPrint("Returning repositories found", nil)
|
||||||
return repositories, nil
|
return repositories, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,16 +33,15 @@ func main() {
|
||||||
// check for git
|
// check for git
|
||||||
err := verifyGitAvailable()
|
err := verifyGitAvailable()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logFatal("FATAL: git not found in path: %v", err)
|
logFatal("git not found in path: %v", err)
|
||||||
}
|
}
|
||||||
logPrint("Git is available. Proceeding with the program.", nil)
|
logPrint("VALIDATION: git found in path", nil)
|
||||||
|
|
||||||
// fetch repository information from gitlab
|
// fetch repository information from gitlab
|
||||||
repositories, err := fetchRepositoriesGitlab()
|
repositories, err := fetchRepositoriesGitlab()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logFatal("FATAL: %v", err)
|
logFatal("FATAL: %v", err)
|
||||||
}
|
}
|
||||||
logPrint("Logged into GitLab, Repositories found. Proceeding with the program.", nil)
|
|
||||||
|
|
||||||
// print progressbar ony if not in verbose mode
|
// print progressbar ony if not in verbose mode
|
||||||
if !verbose {
|
if !verbose {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue