Passed
Push — master ( b486cf...8123a4 )
by Serhii
02:01 queued 28s
created

timeago.fileExists   A

Complexity

Conditions 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.125

Importance

Changes 0
Metric Value
cc 3
eloc 7
dl 0
loc 12
ccs 3
cts 6
cp 0.5
crap 4.125
rs 10
c 0
b 0
f 0
nop 1
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
	if errors.Is(err, os.ErrNotExist) {
21
		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.Fatal(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 parseTimestampToString(timestamp int) string {
42 1
	return time.Unix(int64(timestamp), 0).Format("2006-01-02 15:04:05")
43
}
44
45
// gets the last digit from a
46
func getLastNumberDigit(num int) int {
47 1
	numStr := strconv.Itoa(num)
48 1
	result, _ := strconv.Atoi(numStr[len(numStr)-1:])
49
50 1
	return result
51
}
52
53
func parseJsonIntoLang(fileName string) Lang {
54 1
	content := getFileContent(fileName)
55
56 1
	var result Lang
57
58 1
	err := json.Unmarshal(content, &result)
59
60 1
	if err != nil {
61
		log.Fatal(err)
62
	}
63
64 1
	return result
65
}
66