-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathversion.go
More file actions
57 lines (49 loc) · 1023 Bytes
/
version.go
File metadata and controls
57 lines (49 loc) · 1023 Bytes
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
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package config
import (
"encoding/json"
"io/ioutil"
"path"
"time"
"xbase"
"github.com/pkg/errors"
)
const (
// versionJSONFile version file name.
versionJSONFile = "version.json"
)
// Version tuple.
type Version struct {
Ts int64 `json:"version"`
}
// UpdateVersion used to update the config version of the file.
func UpdateVersion(metadir string) error {
name := path.Join(metadir, versionJSONFile)
version := &Version{
Ts: time.Now().UnixNano(),
}
b, err := json.Marshal(version)
if err != nil {
return errors.WithStack(err)
}
return xbase.WriteFile(name, b)
}
// ReadVersion used to read the config version from the file.
func ReadVersion(metadir string) int64 {
name := path.Join(metadir, versionJSONFile)
version := &Version{}
data, err := ioutil.ReadFile(name)
if err != nil {
return 0
}
if err := json.Unmarshal([]byte(data), version); err != nil {
return 0
}
return version.Ts
}