Skip to content

Commit

Permalink
engine: resources: Improve file Cmp function
Browse files Browse the repository at this point in the history
  • Loading branch information
purpleidea committed Mar 6, 2019
1 parent a56991d commit efef260
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions engine/resources/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -848,45 +848,50 @@ func (obj *FileRes) CheckApply(apply bool) (bool, error) {

// Cmp compares two resources and returns an error if they are not equivalent.
func (obj *FileRes) Cmp(r engine.Res) error {
if !obj.Compare(r) {
return fmt.Errorf("did not compare")
}
return nil
}

// Compare two resources and return if they are equivalent.
func (obj *FileRes) Compare(r engine.Res) bool {
// we can only compare FileRes to others of the same resource kind
res, ok := r.(*FileRes)
if !ok {
return false
return fmt.Errorf("not a %s", obj.Kind())
}

if obj.path != res.path {
return false
return fmt.Errorf("the Path differs")
}
if (obj.Content == nil) != (res.Content == nil) { // xor
return false
return fmt.Errorf("the Content differs")
}
if obj.Content != nil && res.Content != nil {
if *obj.Content != *res.Content { // compare the strings
return false
return fmt.Errorf("the contents of Content differ")
}
}
if obj.Source != res.Source {
return false
return fmt.Errorf("the Source differs")
}
if obj.State != res.State {
return false
return fmt.Errorf("the State differs")
}

if obj.Owner != res.Owner {
return fmt.Errorf("the Owner differs")
}
if obj.Group != res.Group {
return fmt.Errorf("the Group differs")
}
// TODO: when we start to allow alternate representations for the mode,
// ensure that we compare in the same format. Eg: `ug=rw` == `0660`.
if obj.Mode != res.Mode {
return fmt.Errorf("the Mode differs")
}

if obj.Recurse != res.Recurse {
return false
return fmt.Errorf("the Recurse option differs")
}
if obj.Force != res.Force {
return false
return fmt.Errorf("the Force option differs")
}

return true
return nil
}

// FileUID is the UID struct for FileRes.
Expand Down

0 comments on commit efef260

Please sign in to comment.