diff --git a/github/api.go b/github/api.go index 82df2cc..101ee2d 100644 --- a/github/api.go +++ b/github/api.go @@ -28,7 +28,7 @@ func fetchRepoJson(repoURL string, token string) Repository { "Authorization": {"token " + token}, }) if err != nil { - logging.Printlnf("an error occurred in fetching repository %s: %v", repoURL, err) + logging.Verbosef("an error occurred in fetching repository %s: %v", repoURL, err) return Repository{} } defer resp.Body.Close() @@ -36,7 +36,7 @@ func fetchRepoJson(repoURL string, token string) Repository { var repo Repository err = decoder.Decode(&repo) if err != nil { - logging.Printlnf("could not decode JSON body for repository %s", repoURL) + logging.Verbosef("could not decode JSON body for repository %s", repoURL) return Repository{} } return repo @@ -50,7 +50,7 @@ func getRepoStars(repoURL string, token string, retries int) int { repo := fetchRepoJson(repoURL, token) if repo.Message != "" && repo.Message != "Not Found" { if retries > 0 { - logging.Printlnf("temporary error message for repo %s: %s. Retrying...", repoURL, repo.Message) + logging.Verbosef("temporary error message for repo %s: %s. Retrying...", repoURL, repo.Message) time.Sleep(500 * time.Millisecond) return getRepoStars(repoURL, token, retries-1) } else { diff --git a/logging/printer.go b/logging/printer.go index 94cc0dd..722c76f 100644 --- a/logging/printer.go +++ b/logging/printer.go @@ -12,14 +12,20 @@ func SetVerbose(b bool) { verboseFlagEnabled = b } -func Println(a ...interface{}) { +func Verbose(a ...interface{}) { if verboseFlagEnabled { fmt.Println(a...) } } -func Printlnf(format string, a ...interface{}) { +func Verbosef(format string, a ...interface{}) { if verboseFlagEnabled { fmt.Printf(format+"\n", a...) } } + +func Inlinef(format string, a ...interface{}) { + if !verboseFlagEnabled { + fmt.Printf("\r"+format, a...) + } +} diff --git a/main.go b/main.go index f80a6ea..0dd4ecf 100644 --- a/main.go +++ b/main.go @@ -36,7 +36,7 @@ func main() { log.Fatalf("A URL to the markdown file must be provided!") } link := args[0] - logging.Printlnf("URL to parse markdown: %s", link) + logging.Verbosef("URL to parse markdown: %s", link) // check file path outputFileName := *outputPtr @@ -51,6 +51,7 @@ func main() { if err != nil { log.Fatalf("failed to write to file %s: %v", outputFileName, err) } + fmt.Printf("Wrote to file: %s\n", outputFileName) } else { fmt.Println(sortedContents) } diff --git a/parser/markdown.go b/parser/markdown.go index 6535907..ab39483 100644 --- a/parser/markdown.go +++ b/parser/markdown.go @@ -75,7 +75,8 @@ type Markdown struct { } func ParseMarkdown(url string) *Markdown { - logging.Println("Retrieving markdown...") + defer fmt.Println(" Done!") + logging.Verbose("Retrieving markdown...") now := time.Now() resp, err := requests.Get(url, nil) if err != nil { @@ -83,7 +84,7 @@ func ParseMarkdown(url string) *Markdown { } defer resp.Body.Close() took := time.Now().Sub(now) - logging.Printlnf("Markdown retrieved in %v", took.String()) + logging.Verbosef("Markdown retrieved in %v", took.String()) b, err := ioutil.ReadAll(resp.Body) if err != nil { @@ -99,6 +100,7 @@ func ParseMarkdown(url string) *Markdown { start := 0 end := 0 for i, line := range lines { + logging.Inlinef("Parsing markdown for potential repository links: %d/%d lines.", i+1, len(lines)) submatches := regexUrlLine.FindStringSubmatch(line) if len(submatches) > 0 { separator := submatches[1] @@ -208,17 +210,17 @@ func readHTMLTextForGithubURL(urlString string) string { return "" } - logging.Printlnf("checking HTML from %s", urlString) + logging.Verbosef("checking HTML from %s", urlString) resp, err := requests.Get(urlString, nil) if err != nil { - logging.Printlnf("a non-fatal error occurred retrieving the HTML for url (%s): %v", urlString, err) + logging.Verbosef("a non-fatal error occurred retrieving the HTML for url (%s): %v", urlString, err) return "" } defer resp.Body.Close() htmlText, err := ioutil.ReadAll(resp.Body) if err != nil { - logging.Printlnf("a non-fatal error occurred reading the HTML for url (%s): %v", urlString, err) + logging.Verbosef("a non-fatal error occurred reading the HTML for url (%s): %v", urlString, err) return "" } @@ -249,17 +251,20 @@ func (md *Markdown) CountAll() int { } func (md *Markdown) FetchStars(token string, subBlockSize int) { + defer fmt.Println(" Done!") blockCount := len(md.blocks) - logging.Printlnf("%d blocks to fetch info for", blockCount) for i, githubBlock := range md.blocks { + logging.Inlinef("Found %d blocks of repositories. Fetching stars for blocks: %d/%d.", blockCount, i+1, blockCount) githubBlock.fetchStars(token, i, subBlockSize) } } func (md *Markdown) Sort() { + defer fmt.Println(" Done!") for blockNum, githubBlock := range md.blocks { - logging.Printlnf("Sorting block %d", blockNum) + logging.Verbosef("Sorting block %d", blockNum) + logging.Inlinef("Sorting blocks by stars: %d/%d.", blockNum+1, len(md.blocks)) sort.Sort(ByStars(githubBlock.repositories)) start := githubBlock.start @@ -281,7 +286,7 @@ func (b *GithubBlock) fetchStars(token string, blockNumber int, subBlockSize int subBlocks := int(math.Ceil(float64(repoCount) / float64(subBlockSize))) - logging.Printlnf("Started fetching stars for block %d. Splitting into %d sub-blocks of size %d", blockNumber, subBlocks, subBlockSize) + logging.Verbosef("Started fetching stars for block %d. Splitting into %d sub-blocks of size %d", blockNumber, subBlocks, subBlockSize) for i := 0; i < subBlocks; i++ { start := i * subBlockSize @@ -306,5 +311,5 @@ func (b *GithubBlock) fetchStars(token string, blockNumber int, subBlockSize int wg.Wait() } - logging.Printlnf("fetching stars for block %d done.", blockNumber) + logging.Verbosef("fetching stars for block %d done.", blockNumber) }