github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/forwarding/configuration.go (about) 1 package forwarding 2 3 import ( 4 "errors" 5 6 "github.com/mutagen-io/mutagen/pkg/filesystem" 7 ) 8 9 // EnsureValid ensures that Configuration's invariants are respected. The 10 // validation of the configuration depends on whether or not it is 11 // endpoint-specific. 12 func (c *Configuration) EnsureValid(endpointSpecific bool) error { 13 // A nil configuration is not considered valid. 14 if c == nil { 15 return errors.New("nil configuration") 16 } 17 18 // Verify that the socket overwrite mode is unspecified or supported. 19 if !(c.SocketOverwriteMode.IsDefault() || c.SocketOverwriteMode.Supported()) { 20 return errors.New("unknown or unsupported socket overwrite mode") 21 } 22 23 // Verify the socket owner specification. 24 if c.SocketOwner != "" { 25 if kind, _ := filesystem.ParseOwnershipIdentifier(c.SocketOwner); kind == filesystem.OwnershipIdentifierKindInvalid { 26 return errors.New("invalid socket owner specification") 27 } 28 } 29 30 // Verify the socket group specification. 31 if c.SocketGroup != "" { 32 if kind, _ := filesystem.ParseOwnershipIdentifier(c.SocketGroup); kind == filesystem.OwnershipIdentifierKindInvalid { 33 return errors.New("invalid socket group specification") 34 } 35 } 36 37 // We don't verify the socket permission mode because there's not really any 38 // way to know if it's a sane value. 39 40 // Success. 41 return nil 42 } 43 44 // Equal returns whether or not the configuration is equivalent to another. The 45 // result of this method is only valid if both configurations are valid. 46 func (c *Configuration) Equal(other *Configuration) bool { 47 // Ensure that both are non-nil. 48 if c == nil || other == nil { 49 return false 50 } 51 52 // Perform an equivalence check. 53 return c.SocketOverwriteMode == other.SocketOverwriteMode && 54 c.SocketOwner == other.SocketOwner && 55 c.SocketGroup == other.SocketGroup && 56 c.SocketPermissionMode == other.SocketPermissionMode 57 } 58 59 // MergeConfigurations merges two configurations of differing priorities. Both 60 // configurations must be non-nil. 61 func MergeConfigurations(lower, higher *Configuration) *Configuration { 62 // Create the resulting configuration. 63 result := &Configuration{} 64 65 // Merge the socket overwrite mode. 66 if !higher.SocketOverwriteMode.IsDefault() { 67 result.SocketOverwriteMode = higher.SocketOverwriteMode 68 } else { 69 result.SocketOverwriteMode = lower.SocketOverwriteMode 70 } 71 72 // Merge the socket owner. 73 if higher.SocketOwner != "" { 74 result.SocketOwner = higher.SocketOwner 75 } else { 76 result.SocketOwner = lower.SocketOwner 77 } 78 79 // Merge the socket group. 80 if higher.SocketGroup != "" { 81 result.SocketGroup = higher.SocketGroup 82 } else { 83 result.SocketGroup = lower.SocketGroup 84 } 85 86 // Merge the socket permission mode. 87 if higher.SocketPermissionMode != 0 { 88 result.SocketPermissionMode = higher.SocketPermissionMode 89 } else { 90 result.SocketPermissionMode = lower.SocketPermissionMode 91 } 92 93 // Done. 94 return result 95 }