Passed
Push — master ( 9b054d...046c7f )
by Valentin
02:39
created

util.PathsEqual   A

Complexity

Conditions 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nop 2
dl 0
loc 14
rs 9.9
c 0
b 0
f 0
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 {
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 {
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 {
29
	_, err := os.Stat(filename)
30
31
	return err == nil
32
}
33
34
func Extension(filename string) string {
35
	return strings.Trim(filepath.Ext(filename), ".")
36
}
37
38
func Abs(path string) string {
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 {
48
	p1 = strings.TrimRight(p1, "/\\")
49
	p2 = strings.TrimRight(p2, "/\\")
50
51
	if p1 == p2 {
52
		return true
53
	}
54
55
	p1 = strings.Replace(p1, "/", string(os.PathSeparator), -1)
56
	p1 = strings.Replace(p1, "\\", string(os.PathSeparator), -1)
57
	p2 = strings.Replace(p2, "/", string(os.PathSeparator), -1)
58
	p2 = strings.Replace(p2, "\\", string(os.PathSeparator), -1)
59
60
	return p1 == p2
61
	//p1to := filepath.ToSlash(p1)
62
	//p1from := filepath.FromSlash(p1)
63
	//
64
	//p2to := filepath.ToSlash(p2)
65
	//p2from := filepath.FromSlash(p2)
66
	//
67
	//if p1to == p2to || p1to == p2from || p1from == p2to || p1from == p2from {
68
	//	return true
69
	//}
70
	//
71
	//return false
72
}
73