-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.go
81 lines (64 loc) · 1.36 KB
/
tools.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
//go:generate goversioninfo
package main
import (
"bufio"
"fmt"
"io"
"net/url"
"os"
"path"
"path/filepath"
"reflect"
"strings"
"github.com/spf13/viper"
)
func combinePath(userPath, file string) string {
return filepath.Join(userPath, file)
}
func makeURL(uri, filePath string) string {
url, err := url.Parse(uri)
if err != nil {
fmt.Printf("Incorrect URI: %v", uri)
}
url.Path = path.Join(url.Path, filePath)
return url.String()
}
func onemptyreturnzero(tag string, engine string) string {
if (tag == "") && (viper.GetBool(engine + ".onemptyreturnzero")) {
return "0"
}
return tag
}
func checkIfIsMap(typeFC interface{}) bool {
if reflect.ValueOf(typeFC).Kind() == reflect.Map {
return true
}
return false
}
func logLastLine(file string) []string {
var oldOffset int64
logFile, err := os.Open(file)
if err != nil {
fmt.Printf("File: " + file + " not found.")
os.Exit(1)
}
defer logFile.Close()
reader := bufio.NewReader(logFile)
lastLineSize := 0
for {
line, _, err := reader.ReadLine()
if err == io.EOF {
break
}
lastLineSize = len(line)
}
logStat, err := os.Stat(file)
buffer := make([]byte, lastLineSize)
offset := logStat.Size() - int64(lastLineSize+1)
numRead, err := logFile.ReadAt(buffer, offset)
if oldOffset != offset {
buffer = buffer[:numRead]
oldOffset = offset
}
return strings.Fields(string(buffer))
}