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)
|
||||
repoDestination := repoDestinationPre + repoName
|
||||
|
||||
// log activity
|
||||
logPrint("Starting on repository: "+repoName, nil)
|
||||
|
||||
// make gitlab url
|
||||
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 {
|
||||
checkCmd := exec.Command("git", "-C", repoDestination, "remote", "-v")
|
||||
checkOutput, _ := checkCmd.CombinedOutput()
|
||||
logPrint("Checking status for repository: "+repoName, nil)
|
||||
|
||||
return string(checkOutput)
|
||||
}
|
||||
|
|
@ -53,10 +57,14 @@ func checkoutRepositories(repositories []Repository, concurrency int) {
|
|||
switch {
|
||||
case strings.Contains(string(repoStatus), "No such file or directory"):
|
||||
|
||||
// log activity
|
||||
logPrint("Decided to clone repository: "+repoName, nil)
|
||||
|
||||
// clone the repo
|
||||
cloneRepository := func(repoDestination string, url string) (string, error) {
|
||||
cloneCmd := exec.Command("git", "clone", url, repoDestination)
|
||||
cloneOutput, err := cloneCmd.CombinedOutput()
|
||||
logPrint("Cloning repository: "+repoName+" to "+repoDestination, nil)
|
||||
|
||||
return string(cloneOutput), err
|
||||
}
|
||||
|
|
@ -79,6 +87,7 @@ func checkoutRepositories(repositories []Repository, concurrency int) {
|
|||
|
||||
// pull the latest
|
||||
case strings.Contains(string(repoStatus), url):
|
||||
logPrint("Decided to pull repository: "+repoName, nil)
|
||||
pullRepository(repoName, repoDestination)
|
||||
if !verbose {
|
||||
descriptionPrefixPre := "Pulling repository "
|
||||
|
|
@ -88,7 +97,7 @@ func checkoutRepositories(repositories []Repository, concurrency int) {
|
|||
}
|
||||
|
||||
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)
|
||||
|
||||
// set a lock, increment counters and unlock
|
||||
|
|
@ -108,6 +117,9 @@ func checkoutRepositories(repositories []Repository, concurrency int) {
|
|||
|
||||
func pullRepository(repoName string, repoDestination string) {
|
||||
|
||||
// log activity
|
||||
logPrint("Pulling repository: "+repoName+" at "+repoDestination, nil)
|
||||
|
||||
// find remote
|
||||
findRemote := func(repoDestination string) (string, error) {
|
||||
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)
|
||||
}
|
||||
|
||||
logPrint("Finding remote for repository: "+repoName+" at "+repoDestination, nil)
|
||||
remote := strings.Split(strings.TrimSpace(string(remoteOutput)), "\n")[0]
|
||||
logPrint("Found remote; "+remote+" for repository: "+repoName+" at "+repoDestination, nil)
|
||||
return remote, nil
|
||||
}
|
||||
remote, _ := findRemote(repoDestination)
|
||||
|
|
@ -141,9 +155,13 @@ func pullRepository(repoName string, repoDestination string) {
|
|||
switch {
|
||||
case strings.Contains(string(pullOutput), "You have unstaged changes"):
|
||||
pullErrorMsg = append(pullErrorMsg, repoDestination)
|
||||
logPrint("Found unstaged changes for repository: "+repoName+" at "+repoDestination, nil)
|
||||
|
||||
default:
|
||||
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",
|
||||
gitlabHost, membership, order, perpage, archived)
|
||||
|
||||
logPrint("Creating API request", nil)
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
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)
|
||||
|
||||
logPrint("Making request", nil)
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
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)
|
||||
}
|
||||
|
||||
logPrint("Decoding JSON response", nil)
|
||||
var repositories []Repository
|
||||
if err := json.NewDecoder(resp.Body).Decode(&repositories); err != nil {
|
||||
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")
|
||||
}
|
||||
|
||||
logPrint("Returning repositories found", nil)
|
||||
return repositories, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,16 +33,15 @@ func main() {
|
|||
// check for git
|
||||
err := verifyGitAvailable()
|
||||
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
|
||||
repositories, err := fetchRepositoriesGitlab()
|
||||
if err != nil {
|
||||
logFatal("FATAL: %v", err)
|
||||
}
|
||||
logPrint("Logged into GitLab, Repositories found. Proceeding with the program.", nil)
|
||||
|
||||
// print progressbar ony if not in verbose mode
|
||||
if !verbose {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue