-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
156 lines (138 loc) · 2.66 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
)
func check(e error) {
if e != nil {
panic(e)
}
}
func unique(e []string) []string {
r := []string{}
for _, s := range e {
if !contains(r[:], s) {
r = append(r, s)
}
}
return r
}
func contains(arr []string, str string) bool {
for _, a := range arr {
if a == str {
return true
}
}
return false
}
func counter(arr []string) map[string]int {
var count = make(map[string]int)
for _, x := range arr {
count[x]++
}
return count
}
func insert(slice []string, index int, value string) []string {
slice = append(slice, "")
copy(slice[index+1:], slice[index:])
slice[index] = value
return slice
}
func filenameHeader(filename string) string {
/*
Prints filename header
*/
header := filename + "--------------------------------------------------------------------------"
return fmt.Sprintf("----%-25v\n", blueText.Sprint(header[:60]))
}
func outputPipe() bool {
fi, _ := os.Stdout.Stat()
if (fi.Mode() & os.ModeCharDevice) == 0 {
return true
}
return false
}
/* True if data is coming in from stdin */
func inputPipe() bool {
stat, _ := os.Stdin.Stat()
if (stat.Mode()&os.ModeCharDevice) == 0 && stat.Size() > 0 {
return true
}
return false
}
func ifelse(s bool, t string, f string) string {
if s {
return t
}
return f
}
func parseTags(s string) []string {
// Extract tags from string field
re := regexp.MustCompile(`#([A-Za-z0-9]+)`)
r := re.FindAllStringSubmatch(s, -1)
var tagSet []string
if len(r) > 0 {
for _, tags := range r {
tagSet = append(tagSet, tags[1])
}
return tagSet
}
return []string{}
}
func fetchContent(url string, ch chan *string) {
// Fetch raw content from a URL
resp, err := http.Get(url)
if err != nil {
panic(err)
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
var result = string(content)
ch <- &result
}
// True / False
func parseTrueFalse(s string) (bool, error) {
switch {
case strings.ToLower(s) == "t":
return true, nil
case strings.ToLower(s) == "f":
return false, nil
case strings.ToLower(s) == "true":
return true, nil
case strings.ToLower(s) == "false":
return false, nil
}
return false, errors.New("Unable to parse")
}
func trueFalse(s bool) string {
// Convert true and false to 'T' and 'F' b/c of
// bleve search index limitations
if s {
return "T"
}
return "F"
}
func between(x int, a int, b int) bool {
if x >= a && x < b {
return true
}
return false
}
func truncateString(str string, num int) string {
bnoden := str
if len(str) > num {
if num > 3 {
num -= 3
}
bnoden = str[0:num] + "..."
}
return bnoden
}