-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathfile_helpers.go
64 lines (53 loc) · 1.2 KB
/
file_helpers.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
/**
* Copyright (c) F5, Inc.
*
* This source code is licensed under the Apache License, Version 2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
package files
import (
"bytes"
"fmt"
"io"
"os"
"strconv"
"time"
"github.com/gogo/protobuf/types"
)
func GetFileMode(mode string) os.FileMode {
result, err := strconv.ParseInt(mode, 8, 32)
if err != nil {
return os.FileMode(0o644)
}
return os.FileMode(result)
}
func GetPermissions(fileMode os.FileMode) string {
return fmt.Sprintf("%#o", fileMode.Perm())
}
func GetLineCount(path string) (int, error) {
reader, err := os.Open(path)
if err != nil {
return 0, fmt.Errorf("failed to read file(%s) while trying to get lineCount: %v", path, err)
}
defer reader.Close()
buf := make([]byte, 32*1024)
count := 0
lineSep := []byte{'\n'}
for {
c, err := reader.Read(buf)
count += bytes.Count(buf[:c], lineSep)
switch {
case err == io.EOF:
return count, nil
case err != nil:
return count, fmt.Errorf("failed to read file(%s): %v", path, err)
}
}
}
func TimeConvert(t time.Time) *types.Timestamp {
ts, err := types.TimestampProto(t)
if err != nil {
return types.TimestampNow()
}
return ts
}