/
file_exists.go
62 lines (49 loc) · 1.28 KB
/
file_exists.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
package check
import (
"context"
"errors"
"fmt"
"os"
"github.com/mongodb/amboy"
"github.com/mongodb/amboy/registry"
"github.com/mongodb/grip"
)
func init() {
fileExistsFactoryFactory := func(name string, shouldExist bool) func() amboy.Job {
return func() amboy.Job {
return &fileExistance{
ShouldExist: shouldExist,
Base: NewBase(name, 0), // (name, version)
}
}
}
name := "file-exists"
registry.AddJobType(name, fileExistsFactoryFactory(name, true))
name = "file-does-not-exist"
registry.AddJobType(name, fileExistsFactoryFactory(name, false))
}
type fileExistance struct {
FileName string `bson:"name" json:"name" yaml:"name"`
ShouldExist bool `bson:"should_exist" json:"should_exist" yaml:"should_exist"`
*Base
}
func (c *fileExistance) Run(_ context.Context) {
c.startTask()
defer c.MarkComplete()
var fileExists bool
var verb string
stat, err := os.Stat(c.FileName)
fileExists = !os.IsNotExist(err)
c.setState(fileExists == c.ShouldExist)
if fileExists != c.ShouldExist {
c.AddError(errors.New("file existence check did not detect expected state"))
}
if c.ShouldExist {
verb = "should"
} else {
verb = "should not"
}
m := fmt.Sprintf("file '%s' %s exist. stats=%+v", c.FileName, verb, stat)
grip.Debug(m)
c.setMessage(m)
}