This repository has been archived by the owner on Sep 22, 2023. It is now read-only.
/
utils.go
94 lines (83 loc) · 1.74 KB
/
utils.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
92
93
94
package pkg
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"regexp"
"strings"
)
const (
codeBlockPattern = `(?m)^(.*\x60{3}).*\n(.*|\n)+?\n(.*\x60{3})$`
)
func fileExists(file string) error {
if _, err := os.Stat(file); err != nil {
if os.IsNotExist(err) {
return err
}
}
return nil
}
func headerExists(link string, headers []string) bool {
link = strings.TrimPrefix(link, "#")
for _, header := range headers {
if link == strings.ToLower(strings.Replace(header, " ", "-", -1)) {
return true
}
}
return false
}
func unique(elements []string) []string {
encountered := map[string]bool{}
for v := range elements {
encountered[elements[v]] = true
}
result := []string{}
for key := range encountered {
if string(key) != "" {
result = append(result, key)
}
}
return result
}
func removeIgnoredFiles(filePaths, filesToIgnore []string) []string {
var newFilePaths []string
for _, file := range filePaths {
exists := false
for _, fileToIgnore := range filesToIgnore {
if strings.Contains(file, fileToIgnore) {
exists = true
break
}
}
if !exists {
newFilePaths = append(newFilePaths, file)
}
}
return newFilePaths
}
func readMarkdown(filePath string) (string, error) {
content, err := ioutil.ReadFile(filePath)
if err != nil {
return "", err
}
return string(content), nil
}
func removeCodeBlocks(markdown string) string {
re := regexp.MustCompile(codeBlockPattern)
return re.ReplaceAllString(markdown, "")
}
func contains(slice []string, value string) bool {
for _, el := range slice {
if value == el {
return true
}
}
return false
}
func CloseBody(closer io.Closer) {
if err := closer.Close(); err != nil {
log.Println(fmt.Sprintf("Error while closing response body: %+v", err))
}
}