-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfile.go
63 lines (50 loc) · 1.17 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
package baur
import (
"path/filepath"
"github.com/simplesurance/baur/digest"
"github.com/simplesurance/baur/digest/sha384"
)
// File represent a file
type File struct {
repoRootPath string
relPath string
absPath string
digest *digest.Digest
}
// NewFile returns a new file
func NewFile(repoRootPath, relPath string) *File {
return &File{
repoRootPath: repoRootPath,
relPath: relPath,
absPath: filepath.Join(repoRootPath, relPath),
}
}
// Digest returns a digest of the file
func (f *File) Digest() (digest.Digest, error) {
if f.digest != nil {
return *f.digest, nil
}
sha := sha384.New()
err := sha.AddBytes([]byte(f.relPath))
if err != nil {
return digest.Digest{}, err
}
err = sha.AddFile(filepath.Join(f.absPath))
if err != nil {
return digest.Digest{}, err
}
f.digest = sha.Digest()
return *f.digest, nil
}
// Path returns it's absolute path
func (f *File) Path() string {
return f.absPath
}
// RepoRelPath returns the path relative to the baur repository
func (f *File) RepoRelPath() string {
return f.relPath
}
// String returns it's string representation
func (f *File) String() string {
return f.RepoRelPath()
}