forked from Azure/custom-script-extension-linux
/
seqnum.go
73 lines (65 loc) · 1.96 KB
/
seqnum.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
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"github.com/pkg/errors"
)
const (
// chmod is used to set the mode bits for new seqnum files.
chmod = os.FileMode(0600)
)
// FindSeqNumConfig gets the laster seq no from config files
func FindSeqNumConfig(path string) (int, error) {
return FindSeqNum(path, ".settings")
}
// FindSeqNumStatus gets the laster seq no from status files
func FindSeqNumStatus(path string) (int, error) {
return FindSeqNum(path, ".status")
}
// FindSeqNum finds the file with the highest number under configFolder
// named like 0.settings, 1.settings so on.
func FindSeqNum(path, ext string) (int, error) {
g, err := filepath.Glob(filepath.Join(path, fmt.Sprintf("*%s", ext)))
if err != nil {
return 0, err
}
seqs := make([]int, len(g))
for _, v := range g {
f := filepath.Base(v)
i, err := strconv.Atoi(strings.TrimSuffix(f, filepath.Ext(f)))
if err != nil {
return 0, fmt.Errorf("Can't parse int from filename: %s", f)
}
seqs = append(seqs, i)
}
if len(seqs) == 0 {
return 0, fmt.Errorf("Can't find out seqnum from %s, not enough files", path)
}
sort.Sort(sort.Reverse(sort.IntSlice(seqs)))
return seqs[0], nil
}
// SaveSeqNum replaces the stored sequence number in file, or creates a new file at
// path if it does not exist.
func SaveSeqNum(path string, num int) error {
b := []byte(fmt.Sprintf("%v", num))
return errors.Wrap(ioutil.WriteFile(path, b, chmod), "seqnum: failed to write")
}
// IsSmallerThan returns true if the sequence number stored at path is smaller
// than the provided num. If no number is stored, returns true and no
// error.
func IsSmallerThan(path string, num int) (bool, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return true, nil
}
return false, errors.Wrap(err, "seqnum: failed to read")
}
stored, err := strconv.Atoi(string(b))
return stored < num, errors.Wrapf(err, "seqnum: cannot parse number %q", b)
}