-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwat.go
61 lines (54 loc) · 1.52 KB
/
wat.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"strings"
)
func validateEntry(name []string, description []string, tags []string) bool {
return (len(name) > 1 && len(name[1]) > 0) &&
//(len(description) > 1 && len(description[1]) > 0) &&
(len(tags) > 1 && len(tags[1]) > 0)
}
func main() {
zshrc, err := os.Open(fmt.Sprintf("%s/.zshrc", os.Getenv("HOME")))
if err != nil {
panic(err)
}
defer zshrc.Close()
nameRegex := regexp.MustCompile("^alias (.+?)=") // (?<=^alias ).+?(?==)
descriptionRegex := regexp.MustCompile("#d:\\s?(.+?)(?: #t:|$)") // (?<= #d:).+?(?= #t:|$)
tagsRegex := regexp.MustCompile("#t:\\s?(.+?)(?: #d:|$)") // (?<= #t:).+?(?= #d:|$)
scanner := bufio.NewScanner(zshrc)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "alias ") {
nameMatches := nameRegex.FindStringSubmatch(line)
descriptionMatches := descriptionRegex.FindStringSubmatch(line)
tagsMatches := tagsRegex.FindStringSubmatch(line)
if validateEntry(nameMatches, descriptionMatches, tagsMatches) {
description := ""
if len(descriptionMatches) > 1 {
description = fmt.Sprintf("%s ", descriptionMatches[1])
}
tags := strings.Split(tagsMatches[1], ",")
var space string
if len(tags) > 0 {
space = " "
}
fmt.Printf(
"%s: %s%s[%s]\n",
strings.Trim(nameMatches[1], " "),
strings.Trim(description, " "),
space,
strings.Join(tags, ","),
)
}
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}