diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..6e1b7c3 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,10 @@ +- repo: https://github.com/jessp01/pre-commit-golang + rev: v0.5.3 + hooks: + - id: go-fmt + - id: go-imports + - id: go-vet + - id: go-lint + - id: go-critic + - id: go-ineffassign + #- id: go-cyclo diff --git a/examples/syncat.go b/examples/syncat.go index ba66cf7..fb3c229 100644 --- a/examples/syncat.go +++ b/examples/syncat.go @@ -9,7 +9,7 @@ import ( "strings" "github.com/fatih/color" - "github.com/jessp01/gohighlight" + highlight "github.com/jessp01/gohighlight" ) func main() { @@ -20,15 +20,15 @@ func main() { gopath := os.Getenv("GOPATH") - var syn_dir string + var synDir string if gopath == "" { - syn_dir = os.Getenv("HOME") + "/.config/zaje/syntax_files" + synDir = os.Getenv("HOME") + "/.config/zaje/syntax_files" } else { - syn_dir = gopath + "/src/github.com/jessp01/gohighlight/syntax_files" + synDir = gopath + "/src/github.com/jessp01/gohighlight/syntax_files" } var defs []*highlight.Def - err, warnings := highlight.ParseSyntaxFiles(syn_dir, &defs) + warnings, err := highlight.ParseSyntaxFiles(synDir, &defs) if err != nil { log.Fatal(err) } @@ -67,8 +67,6 @@ func main() { color.Set(color.FgHiBlue) case highlight.Groups["preproc"]: - //fallthrough - //case highlight.Groups["high.red"]: color.Set(color.FgHiRed) case highlight.Groups["special"]: diff --git a/helpers_test.go b/helpers_test.go index 257e4f0..84bd7cf 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -7,14 +7,14 @@ import ( "testing" ) -func getDefs(t *testing.T, filename string, data []byte, highlight_lexer string) *Def { +func getDefs(t *testing.T, filename string, data []byte, highlightLexer string) *Def { var def *Def - syn_dir := "./syntax_files" + synDir := "./syntax_files" var defs []*Def - lerr, warnings := ParseSyntaxFiles(syn_dir, &defs) + warnings, lerr := ParseSyntaxFiles(synDir, &defs) if lerr != nil { - t.Errorf("Couldn't get defs from '%s', error: %v\n", syn_dir, lerr) + t.Errorf("Couldn't get defs from '%s', error: %v\n", synDir, lerr) } if len(warnings) > 0 { t.Errorf("Parsing ended with warnings: '%s'\n", strings.Join(warnings, ";")) @@ -28,8 +28,8 @@ func getDefs(t *testing.T, filename string, data []byte, highlight_lexer string) } // if a specific lexer was requested by setting the ENV var, try to load it - if highlight_lexer != "" { - syntaxFile, lerr := ioutil.ReadFile(syn_dir + "/" + highlight_lexer + ".yaml") + if highlightLexer != "" { + syntaxFile, lerr := ioutil.ReadFile(synDir + "/" + highlightLexer + ".yaml") if lerr == nil { // Parse it into a `*highlight.Def` def, _ = ParseDef(syntaxFile) @@ -40,6 +40,5 @@ func getDefs(t *testing.T, filename string, data []byte, highlight_lexer string) t.Errorf("Found no defs for '%s'\n", filename) } - //fmt.Println(def) return def } diff --git a/highlighter.go b/highlighter.go index f5a01d5..e7e0b44 100644 --- a/highlighter.go +++ b/highlighter.go @@ -62,30 +62,29 @@ func NewHighlighter(def *Def) *Highlighter { // color's group (represented as one byte) type LineMatch map[int]Group -func ParseSyntaxFiles(dir string, defs *[]*Def) (error, []string) { +// ParseSyntaxFiles builds def array from YAML files in dir +func ParseSyntaxFiles(dir string, defs *[]*Def) ([]string, error) { pattern := "*.yaml" files, err := filepath.Glob(dir + "/" + pattern) var warnings []string if err != nil { - return err, nil + return nil, err } - for _, file_path := range files { - file, err := ioutil.ReadFile(file_path) + for _, filePath := range files { + file, err := ioutil.ReadFile(filePath) if err != nil { - warnings = append(warnings, file_path+": "+err.Error()) - } else { - if len(file) > 0 { - d, err := ParseDef(file) - if err != nil { - warnings = append(warnings, file_path+": "+err.Error()) - } else { - *defs = append(*defs, d) - } + warnings = append(warnings, filePath+": "+err.Error()) + } else if len(file) > 0 { + d, err := ParseDef(file) + if err != nil { + warnings = append(warnings, filePath+": "+err.Error()) + } else { + *defs = append(*defs, d) } } } - return err, warnings + return warnings, err } func findIndex(regex *regexp.Regexp, skip *regexp.Regexp, str []rune, canMatchStart, canMatchEnd bool) []int { diff --git a/highlighter_test.go b/highlighter_test.go index c78a009..af6fdb2 100644 --- a/highlighter_test.go +++ b/highlighter_test.go @@ -10,10 +10,10 @@ import ( func TestInputs(t *testing.T) { - test_inputs_dir := "./test_inputs/*" - files, err := filepath.Glob(test_inputs_dir) + testInputsDir := "./test_inputs/*" + files, err := filepath.Glob(testInputsDir) if err != nil { - t.Errorf("Couldn't open '%s', error: %v\n", test_inputs_dir, err) + t.Errorf("Couldn't open '%s', error: %v\n", testInputsDir, err) } for _, filename := range files { ext := strings.Split(filename, ".") @@ -23,7 +23,7 @@ func TestInputs(t *testing.T) { exp := ext[len(ext)-1] if def.FileType != exp { t.Errorf("\nInput [%#v]\nExpected[%#v]\nGot [%#v]\n", - //string(data), exp, def.FileType) + // string(data), exp, def.FileType) filename, exp, def.FileType) } } diff --git a/parser.go b/parser.go index 6bf9270..49b2e50 100644 --- a/parser.go +++ b/parser.go @@ -87,11 +87,11 @@ func ParseDef(input []byte) (s *Def, err error) { s = new(Def) for k, v := range rules { - if k == "filetype" { + switch k { + case "filetype": filetype := v.(string) - s.FileType = filetype - } else if k == "detect" { + case "detect": ftdetect := v.(map[interface{}]interface{}) if len(ftdetect) >= 1 { syntax, err := regexp.Compile(ftdetect["filename"].(string)) @@ -109,7 +109,7 @@ func ParseDef(input []byte) (s *Def, err error) { s.ftdetect = append(s.ftdetect, header) } - } else if k == "rules" { + case "rules": inputRules := v.([]interface{}) rules, err := parseRules(inputRules, nil) diff --git a/syntax_files/syntax_checker.go b/syntax_files/syntax_checker.go index bdffe3a..d08d281 100644 --- a/syntax_files/syntax_checker.go +++ b/syntax_files/syntax_checker.go @@ -5,7 +5,7 @@ import ( "io/ioutil" "strings" - "github.com/jessp01/gohighlight" + highlight "github.com/jessp01/gohighlight" ) func main() { diff --git a/test_inputs/1.c b/test_inputs/1.c index 46ade95..a091b4d 100644 --- a/test_inputs/1.c +++ b/test_inputs/1.c @@ -1,3 +1,4 @@ +//nolint #include int main(int argc, char *argv[]) { printf("%s\n", "Hello world!"); diff --git a/test_inputs/1.df b/test_inputs/1.df new file mode 100644 index 0000000..ea08672 --- /dev/null +++ b/test_inputs/1.df @@ -0,0 +1,13 @@ +Filesystem Size Used Avail Use% Mounted on +udev 7.7G 0 7.7G 0% /dev +tmpfs 1.6G 3.1M 1.6G 1% /run +/dev/nvme0n1p2 23G 21G 673M 97% / +tmpfs 7.7G 55M 7.7G 1% /dev/shm +tmpfs 5.0M 4.0K 5.0M 1% /run/lock +/dev/nvme0n1p5 1.8G 271M 1.5G 16% /tmp +/dev/nvme0n1p3 9.1G 7.9G 737M 92% /var +/dev/nvme0n1p6 434G 230G 181G 56% /home +/dev/nvme0n1p1 511M 3.5M 508M 1% /boot/efi +tmpfs 1.6G 25M 1.6G 2% /run/user/1000 +tmpfs 1.6G 92K 1.6G 1% /run/user/0 + diff --git a/test_inputs/1.go b/test_inputs/1.go new file mode 100644 index 0000000..a330258 --- /dev/null +++ b/test_inputs/1.go @@ -0,0 +1,16 @@ +package main + +import ( + "fmt" + "os" + "path/filepath" +) + +func main() { + ex, err := os.Executable() + if err != nil { + panic(err) + } + exPath := filepath.Dir(ex) + fmt.Println(exPath) +} diff --git a/test_inputs/1.netstat b/test_inputs/1.netstat new file mode 100644 index 0000000..3163925 --- /dev/null +++ b/test_inputs/1.netstat @@ -0,0 +1,30 @@ +Active Internet connections (only servers) +Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name +tcp 0 0 0.0.0.0:35211 0.0.0.0:* LISTEN - +tcp 0 0 0.0.0.0:51435 0.0.0.0:* LISTEN 1798003/rpc.mountd +tcp 0 0 0.0.0.0:2049 0.0.0.0:* LISTEN - +tcp 0 0 0.0.0.0:51311 0.0.0.0:* LISTEN 1797998/rpc.statd +tcp 0 0 127.0.0.1:53 0.0.0.0:* LISTEN 1803502/connmand +tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 2384685/cupsd +tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 1798145/mariadbd +tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1798150/sshd: /usr/ +tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN 1798810/master +tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd +tcp 0 0 127.0.0.1:35765 0.0.0.0:* LISTEN 878/containerd +tcp 0 0 127.0.0.1:3000 0.0.0.0:* LISTEN 2082789/node +tcp 0 0 0.0.0.0:46613 0.0.0.0:* LISTEN 1798003/rpc.mountd +tcp 0 0 0.0.0.0:54439 0.0.0.0:* LISTEN 1798003/rpc.mountd +tcp6 0 0 :::2049 :::* LISTEN - +tcp6 0 0 :::36647 :::* LISTEN 1798003/rpc.mountd +tcp6 0 0 ::1:631 :::* LISTEN 2384685/cupsd +tcp6 0 0 ::1:53 :::* LISTEN 1803502/connmand +tcp6 0 0 :::443 :::* LISTEN 1803521/apache2 +tcp6 0 0 :::33167 :::* LISTEN - +tcp6 0 0 :::22 :::* LISTEN 1798150/sshd: /usr/ +tcp6 0 0 :::25 :::* LISTEN 1798810/master +tcp6 0 0 :::111 :::* LISTEN 1/systemd +tcp6 0 0 :::80 :::* LISTEN 1803521/apache2 +tcp6 0 0 :::49243 :::* LISTEN 1798003/rpc.mountd +tcp6 0 0 :::55779 :::* LISTEN 1798003/rpc.mountd +tcp6 0 0 :::57253 :::* LISTEN 1797998/rpc.statd +tcp6 0 0 :::7373 :::* LISTEN 1803521/apache2 diff --git a/test_inputs/1.ping b/test_inputs/1.ping new file mode 100644 index 0000000..86ccbd9 --- /dev/null +++ b/test_inputs/1.ping @@ -0,0 +1,9 @@ +PING debian.org (149.20.4.15) 56(84) bytes of data. +64 bytes from mirror-isc3.debian.org (149.20.4.15): icmp_seq=1 ttl=50 time=155 ms +64 bytes from mirror-isc3.debian.org (149.20.4.15): icmp_seq=2 ttl=50 time=152 ms +64 bytes from mirror-isc3.debian.org (149.20.4.15): icmp_seq=3 ttl=50 time=160 ms +64 bytes from mirror-isc3.debian.org (149.20.4.15): icmp_seq=4 ttl=50 time=173 ms + +--- debian.org ping statistics --- +4 packets transmitted, 4 received, 0% packet loss, time 3003ms +rtt min/avg/max/mdev = 151.851/160.021/172.823/7.983 ms diff --git a/test_inputs/1.traceroute b/test_inputs/1.traceroute new file mode 100644 index 0000000..a5d1829 --- /dev/null +++ b/test_inputs/1.traceroute @@ -0,0 +1,16 @@ +traceroute to debian.org (149.20.4.15), 30 hops max, 60 byte packets + 1 192.168.25.122 (192.168.25.122) 2.635 ms 3.320 ms 3.403 ms + 2 * * * + 3 * * * + 4 * * * + 5 * * * + 6 172.19.136.161 (172.19.136.161) 294.118 ms 379.170 ms 379.095 ms + 7 * * * + 8 172.19.17.154 (172.19.17.154) 25.121 ms 16.297 ms * + 9 195.66.227.6 (195.66.227.6) 26.169 ms 26.129 ms 22.198 ms +10 * * * +11 * * * +12 * * * +13 * * * +14 he.pao1.isc.org (72.52.94.70) 154.700 ms 153.893 ms 163.018 ms +15 mirror-isc3.debian.org (149.20.4.15) 162.062 ms 143.122 ms 165.659 ms