-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
95 lines (75 loc) · 1.71 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package vcsview
import (
"os"
"strings"
)
const (
pathSeparator = string(os.PathSeparator)
)
type FileStatus string
const(
FileAdded FileStatus = "A"
FileCopied FileStatus = "C"
FileDeleted FileStatus = "D"
FileModified FileStatus = "M"
FileRenamed FileStatus = "R"
FileTyped FileStatus = "T"
FileUnmerged FileStatus = "U"
FileUnknownStatus FileStatus = "X"
)
// Project file with relative path
type File struct {
// File name
name string
// Relative file path
path string
// True if file is directory
isDir bool
// True if file is existent
isExists bool
// File size
size int64
// File access mode
mode os.FileMode
}
// Get file name (without file path)
func (f File) Name() string {
return f.name
}
// Get file relative file path (without file name)
func (f File) Path() string {
return f.path
}
// Returns relative file path with file name
func (f File) Pathname() string {
if f.path == "" || f.path == pathSeparator {
return f.name
}
return strings.TrimLeft(f.path+pathSeparator+f.name, pathSeparator)
}
// Returns true if file is directory
func (f File) IsDir() bool {
return f.isDir
}
// Returns true if file is exists at the time
func (f File) IsExists() bool {
return f.isExists
}
// Returns file bytes size
func (f File) Size() int64 {
return f.size
}
// Returns permissions for the file
func (f File) Mode() os.FileMode {
return f.mode
}
// Create new file for project repository list
// In this case file should exist on the disk
// relativePath is relative path, where file located
func NewFileFromProjectList(i os.FileInfo, relativePath string) File {
if relativePath == "." {
relativePath = ""
}
f := File{i.Name(), relativePath, i.IsDir(), true, i.Size(), i.Mode()}
return f
}