-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
88 lines (75 loc) · 2.1 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/kvnxiao/sort-awesome-lists/logging"
"github.com/kvnxiao/sort-awesome-lists/parser"
)
func main() {
// flags setup
tokenPtr := flag.String("t", "", "GitHub personal access token")
verbosePtr := flag.Bool("v", false, "prints debug messages to stdout if true (default = false)")
outputPtr := flag.String("o", "", "name of file to write output to if set, otherwise prints to stdout")
subBlockSizePtr := flag.Int("bs", 5, "number of concurrent requests to send to GitHub API at a time, per each block found.")
flag.Parse()
// read token
token := *tokenPtr
if token == "" {
log.Fatalf("Please pass in a GitHub personal access token before using")
}
// set verbosity in logger
verbose := *verbosePtr
logging.SetVerbose(verbose)
// parse args for link
args := flag.Args()
if len(args) < 1 {
log.Fatalf("A URL to the markdown file must be provided!")
}
link := args[0]
logging.Verbosef("URL to parse markdown: %s", link)
// check file path
outputFileName := *outputPtr
outputFilePath := checkFilePath(outputFileName)
// parse and sort markdown by number of github stars
md := parseAndSort(link, token, *subBlockSizePtr)
sortedContents := md.ToString()
if outputFilePath != "" {
err := ioutil.WriteFile(outputFileName, []byte(sortedContents), 0666)
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)
}
}
func checkFilePath(path string) string {
if path == "" {
return ""
}
absPath, err := filepath.Abs(path)
if err != nil {
log.Fatalf("specified output path is invalid: %s", absPath)
}
if fileExists(absPath) {
log.Fatalf("file already exists in path %s", absPath)
}
return absPath
}
func fileExists(path string) bool {
if _, err := os.Stat(path); err == nil {
return true
} else {
return false
}
}
func parseAndSort(link, token string, subBlockSize int) *parser.Markdown {
md := parser.ParseMarkdown(link)
md.FetchStars(token, subBlockSize)
md.Sort()
return md
}