@ -5,6 +5,7 @@ using CliFx.Exceptions;
using CliFx.Infrastructure ;
using CliFx.Infrastructure ;
using Common ;
using Common ;
using JetBrains.Annotations ;
using JetBrains.Annotations ;
using Recyclarr.Command.Initialization ;
using Serilog ;
using Serilog ;
using TrashLib ;
using TrashLib ;
@ -16,40 +17,40 @@ public class CreateConfigCommand : ICommand
{
{
private readonly IFileSystem _fs ;
private readonly IFileSystem _fs ;
private readonly IAppPaths _paths ;
private readonly IAppPaths _paths ;
private readonly IDefaultAppDataSetup _appDataSetup ;
private readonly ILogger _log ;
private readonly ILogger _log ;
private string? _path ;
public CreateConfigCommand ( ILogger logger , IFileSystem fs , IAppPaths paths )
public CreateConfigCommand ( ILogger logger , IFileSystem fs , IAppPaths paths , IDefaultAppDataSetup appDataSetup )
{
{
_log = logger ;
_log = logger ;
_fs = fs ;
_fs = fs ;
_paths = paths ;
_paths = paths ;
_appDataSetup = appDataSetup ;
}
}
[ CommandOption ( "path" , 'p' , Description =
[ CommandOption ( "path" , 'p' , Description =
"Path where the new YAML file should be created. Must include the filename (e.g. path/to/config.yml). " +
"Path where the new YAML file should be created. Must include the filename (e.g. path/to/config.yml). " +
"File must not already exist. If not specified, uses the default path of `recyclarr.yml` right next to the " +
"File must not already exist. If not specified, uses the default path of `recyclarr.yml` in the app data " +
"executable." ) ]
"directory" ) ]
public string Path
public string? Path { get ; set ; }
{
get = > _path ? ? _paths . ConfigPath ;
set = > _path = value ;
}
public ValueTask ExecuteAsync ( IConsole console )
public ValueTask ExecuteAsync ( IConsole console )
{
{
_appDataSetup . SetupDefaultPath ( ) ;
var reader = new ResourceDataReader ( typeof ( Program ) ) ;
var reader = new ResourceDataReader ( typeof ( Program ) ) ;
var ymlData = reader . ReadData ( "config-template.yml" ) ;
var ymlData = reader . ReadData ( "config-template.yml" ) ;
var path = Path ? ? _paths . ConfigPath ;
if ( _fs . File . Exists ( P ath) )
if ( _fs . File . Exists ( p ath) )
{
{
throw new CommandException ( $"The file { P ath} already exists. Please choose another path or " +
throw new CommandException ( $"The file { p ath} already exists. Please choose another path or " +
"delete/move the existing file and run this command again." ) ;
"delete/move the existing file and run this command again." ) ;
}
}
_fs . Directory . CreateDirectory ( _fs . Path . GetDirectoryName ( P ath) ) ;
_fs . Directory . CreateDirectory ( _fs . Path . GetDirectoryName ( p ath) ) ;
_fs . File . WriteAllText ( P ath, ymlData ) ;
_fs . File . WriteAllText ( p ath, ymlData ) ;
_log . Information ( "Created configuration at: {Path}" , P ath) ;
_log . Information ( "Created configuration at: {Path}" , p ath) ;
return default ;
return default ;
}
}
}
}