/
lockfile.go
59 lines (46 loc) · 1.23 KB
/
lockfile.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
package apigen
import (
"encoding/json"
"os"
"path/filepath"
"sort"
"strings"
"github.com/pkg/errors"
)
type Lockfile struct {
Generated []string `json:"generated"`
}
var lockfile Lockfile = Lockfile{}
func addGeneratedFileToLockfile(path string) error {
path = strings.TrimPrefix(path, config.TargetDir)
path = strings.TrimPrefix(path, "/")
lockfile.Generated = append(lockfile.Generated, path)
sort.Strings(lockfile.Generated)
return nil
}
func writeLockfile() error {
lockfilePath := filepath.Join(config.TargetDir, "apigen.lock")
lockfileContents, err := json.MarshalIndent(lockfile, "", " ")
if err != nil {
return errors.Wrapf(err, "failed to marshal lockfile contents to JSON:\n%s", lockfile)
}
err = os.WriteFile(lockfilePath, lockfileContents, 0664)
if err != nil {
return errors.Wrapf(err, "failed to write lockfile with contents:\n%s", lockfileContents)
}
return nil
}
func deleteGeneratedFiles() error {
for _, file := range lockfile.Generated {
err := os.Remove(file)
if err != nil {
return errors.Wrapf(err, "failed to delete file '%s'", file)
}
}
lockfile.Generated = []string{}
err := writeLockfile()
if err != nil {
return errors.Wrap(err, "failed to write empty lockfile")
}
return nil
}