-
Notifications
You must be signed in to change notification settings - Fork 1
/
mfsr.go
55 lines (43 loc) · 933 Bytes
/
mfsr.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
package mfsr
import (
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"strings"
)
const VersionFile = "version"
type RepoPath string
func (rp RepoPath) VersionFile() string {
return path.Join(string(rp), VersionFile)
}
func (rp RepoPath) Version() (int, error) {
if rp == "" {
return 0, fmt.Errorf("invalid repo path \"%s\"", rp)
}
fn := rp.VersionFile()
if _, err := os.Stat(fn); err != nil {
return 0, err
}
c, err := ioutil.ReadFile(fn)
if err != nil {
return 0, err
}
s := strings.TrimSpace(string(c))
return strconv.Atoi(s)
}
func (rp RepoPath) CheckVersion(version int) error {
v, err := rp.Version()
if err != nil {
return err
}
if v != version {
return fmt.Errorf("versions differ (expected: %d, actual:%d)", version, v)
}
return nil
}
func (rp RepoPath) WriteVersion(version int) error {
fn := rp.VersionFile()
return ioutil.WriteFile(fn, []byte(fmt.Sprintf("%d\n", version)), 0644)
}