-
Notifications
You must be signed in to change notification settings - Fork 1
/
func.go
198 lines (185 loc) · 4.88 KB
/
func.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// -----------------------------------------------------------------------------
// CMDX Utilities Suite cmdx/[func.go]
// (c) balarabe@protonmail.com License: GPLv3
// -----------------------------------------------------------------------------
package main
// checksum(s string) string
// filterLongLines(
// lines []string,
// maxLineLength int,
// ) (ret []string)
// getFilesMap(dir, filter string) FilesMap
// isHelpRequested(args []string)
// parseTime(value interface{}) time.Time
// sortUniqueStrings(a []string) []string
// splitArgsFilter(args []string) (retArgs []string, filter string)
// trim(s string) string
import (
"fmt"
"hash/crc32"
"os"
"path/filepath"
"reflect"
"sort"
"strings"
"time"
"github.com/balacode/zr"
)
// checksum returns a shortened CRC32 checksum of the given string.
// The returned checksum is a string made up of 6 hexadecimal digits,
// shorter than the 8 hex digits required for a normal CRC32.
func checksum(s string) string {
chk := crc32.ChecksumIEEE([]byte(s))
chk = (chk / 0x00FFFFFF) ^ (chk & 0x00FFFFFF) // <- from 4 to 3 bytes
return fmt.Sprintf("%06X", chk)
}
/*UNUSED:
// filterLongLines _ _
func filterLongLines(
lines []string,
longerThan int,
) (
ret []string,
) {
for i, s := range lines {
if strings.Contains(s, "\t") {
s = strings.ReplaceAll(s, "\t", " ")
}
n := len(s)
if n > longerThan && n < LongestLine {
ret = append(ret, lines[i])
}
}
return ret
}
:UNUSED*/
// getFilesMap _ _
func getFilesMap(dir, filter string) FilesMap {
filter = strings.ToLower(filter)
ret := make(FilesMap, 1000)
// TODO: use fs.WalkPath() instead of this; then remove "os" dependency
filepath.Walk(
dir, func(path string, info os.FileInfo, err error) error {
if strings.Contains(path, "$RECYCLE.BIN") {
return nil
}
if err != nil {
env.Printf("in path %s: %s\n", path, err)
return nil
}
if info.IsDir() {
return nil
}
if !strings.Contains(strings.ToLower(path), filter) {
return nil
}
size := info.Size()
ret[size] = append(ret[size], &PathAndSize{Path: path, Size: size})
return nil
},
)
return ret
}
// isHelpRequested returns true if args contains a help request.
// That is '?', 'h', 'hlp', or 'help' (or '/help', '-help', etc.)
func isHelpRequested(args []string) bool {
for _, arg := range args {
arg = strings.ToLower(strings.Trim(arg, "-/\\"))
if arg == "?" || arg == "h" || arg == "hlp" || arg == "help" {
return true
}
}
return false
}
// parseTime converts any string-like value to time.Time without returning
// an error if the conversion failed, in which case it logs an error
// and returns a zero-value time.Time.
//
// If val is a zero-length string, returns a zero-value time.Time
// but does not log a warning.
//
// It also accepts a time.Time as input.
//
// In both cases the returned Time type will contain only the date
// part without the time or time zone components.
//
// Note: fmt.Stringer (or fmt.GoStringer) interfaces are not treated as
// strings to avoid bugs from implicit conversion. Use the String method.
//
func parseTime(value interface{}) time.Time {
switch v := value.(type) {
case time.Time:
{
return v
}
case string:
{
if v == "" {
return time.Time{}
}
var tm time.Time
var err error
if len(v) == 10 {
tm, err = time.Parse("2006-01-02", v)
if err == nil && !tm.IsZero() {
return parseTime(tm)
}
}
if len(v) == 19 {
tm, err = time.Parse("2006-01-02 15:04:05", v)
if err == nil && !tm.IsZero() {
return parseTime(tm)
}
}
if err != nil {
zr.Error(err)
}
return time.Time{}
}
case *string:
if v != nil {
return parseTime(*v)
}
}
zr.Error("Can not convert", reflect.TypeOf(value), "to int:", value)
return time.Time{}
}
// sortUniqueStrings sorts string array 'a' and removes any repeated values
func sortUniqueStrings(a []string) []string {
unique := make(map[string]bool, len(a))
for _, s := range a {
unique[s] = true
}
ret := make([]string, 0, len(unique))
for s := range unique {
ret = append(ret, s)
}
// sort the lines
sort.Strings(ret)
return ret
}
// splitArgsFilter extracts '-filter expr' or '--filter expr' from args,
// and returns args with the option removed, and the extracted filter value.
func splitArgsFilter(args []string) (retArgs []string, filter string) {
//
endArg := len(args) - 1
for i := 0; i <= endArg; i++ {
arg := strings.ToLower(args[i])
if arg == "-filter" || arg == "--filter" {
if i == endArg {
env.Println(arg + " is missing its value")
return args, ""
}
filter = args[i+1]
args = args[:i+copy(args[i:], args[i+2:])]
endArg = len(args) - 1
i--
}
}
return args, filter
}
// trim removes all leading and trailing white-spaces from a string
func trim(s string) string {
return strings.TrimSpace(s)
}
// end