Passed
Push — v3.0.0 ( beea69...5114d0 )
by Serhii
01:21
created

timeago.fileContent   A

Complexity

Conditions 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
nop 1
1
package timeago
2
3
import (
4
	"encoding/json"
5
	"errors"
6
	"log"
7
	"os"
8
	"time"
9
)
10
11
func parseTimestampIntoTime(timestamp int) time.Time {
12
	return time.Unix(int64(timestamp), 0)
13
}
14
15
func getTimestampOfPastDate(subDuration time.Duration) int {
16
	return int(time.Now().Add(-subDuration).UnixNano() / 1000000000)
17
}
18
19
func parseLangSet(fileName string) *LangSet {
20
	content := fileContent(fileName)
21
22
	var res LangSet
23
24
	err := json.Unmarshal(content, &res)
25
26
	if err != nil {
27
		log.Fatalln(err)
28
	}
29
30
	return &res
31
}
32
33
func fileContent(filePath string) []byte {
34
	content, err := os.ReadFile(filePath)
35
36
	if err != nil {
37
		log.Fatalln(err)
38
	}
39
40
	return content
41
}
42
43
func fileExists(filePath string) (bool, error) {
44
	_, err := os.Stat(filePath)
45
46
	if err == nil {
47
		return true, nil
48
	}
49
50
	if errors.Is(err, os.ErrNotExist) {
51
		return false, nil
52
	}
53
54
	return false, err
55
}
56