engine: resources: file: Add undefined file state and validate it

We should consider using *string instead of the empty string, but let's
keep the diff smaller for now.
This commit is contained in:
James Shubin
2019-08-24 00:28:05 -04:00
parent 4943d37ccf
commit 20937d05c3

View File

@@ -50,6 +50,9 @@ const (
// FileStateAbsent is the string that represents that the file should // FileStateAbsent is the string that represents that the file should
// not exist. // not exist.
FileStateAbsent = "absent" FileStateAbsent = "absent"
// FileStateUndefined means the file state has not been specified.
// TODO: consider moving to *string and express this state as a nil.
FileStateUndefined = ""
) )
// 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
@@ -123,6 +126,10 @@ func (obj *FileRes) Validate() error {
return fmt.Errorf("can't specify Content when creating a Dir") return fmt.Errorf("can't specify Content when creating a Dir")
} }
if obj.State != FileStateExists && obj.State != FileStateAbsent && obj.State != FileStateUndefined {
return fmt.Errorf("the State is invalid")
}
if obj.Mode != "" { if obj.Mode != "" {
if _, err := obj.mode(); err != nil { if _, err := obj.mode(); err != nil {
return err return err
@@ -600,7 +607,7 @@ func (obj *FileRes) syncCheckApply(apply bool, src, dst string) (bool, error) {
// state performs a CheckApply of the file state to create an empty file. // state performs a CheckApply of the file state to create an empty file.
func (obj *FileRes) stateCheckApply(apply bool) (bool, error) { func (obj *FileRes) stateCheckApply(apply bool) (bool, error) {
if obj.State == "" { // state is not specified if obj.State == FileStateUndefined { // state is not specified
return true, nil return true, nil
} }
@@ -1046,7 +1053,7 @@ func (obj *FileRes) Copy() engine.CopyableRes {
Basename: obj.Basename, Basename: obj.Basename,
Content: content, Content: content,
Source: obj.Source, Source: obj.Source,
State: obj.State, State: obj.State, // TODO: if this becomes a pointer, copy the string!
Owner: obj.Owner, Owner: obj.Owner,
Group: obj.Group, Group: obj.Group,
Mode: obj.Mode, Mode: obj.Mode,