File: version.go

package info (click to toggle)
rclone 1.60.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 34,820 kB
  • sloc: sh: 957; xml: 857; python: 655; javascript: 612; makefile: 264; ansic: 101; php: 74
file content (62 lines) | stat: -rw-r--r-- 1,786 bytes parent folder | download | duplicates (3)
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
// Package version provides machinery for versioning file names
// with a timestamp-based version string
package version

import (
	"path"
	"regexp"
	"strings"
	"time"
)

const versionFormat = "-v2006-01-02-150405.000"

var versionRegexp = regexp.MustCompile(`-v\d{4}-\d{2}-\d{2}-\d{6}-\d{3}`)

// Split fileName into base and extension so that base + ext == fileName
func splitExt(fileName string) (base, ext string) {
	ext = path.Ext(fileName)
	base = fileName[:len(fileName)-len(ext)]
	// .file splits to base == "", ext == ".file"
	// so swap ext and base in this case
	if ext != "" && base == "" {
		base, ext = ext, base
	}
	return base, ext
}

// Add returns fileName modified to include t as the version
func Add(fileName string, t time.Time) string {
	base, ext := splitExt(fileName)
	s := t.Format(versionFormat)
	// Replace the '.' with a '-'
	s = strings.ReplaceAll(s, ".", "-")
	return base + s + ext
}

// Remove returns a modified fileName without the version string and the time it represented
// If the fileName did not have a version then time.Time{} is returned along with an unmodified fileName
func Remove(fileName string) (t time.Time, fileNameWithoutVersion string) {
	fileNameWithoutVersion = fileName
	base, ext := splitExt(fileName)
	if len(base) < len(versionFormat) {
		return
	}
	versionStart := len(base) - len(versionFormat)
	// Check it ends in -xxx
	if base[len(base)-4] != '-' {
		return
	}
	// Replace with .xxx for parsing
	base = base[:len(base)-4] + "." + base[len(base)-3:]
	newT, err := time.Parse(versionFormat, base[versionStart:])
	if err != nil {
		return
	}
	return newT, base[:versionStart] + ext
}

// Match returns true if the fileName has a version string
func Match(fileName string) bool {
	return versionRegexp.MatchString(fileName)
}