Completed
Push — master ( 5fa483...9b054d )
by Valentin
02:16
created

util/files.go   A

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 39
dl 0
loc 65
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A util.GetCSVReader 0 6 1
A util.Abs 0 7 2
A util.Extension 0 2 1
A util.MustOpenReadonlyFile 0 7 2
A util.FileExists 0 4 1
B util.PathsEqual 0 19 6
1
package util
2
3
import (
4
	"encoding/csv"
5
	"log"
6
	"os"
7
	"path/filepath"
8
	"strings"
9
)
10
11
func MustOpenReadonlyFile(filename string) *os.File {
1 ignored issue
show
introduced by
exported function MustOpenReadonlyFile should have comment or be unexported
Loading history...
12
	file, err := os.OpenFile(filename, os.O_RDONLY, os.ModePerm)
13
	if err != nil {
14
		log.Fatalln(err)
15
	}
16
17
	return file
18
}
19
20
func GetCSVReader(file *os.File, sep rune) *csv.Reader {
1 ignored issue
show
introduced by
exported function GetCSVReader should have comment or be unexported
Loading history...
21
	reader := csv.NewReader(file)
22
	reader.Comma = sep
23
	reader.FieldsPerRecord = -1
24
25
	return reader
26
}
27
28
func FileExists(filename string) bool {
1 ignored issue
show
introduced by
exported function FileExists should have comment or be unexported
Loading history...
29
	_, err := os.Stat(filename)
30
31
	return err == nil
32
}
33
34
func Extension(filename string) string {
1 ignored issue
show
introduced by
exported function Extension should have comment or be unexported
Loading history...
35
	return strings.Trim(filepath.Ext(filename), ".")
36
}
37
38
func Abs(path string) string {
1 ignored issue
show
introduced by
exported function Abs should have comment or be unexported
Loading history...
39
	abs, err := filepath.Abs(path)
40
	if err == nil {
41
		return abs
42
	}
43
44
	return path
45
}
46
47
func PathsEqual(p1, p2 string) bool {
1 ignored issue
show
introduced by
exported function PathsEqual should have comment or be unexported
Loading history...
48
	p1 = strings.TrimRight(p1, "/\\")
49
	p2 = strings.TrimRight(p2, "/\\")
50
51
	if p1 == p2 {
52
		return true
53
	}
54
55
	p1to := filepath.ToSlash(p1)
56
	p1from := filepath.FromSlash(p1)
57
58
	p2to := filepath.ToSlash(p2)
59
	p2from := filepath.FromSlash(p2)
60
61
	if p1to == p2to || p1to == p2from || p1from == p2to || p1from == p2from {
62
		return true
63
	}
64
65
	return false
66
}
67