-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
74 lines (61 loc) · 1.91 KB
/
Copy pathutils.go
File metadata and controls
74 lines (61 loc) · 1.91 KB
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
package main
import (
"fmt"
"path/filepath"
"sort"
"strings"
"time"
)
// SortBackupList sorts a passed backup folder names slice in ASC (most recent one last)
// or DESC (oldest one last) direction, depending on the second parameter
func SortBackupList(backups *[]string, desc bool) {
sort.SliceStable(*backups, func(i, j int) bool {
iBasename := filepath.Base((*backups)[i])
iDate, err := BackupNameToTime(iBasename)
if err != nil {
panic(fmt.Sprintf("DetermineLastBackup: error parsing backup folder %s into time: %v", iBasename, err))
}
jBasename := filepath.Base((*backups)[j])
jDate, err := BackupNameToTime(jBasename)
if err != nil {
panic(fmt.Sprintf("DetermineLastBackup: error parsing backup folder %s into time: %v", jBasename, err))
}
if desc {
if iDate.After(jDate) {
return true
}
} else {
if iDate.Before(jDate) {
return true
}
}
return false
})
}
// BackupNameToTime takes a backup name as string and returns the corresponding time instance
// Returns error if name could not be parsed
func BackupNameToTime(backupName string) (time.Time, error) {
iDate, err := time.Parse(BackupFolderTimeFormat, backupName)
if err != nil {
return time.Now(), err
}
return iDate, nil
}
// NormalizeFolderPath ensures a folder path is well-formed and ends with a slash
func NormalizeFolderPath(dirtyPath string) string {
path := filepath.Clean(dirtyPath)
if !strings.HasSuffix(path, "/") {
path = fmt.Sprintf("%v/", path)
}
return path
}
// DetermineNewestBackupInFolder fetches all backup folder names in the target path and determines the most
// most recent one, returning its relative path relative to the target folder
func DetermineNewestBackupInFolder(options *Options, targetPath string) string {
backups := ListBackupsInPath(options, targetPath, targetPath)
if len(backups) > 0 {
SortBackupList(&backups, false)
return backups[len(backups)-1]
}
return ""
}