ConfigManager: Test write access for config paths

This commit is contained in:
Jonathan Jenne
2019-07-08 12:18:58 +02:00
parent 75fc25bf94
commit b88479b8b5
3 changed files with 75 additions and 39 deletions

View File

@@ -11,8 +11,8 @@ Public Class ConfigManager(Of T)
Private ReadOnly _LogConfig As LogConfig
Private ReadOnly _Logger As Logger
Private ReadOnly _File As Filesystem.File
Private ReadOnly _UserPath As String
Private ReadOnly _ComputerPath As String
Private ReadOnly _UserDirectory As String
Private ReadOnly _ComputerDirectory As String
Private ReadOnly _TestMode As Boolean = False
Private ReadOnly _Blueprint As T
@@ -33,17 +33,17 @@ Public Class ConfigManager(Of T)
''' <returns></returns>
Public ReadOnly Property Config As T
''' <summary>
''' Path to the current user config.
''' </summary>
''' <returns></returns>
Public ReadOnly Property UserConfigPath As String
Get
Return _UserPath
End Get
End Property
''' <summary>
''' Path to the current computer config. Maybe the same as `UserConfigPath`
''' </summary>
''' <returns></returns>
Public ReadOnly Property ComputerConfigPath As String
Get
Return _ComputerPath
End Get
End Property
''' <summary>
''' Creates a new instance of the ConfigManager
@@ -58,35 +58,18 @@ Public Class ConfigManager(Of T)
_Logger = LogConfig.GetLogger()
_File = New Filesystem.File(_LogConfig)
_UserPath = Path.Combine(UserConfigPath, USER_CONFIG_NAME)
_ComputerPath = Path.Combine(ComputerConfigPath, COMPUTER_CONFIG_NAME)
_ForceUserConfig = ForceUserConfig
_Blueprint = Activator.CreateInstance(Of T)
_BlueprintType = _Blueprint.GetType
_Serializer = New XmlSerializer(_BlueprintType)
If Not Directory.Exists(UserConfigPath) Then
_Logger.Debug("UserConfigPath {0} did not exist and was created", UserConfigPath)
Directory.CreateDirectory(UserConfigPath)
End If
_UserDirectory = _File.CreateDirectory(UserConfigPath)
_ComputerDirectory = _File.CreateDirectory(ComputerConfigPath)
_ForceUserConfig = ForceUserConfig
If Not _File.TestPathIsDirectory(UserConfigPath) Then
_Logger.Warn("UserConfigPath {0} is not a directory", UserConfigPath)
Throw New ArgumentException($"Path {UserConfigPath} is not a directory!")
End If
UserConfigPath = Path.Combine(_UserDirectory, USER_CONFIG_NAME)
ComputerConfigPath = Path.Combine(_ComputerDirectory, COMPUTER_CONFIG_NAME)
If Not Directory.Exists(ComputerConfigPath) Then
_Logger.Debug("ComputerConfigPath {0} did not exist and was created", ComputerConfigPath)
Directory.CreateDirectory(ComputerConfigPath)
End If
If Not _File.TestPathIsDirectory(ComputerConfigPath) Then
_Logger.Warn("ComputerConfigPath {0} is not a directory", ComputerConfigPath)
Throw New ArgumentException($"Path {ComputerConfigPath} is not a directory!")
End If
_Config = LoadConfig()
Config = LoadConfig()
End Sub
''' <summary>
@@ -105,7 +88,7 @@ Public Class ConfigManager(Of T)
''' <returns>True if save was successful, False otherwise</returns>
Public Function Save(Optional ForceAll As Boolean = False) As Boolean
Try
WriteToFile(_Config, _UserPath, ForceAll)
WriteToFile(Config, UserConfigPath, ForceAll)
Return True
Catch ex As Exception
_Logger.Error(ex)
@@ -169,9 +152,9 @@ Public Class ConfigManager(Of T)
Private Function LoadComputerConfig(ByVal Config As T) As T
If File.Exists(_ComputerPath) Then
If File.Exists(ComputerConfigPath) Then
Try
Dim oComputerConfig = ReadFromFile(_ComputerPath)
Dim oComputerConfig = ReadFromFile(ComputerConfigPath)
CopyValues(oComputerConfig, Config)
Catch ex As Exception
_Logger.Error(ex)
@@ -186,9 +169,9 @@ Public Class ConfigManager(Of T)
End Function
Private Function LoadUserConfig(ByVal Config As T) As T
If File.Exists(_UserPath) Then
If File.Exists(UserConfigPath) Then
Try
Dim oUserConfig = ReadFromFile(_UserPath)
Dim oUserConfig = ReadFromFile(UserConfigPath)
' if user config exists
If Not IsNothing(oUserConfig) Then