/
helper.go
126 lines (110 loc) · 3.09 KB
/
helper.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
// Copyright 2020 Juergen Enge, info-age GmbH, Basel. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package indexer
import (
"emperror.dev/errors"
"fmt"
"github.com/op/go-logging"
"mime"
"net/url"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
)
var _logformat = logging.MustStringFormatter(
`%{time:2006-01-02T15:04:05.000} %{module}::%{shortfunc} [%{shortfile}] > %{level:.5s} - %{message}`,
)
func Max(a, b int64) int64 {
if a > b {
return a
}
return b
}
func CreateLogger(module string, logfile string, loglevel string) (log *logging.Logger, lf *os.File) {
log = logging.MustGetLogger(module)
var err error
if logfile != "" {
lf, err = os.OpenFile(logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Errorf("Cannot open logfile %v: %v", logfile, err)
}
//defer lf.CloseInternal()
} else {
lf = os.Stderr
}
backend := logging.NewLogBackend(lf, "", 0)
backendLeveled := logging.AddModuleLevel(backend)
backendLeveled.SetLevel(logging.GetLevel(loglevel), "")
logging.SetFormatter(_logformat)
logging.SetBackend(backendLeveled)
return
}
func ClearMime(mimetype string) string {
// try to get a clean mimetype
for _, v := range strings.Split(mimetype, ",") {
t, _, err := mime.ParseMediaType(v)
if err != nil {
continue
}
return t
break
}
return mimetype
}
func FileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func _getFilePath(uri *url.URL) (string, error) {
if uri.Scheme != "file" {
return "", errors.New(fmt.Sprintf("invalid url scheme: %s", uri.Scheme))
}
filename := filepath.Clean(uri.Path)
if runtime.GOOS == "windows" {
filename = strings.TrimLeft(filename, string(filepath.Separator))
}
return filename, nil
}
var regexpDriveLetter = regexp.MustCompile("^([A-Za-z]):/(.*$)")
func pathToWSL(path string) string {
matches := regexpDriveLetter.FindStringSubmatch(filepath.ToSlash(path))
// no drive letter
if matches == nil {
return path
}
return fmt.Sprintf("/mnt/%s/%s", strings.ToLower(matches[1]), matches[2])
}
func wildCardToRegexp(pattern string) string {
components := strings.Split(pattern, "*")
if len(components) == 1 {
// if len is 1, there are no *'s, return exact match pattern
return "^" + pattern + "$"
}
var result strings.Builder
for i, literal := range components {
// Replace * with .*
if i > 0 {
result.WriteString(".*")
}
// Quote any regular expression meta characters in the
// literal text.
result.WriteString(regexp.QuoteMeta(literal))
}
return "^" + result.String() + "$"
}