Skip to content

Commit

Permalink
add printing of progress messages for non-verbose mode
Browse files Browse the repository at this point in the history
  • Loading branch information
kvnxiao committed May 1, 2019
1 parent 7d70b10 commit fef0e35
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
6 changes: 3 additions & 3 deletions github/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ 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()
decoder := json.NewDecoder(resp.Body)
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
Expand All @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions logging/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
}
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
23 changes: 14 additions & 9 deletions parser/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,16 @@ 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 {
log.Fatalf("an error occurred retrieving markdown: %v", err)
}
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 {
Expand All @@ -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]
Expand Down Expand Up @@ -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 ""
}

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
}

0 comments on commit fef0e35

Please sign in to comment.