engine: resources: file: Use constants for state values
More robustness is yay!
This commit is contained in:
@@ -43,6 +43,15 @@ func init() {
|
|||||||
engine.RegisterResource("file", func() engine.Res { return &FileRes{} })
|
engine.RegisterResource("file", func() engine.Res { return &FileRes{} })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
// FileStateExists is the string that represents that the file should be
|
||||||
|
// present.
|
||||||
|
FileStateExists = "exists"
|
||||||
|
// FileStateAbsent is the string that represents that the file should
|
||||||
|
// not exist.
|
||||||
|
FileStateAbsent = "absent"
|
||||||
|
)
|
||||||
|
|
||||||
// FileRes is a file and directory resource. Dirs are defined by names ending
|
// FileRes is a file and directory resource. Dirs are defined by names ending
|
||||||
// in a slash.
|
// in a slash.
|
||||||
type FileRes struct {
|
type FileRes struct {
|
||||||
@@ -84,7 +93,7 @@ type FileRes struct {
|
|||||||
// Default returns some sensible defaults for this resource.
|
// Default returns some sensible defaults for this resource.
|
||||||
func (obj *FileRes) Default() engine.Res {
|
func (obj *FileRes) Default() engine.Res {
|
||||||
return &FileRes{
|
return &FileRes{
|
||||||
State: "exists",
|
State: FileStateExists,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -601,11 +610,11 @@ func (obj *FileRes) stateCheckApply(apply bool) (bool, error) {
|
|||||||
return false, errwrap.Wrapf(err, "could not stat file")
|
return false, errwrap.Wrapf(err, "could not stat file")
|
||||||
}
|
}
|
||||||
|
|
||||||
if obj.State == "absent" && os.IsNotExist(err) {
|
if obj.State == FileStateAbsent && os.IsNotExist(err) {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if obj.State == "exists" && err == nil {
|
if obj.State == FileStateExists && err == nil {
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -614,7 +623,7 @@ func (obj *FileRes) stateCheckApply(apply bool) (bool, error) {
|
|||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if obj.State == "absent" {
|
if obj.State == FileStateAbsent {
|
||||||
return false, nil // defer the work to contentCheckApply
|
return false, nil // defer the work to contentCheckApply
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -639,7 +648,7 @@ func (obj *FileRes) stateCheckApply(apply bool) (bool, error) {
|
|||||||
func (obj *FileRes) contentCheckApply(apply bool) (bool, error) {
|
func (obj *FileRes) contentCheckApply(apply bool) (bool, error) {
|
||||||
obj.init.Logf("contentCheckApply(%t)", apply)
|
obj.init.Logf("contentCheckApply(%t)", apply)
|
||||||
|
|
||||||
if obj.State == "absent" {
|
if obj.State == FileStateAbsent {
|
||||||
if _, err := os.Stat(obj.getPath()); os.IsNotExist(err) {
|
if _, err := os.Stat(obj.getPath()); os.IsNotExist(err) {
|
||||||
// no such file or directory, but
|
// no such file or directory, but
|
||||||
// file should be missing, phew :)
|
// file should be missing, phew :)
|
||||||
@@ -701,7 +710,7 @@ func (obj *FileRes) contentCheckApply(apply bool) (bool, error) {
|
|||||||
func (obj *FileRes) chmodCheckApply(apply bool) (bool, error) {
|
func (obj *FileRes) chmodCheckApply(apply bool) (bool, error) {
|
||||||
obj.init.Logf("chmodCheckApply(%t)", apply)
|
obj.init.Logf("chmodCheckApply(%t)", apply)
|
||||||
|
|
||||||
if obj.State == "absent" {
|
if obj.State == FileStateAbsent {
|
||||||
// file is absent
|
// file is absent
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
@@ -747,7 +756,7 @@ func (obj *FileRes) chownCheckApply(apply bool) (bool, error) {
|
|||||||
var expectedUID, expectedGID int
|
var expectedUID, expectedGID int
|
||||||
obj.init.Logf("chownCheckApply(%t)", apply)
|
obj.init.Logf("chownCheckApply(%t)", apply)
|
||||||
|
|
||||||
if obj.State == "absent" {
|
if obj.State == FileStateAbsent {
|
||||||
// file is absent or no owner specified
|
// file is absent or no owner specified
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user