From 20937d05c309bc30406c62af3153d52896b9cacb Mon Sep 17 00:00:00 2001 From: James Shubin Date: Sat, 24 Aug 2019 00:28:05 -0400 Subject: [PATCH] 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. --- engine/resources/file.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/engine/resources/file.go b/engine/resources/file.go index 54f86fae..ab3ccce5 100644 --- a/engine/resources/file.go +++ b/engine/resources/file.go @@ -50,6 +50,9 @@ const ( // FileStateAbsent is the string that represents that the file should // not exist. 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 @@ -123,6 +126,10 @@ func (obj *FileRes) Validate() error { 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 _, err := obj.mode(); err != nil { 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. 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 } @@ -1046,7 +1053,7 @@ func (obj *FileRes) Copy() engine.CopyableRes { Basename: obj.Basename, Content: content, Source: obj.Source, - State: obj.State, + State: obj.State, // TODO: if this becomes a pointer, copy the string! Owner: obj.Owner, Group: obj.Group, Mode: obj.Mode,