-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
mfsr.go
61 lines (47 loc) · 1.05 KB
/
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
56
57
58
59
60
61
package mfsr
import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"
)
const VersionFile = "version"
type RepoPath string
func (rp RepoPath) VersionFile() string {
return path.Join(string(rp), VersionFile)
}
func (rp RepoPath) Version() (string, error) {
if rp == "" {
return "", fmt.Errorf("invalid repo path \"%s\"", rp)
}
fn := rp.VersionFile()
if _, err := os.Stat(fn); os.IsNotExist(err) {
return "", VersionFileNotFound(rp)
}
c, err := ioutil.ReadFile(fn)
if err != nil {
return "", err
}
s := string(c)
s = strings.TrimSpace(s)
return s, nil
}
func (rp RepoPath) CheckVersion(version string) error {
v, err := rp.Version()
if err != nil {
return err
}
if v != version {
return fmt.Errorf("versions differ (expected: %s, actual:%s)", version, v)
}
return nil
}
func (rp RepoPath) WriteVersion(version string) error {
fn := rp.VersionFile()
return ioutil.WriteFile(fn, []byte(version+"\n"), 0644)
}
type VersionFileNotFound string
func (v VersionFileNotFound) Error() string {
return "no version file in repo at " + string(v)
}