Skip to content
This repository has been archived by the owner on May 10, 2021. It is now read-only.

Commit

Permalink
Improve output
Browse files Browse the repository at this point in the history
- Remove the date and time from the output
- Highlight errors and repo-names
- Show the number of processed and remaining repos
  • Loading branch information
peti2001 committed Oct 23, 2020
1 parent b9d24be commit 4c52c2b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.14
require (
github.com/go-git/go-git v4.7.0+incompatible // indirect
github.com/go-git/go-git/v5 v5.1.0
github.com/gookit/color v1.3.1
github.com/jarcoal/httpmock v1.0.6
github.com/onsi/ginkgo v1.14.2
github.com/onsi/gomega v1.10.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/gookit/color v1.3.1 h1:PPD/C7sf8u2L8XQPdPgsWRoAiLQGZEZOzU3cf5IYYUk=
github.com/gookit/color v1.3.1/go.mod h1:R3ogXq2B9rTbXoSHJ1HyUVAZ3poOJHpd9nQmyGZsfvQ=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg=
github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
Expand Down
45 changes: 21 additions & 24 deletions repo/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import (
"os/exec"
"strings"

"github.com/go-git/go-git/v5"
"github.com/gookit/color"

"github.com/codersrank-org/multi_repo_repo_extractor/config"
"github.com/codersrank-org/multi_repo_repo_extractor/entity"
"github.com/go-git/go-git/v5"
)

// RepositoryService handles repository operations like cloning, updating and processing repos
Expand Down Expand Up @@ -81,14 +83,15 @@ func (r *repositoryService) ProcessRepos(repos []*entity.Repository) []*entity.R
for _, repo := range repos {
r.ProcessedRepos++
r.CurrentRepository = repo

fmt.Printf("Extracting %s (%d/%d)\n", color.Info.Sprint(repo.Name), r.ProcessedRepos, len(repos))
err := r.clone(repo)
if err != nil {
fmt.Printf("Couldn't clone repo. Error: %s\n", color.Danger.Sprint(err.Error()))
continue
}
err = r.process(repo)
if err != nil {
log.Printf("Couldn't process repo, skipping: %s, error: %s", repo.FullName, err.Error())
fmt.Printf("Couldn't process repo. Error: %s\n", color.Danger.Sprint(err.Error()))
continue
}
processedRepos = append(processedRepos, repo)
Expand All @@ -108,15 +111,10 @@ func (r *repositoryService) clone(repo *entity.Repository) error {
repoURL := fmt.Sprintf("https://%s:%s@%s/%s", "username", r.Token, r.ProviderName, repo.FullName)
repoPath := r.SaveRepoPath + "/" + repo.FullName
err := cloneRepository(repoURL, repoPath, repo.FullName)
if err != nil {
log.Printf("Couldn't clone/update repo, skipping: %s, error: %s", repo.FullName, err.Error())
}
return nil
return err
}

func (r *repositoryService) process(repo *entity.Repository) error {
log.Printf("Processing %s", repo.FullName)

scriptPath := r.getScriptPath()
repoPath := r.SaveRepoPath + "/" + repo.FullName

Expand Down Expand Up @@ -144,18 +142,16 @@ func (r *repositoryService) process(repo *entity.Repository) error {
}

// Check if provided emails are present in the repo
r.checkEmails(targetLocation, repo.FullName)

return nil
err = r.checkEmails(targetLocation, repo.FullName)
return err
}

// Show user a warning if none of the provided emails found in the repository
func (r *repositoryService) checkEmails(fileLocation, reponame string) {
log.Printf("Checking emails for %s", reponame)
func (r *repositoryService) checkEmails(fileLocation, reponame string) error {
zipReader, err := zip.OpenReader(fileLocation)
if err != nil {
log.Printf("Couldn't read zip file for %s", reponame)
return
err := fmt.Errorf("Couldn't read zip file for %s", reponame)
return err
}
defer zipReader.Close()
var result repoAnalysisResult
Expand All @@ -164,13 +160,13 @@ func (r *repositoryService) checkEmails(fileLocation, reponame string) {
if strings.Contains(f.Name, ".json") {
configFile, err := f.Open()
if err != nil {
log.Printf("Couldn't open zip file for %s", reponame)
return
err := fmt.Errorf("Couldn't open zip file for %s", reponame)
return err
}
jsonParser := json.NewDecoder(configFile)
if err = jsonParser.Decode(&result); err != nil {
log.Printf("Couldn't parse zip file %s", reponame)
return
err := fmt.Errorf("Couldn't parse zip file %s", reponame)
return err
}
break
}
Expand All @@ -183,8 +179,11 @@ func (r *repositoryService) checkEmails(fileLocation, reponame string) {
}
}
if !emailExistsInResult {
log.Printf("None of the provided emails %s found in repo %s", r.Emails, reponame)
err := fmt.Errorf("None of the provided emails (%s) found in repo %s", strings.Join(r.Emails, ", "), reponame)
return err
}

return nil
}

func md5Hash(s string) string {
Expand All @@ -201,18 +200,16 @@ func (r *repositoryService) getScriptPath() string {
// Clone repository from given url to given path
func cloneRepository(url, path, name string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
log.Printf("Cloning %s", name)
_, err := git.PlainClone(path, false, &git.CloneOptions{
URL: url,
//Prognress: os.Stdout,
// TODO add verbose flag to show/hide these.
// Progress: os.Stdout,
})
if err != nil {
return err
}
} else {
// If exists, pull latest changes
log.Printf("Pulling latest changes for %s", name)
repo, err := git.PlainOpen(path)
if err != nil {
return err
Expand Down
15 changes: 9 additions & 6 deletions upload/codersrank.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/gookit/color"
"io"
"io/ioutil"
"log"
Expand All @@ -15,10 +16,10 @@ import (
"path/filepath"
"strings"

"github.com/codersrank-org/multi_repo_repo_extractor/entity"
"github.com/pkg/browser"

config "github.com/codersrank-org/multi_repo_repo_extractor/config"
"github.com/pkg/browser"
"github.com/codersrank-org/multi_repo_repo_extractor/entity"
)

// CodersrankService uploads and merge results with codersrank
Expand All @@ -45,14 +46,16 @@ func NewCodersrankService(c config.Config) CodersrankService {

func (c *codersrankService) UploadRepos(repos []*entity.Repository) {
uploadResults := make(map[string]string)
done := 1
for _, repo := range repos {
log.Printf("Uploading %s results", repo.FullName)
fmt.Printf("Uploading %s results (%d,%d)\n", color.Info.Sprint(repo.FullName), done, len(repos))
uploadToken, err := c.uploadRepo(repo.ID)
if err != nil {
log.Printf("Couldn't upload processed repo: %s, error: %s", repo.FullName, err.Error())
fmt.Printf("Couldn't upload, error: %s", err.Error())
continue
}
uploadResults[repo.Name] = uploadToken
done++
}
resultToken := c.uploadResults(uploadResults)
c.processResults(resultToken)
Expand Down Expand Up @@ -152,11 +155,11 @@ func (c *codersrankService) uploadResults(results map[string]string) string {

func (c *codersrankService) processResults(resultToken string) {
browserURL := c.ProcessURL + resultToken
ok := confirm(fmt.Sprintf("You are being navigated to '%s'. You wish to proceed?", browserURL))
ok := confirm(fmt.Sprintf("You are being navigated to '%s'. Do you wish to proceed?", browserURL))
if ok {
browser.OpenURL(browserURL)
} else {
fmt.Println("Finished")
color.Success.Println("Finished")
}
}
func confirm(s string) bool {
Expand Down

0 comments on commit 4c52c2b

Please sign in to comment.