Passed
Push — master ( 1dbab0...58c7cb )
by Serhii
02:41 queued 50s
created

timeago.fatalIfError   A

Complexity

Conditions 2

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 6
rs 10
c 0
b 0
f 0
nop 2
1
package timeago
2
3
import (
4
	"encoding/json"
5
	"errors"
6
	"io/ioutil"
7
	"log"
8
	"os"
9
	"strconv"
10
	"time"
11
)
12
13
func fileExists(filePath string) (bool, error) {
14 1
	_, err := os.Stat(filePath)
15
16 1
	if err == nil {
17 1
		return true, nil
18
	}
19
20 1
	if errors.Is(err, os.ErrNotExist) {
21 1
		return false, nil
22
	}
23
24
	return false, err
25
}
26
27
func getFileContent(filePath string) []byte {
28 1
	content, err := ioutil.ReadFile(filePath)
29
30 1
	if err != nil {
31
		log.Fatalln(err)
32
	}
33
34 1
	return content
35
}
36
37
func getTimestampOfPastDate(subDuration time.Duration) int {
38 1
	return int(time.Now().Add(-subDuration).UnixNano() / 1000000000)
39
}
40
41
func parseTimestampToTime(timestamp int) time.Time {
42 1
	return time.Unix(int64(timestamp), 0)
43
}
44
45
func getLastNumberDigit(num int) int {
46 1
	numStr := strconv.Itoa(num)
47 1
	result, _ := strconv.Atoi(numStr[len(numStr)-1:])
48
49 1
	return result
50
}
51
52
func parseJsonIntoLang(fileName string) lang {
53 1
	content := getFileContent(fileName)
54
55 1
	var result lang
56
57 1
	err := json.Unmarshal(content, &result)
58
59 1
	if err != nil {
60
		log.Fatalln(err)
61
	}
62
63 1
	return result
64
}
65