From 4c52c2bda635f3e9f715ae3b4df3a0ebe4fe3bea Mon Sep 17 00:00:00 2001 From: Peter Karakas Date: Fri, 23 Oct 2020 22:38:08 +0200 Subject: [PATCH] Improve output - Remove the date and time from the output - Highlight errors and repo-names - Show the number of processed and remaining repos --- go.mod | 1 + go.sum | 2 ++ repo/repository.go | 45 +++++++++++++++++++++----------------------- upload/codersrank.go | 15 +++++++++------ 4 files changed, 33 insertions(+), 30 deletions(-) diff --git a/go.mod b/go.mod index fcc56e0..893e098 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 5092b99..9b6f1dd 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/repo/repository.go b/repo/repository.go index 28a49ee..f50577a 100644 --- a/repo/repository.go +++ b/repo/repository.go @@ -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 @@ -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) @@ -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 @@ -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 @@ -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 } @@ -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 { @@ -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 diff --git a/upload/codersrank.go b/upload/codersrank.go index c9daa34..2740924 100644 --- a/upload/codersrank.go +++ b/upload/codersrank.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/gookit/color" "io" "io/ioutil" "log" @@ -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 @@ -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) @@ -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 {