11/*
2- * Copyright 2021-2022 MONAI Consortium
2+ * Copyright 2021-2023 MONAI Consortium
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2020using System . Reflection ;
2121using System . Threading ;
2222using System . Threading . Tasks ;
23+ using Ardalis . GuardClauses ;
2324using Microsoft . Extensions . Logging ;
2425
2526namespace Monai . Deploy . InformaticsGateway . CLI . Services
@@ -35,13 +36,20 @@ public class ConfigurationService : IConfigurationService
3536 public bool IsConfigExists => _fileSystem . File . Exists ( Common . ConfigFilePath ) ;
3637
3738 public IConfigurationOptionAccessor Configurations { get ; }
39+ public INLogConfigurationOptionAccessor NLogConfigurations { get ; }
3840
39- public ConfigurationService ( ILogger < ConfigurationService > logger , IFileSystem fileSystem , IEmbeddedResource embeddedResource )
41+ public ConfigurationService (
42+ ILogger < ConfigurationService > logger ,
43+ IFileSystem fileSystem ,
44+ IEmbeddedResource embeddedResource ,
45+ IConfigurationOptionAccessor configurationOptionAccessor ,
46+ INLogConfigurationOptionAccessor nLogConfigurationOptionAccessor )
4047 {
4148 _logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
4249 _fileSystem = fileSystem ?? throw new ArgumentNullException ( nameof ( fileSystem ) ) ;
4350 _embeddedResource = embeddedResource ?? throw new ArgumentNullException ( nameof ( embeddedResource ) ) ;
44- Configurations = new ConfigurationOptionAccessor ( fileSystem ) ;
51+ Configurations = configurationOptionAccessor ?? throw new ArgumentNullException ( nameof ( configurationOptionAccessor ) ) ;
52+ NLogConfigurations = nLogConfigurationOptionAccessor ?? throw new ArgumentNullException ( nameof ( nLogConfigurationOptionAccessor ) ) ;
4553 }
4654
4755 public void CreateConfigDirectoryIfNotExist ( )
@@ -55,22 +63,32 @@ public void CreateConfigDirectoryIfNotExist()
5563 public async Task Initialize ( CancellationToken cancellationToken )
5664 {
5765 _logger . DebugMessage ( "Reading default application configurations..." ) ;
58- using var stream = _embeddedResource . GetManifestResourceStream ( Common . AppSettingsResourceName ) ;
66+ await WriteConfigFile ( Common . AppSettingsResourceName , Common . ConfigFilePath , cancellationToken ) . ConfigureAwait ( false ) ;
67+ await WriteConfigFile ( Common . NLogConfigResourceName , Common . NLogConfigFilePath , cancellationToken ) . ConfigureAwait ( false ) ;
68+ }
69+
70+ public async Task WriteConfigFile ( string resourceName , string outputPath , CancellationToken cancellationToken )
71+ {
72+ Guard . Against . NullOrWhiteSpace ( resourceName , nameof ( resourceName ) ) ;
73+ Guard . Against . NullOrWhiteSpace ( outputPath , nameof ( outputPath ) ) ;
74+
75+ using var stream = _embeddedResource . GetManifestResourceStream ( resourceName ) ;
5976
6077 if ( stream is null )
6178 {
6279 _logger . AvailableManifest ( string . Join ( "," , Assembly . GetExecutingAssembly ( ) . GetManifestResourceNames ( ) ) ) ;
63- throw new ConfigurationException ( $ "Default configuration file could not be loaded, please reinstall the CLI.") ;
80+ throw new ConfigurationException ( $ "Default configuration file: { resourceName } could not be loaded, please reinstall the CLI.") ;
6481 }
6582 CreateConfigDirectoryIfNotExist ( ) ;
6683
67- _logger . SaveAppSettings ( Common . ConfigFilePath ) ;
68- using ( var fileStream = _fileSystem . FileStream . Create ( Common . ConfigFilePath , FileMode . Create ) )
84+ _logger . SaveAppSettings ( resourceName , outputPath ) ;
85+ using ( var fileStream = _fileSystem . FileStream . Create ( outputPath , FileMode . Create ) )
6986 {
7087 await stream . CopyToAsync ( fileStream , cancellationToken ) . ConfigureAwait ( false ) ;
7188 await fileStream . FlushAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
7289 }
73- _logger . AppSettingUpdated ( Common . ConfigFilePath ) ;
90+ _logger . AppSettingUpdated ( outputPath ) ;
91+
7492 }
7593 }
7694}
0 commit comments