/
project.go
76 lines (63 loc) · 1.89 KB
/
project.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
package server
import "time"
// Project is a project (directory with a leat one File) in our DB
type Project struct {
Path string
Files FileMap
FileCount int
SizeCount int64
CostCount float64
LocalExpiration Expiration
RemoteExpiration Expiration
BackupEvery time.Duration
LastNoBackupAlert time.Time
Archived bool
SchemaVersion int
}
// ProjectMap is a map of Project
type ProjectMap map[string]*Project
// ProjectNewestVersion is needed because each project record have a version
// and may be upgraded as application version goes. (see Upgrade() below)
// v0: original
// v1: added SchemaVersion + BackupEvery + LastNoBackupAlert
const ProjectNewestVersion = 1
// NewProject create a new Project struct
func NewProject(path string, expirationConfig *ExpirationConfig) *Project {
project := &Project{
Path: path,
Files: make(FileMap),
BackupEvery: ProjectDefaultBackupEvery,
SchemaVersion: ProjectNewestVersion,
}
if expirationConfig != nil {
project.LocalExpiration = expirationConfig.Local
project.RemoteExpiration = expirationConfig.Remote
now := time.Now()
project.LocalExpiration.ReferenceDate = now
project.RemoteExpiration.ReferenceDate = now
} else {
project.LocalExpiration = Expiration{}
project.RemoteExpiration = Expiration{}
}
return project
}
// ModTime is the modification time of the project (aka the latest file's ModTime)
// If the project is empty, result is 0 (see IsZero())
func (p *Project) ModTime() time.Time {
latest := time.Time{}
for _, file := range p.Files {
if file.ModTime.After(latest) {
latest = file.ModTime
}
}
return latest
}
// upgrade Project record to a newest schema version if needed
func (p *Project) upgrade() error {
// v0 to v1
if p.SchemaVersion == 0 {
p.BackupEvery = ProjectDefaultBackupEvery
p.SchemaVersion = 1
}
return nil
}