-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
91 lines (80 loc) · 2.11 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
89
90
91
package main
import (
"bufio"
"fmt"
"log"
"os"
"github.com/alecthomas/chroma/styles"
"github.com/spf13/pflag"
)
// Check is a helper function to error check functions which can be used to error check deferred functions
func Check(f func() error) {
if err := f(); err != nil {
log.Fatal(err)
}
}
func getFileContent(filePath string) []string {
var fileContent []string
file, err := os.Open(filePath)
if err != nil {
log.Fatal(err)
}
defer Check(file.Close)
scanner := bufio.NewScanner(file)
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
for scanner.Scan() {
fileContent = append(fileContent, scanner.Text())
}
return fileContent
}
func ensureThemeExists(theme string) {
// Ensure given theme exists in registry.
if _, ok := styles.Registry[theme]; !ok {
log.Fatalln("\"" + theme + "\" is not a built-in theme.")
}
}
func main() {
var filePath string
pflag.StringVarP(&filePath, "file-name", "f", "", "Parse given file as Makefile")
help := pflag.BoolP("help", "h", false, "Print this message and exit")
all := pflag.BoolP("all", "a", false, "Display all targets including special built-in targets")
list := pflag.Bool("list-themes", false, "List built-in syntax highlighting themes")
pflag.Parse()
if *help {
// Print help message
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
pflag.PrintDefaults()
os.Exit(0)
} else if *list {
fmt.Println("Built-in themes:")
for theme := range styles.Registry {
fmt.Println("\t" + theme)
}
os.Exit(0)
} else if filePath == "" {
// Attempt to find makefile in current directory
defaultMakefileNames := []string{"GNUmakefile", "makefile", "Makefile"}
foundFile := false
for _, name := range defaultMakefileNames {
if _, err := os.Stat(name); os.IsNotExist(err) == false {
// File exists
filePath = name
foundFile = true
break
}
}
if !foundFile {
log.Fatalln("No Makefile found.")
}
}
Check(LoadConfig)
ensureThemeExists(Config.Theme)
content := NewParsedContent(filePath, getFileContent(filePath))
if *all {
content.SetIncludeSpecialTargets(true)
}
content.Parse()
Render(content, Config.Theme)
}