-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
'cache destroy' command #81
Changes from all commits
7e42341
fe8c8fe
57c64e3
0170540
d8e7798
3d3a17e
a1ad2db
d65389b
f04f3c9
6599de0
211aedf
ab66894
7405c33
09d278f
c49c5de
b64ad60
7ff560b
a8be6b0
3a799a3
df909e7
69893d5
0eb0a57
4707eda
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System; | ||
using System.CommandLine.Invocation; | ||
using System.CommandLine.IO; | ||
using Corgibytes.Freshli.Cli.CommandOptions; | ||
using Corgibytes.Freshli.Cli.Extensions; | ||
using Corgibytes.Freshli.Cli.Functionality; | ||
using Corgibytes.Freshli.Lib; | ||
|
||
namespace Corgibytes.Freshli.Cli.CommandRunners.Cache | ||
{ | ||
public class CacheDestroyCommandRunner : CommandRunner<CacheDestroyCommandOptions> | ||
{ | ||
public CacheDestroyCommandRunner(IServiceProvider serviceProvider, Runner runner) | ||
: base(serviceProvider, runner) | ||
{ | ||
|
||
} | ||
|
||
public override int Run(CacheDestroyCommandOptions options, InvocationContext context) | ||
{ | ||
// Unless the --force flag is passed, prompt the user whether they want to destroy the cache | ||
if (!options.Force && !Confirm($"Do you want to completely DELETE the directory {options.CacheDir.FullName}?", context)) | ||
{ | ||
context.Console.Out.WriteLine("Operation aborted. Cache not destroyed."); | ||
return true.ToExitCode(); | ||
} | ||
|
||
// Destroy the cache | ||
context.Console.Out.WriteLine($"Destroying cache at {options.CacheDir}"); | ||
try | ||
{ | ||
return Functionality.Cache.Destroy(options.CacheDir).ToExitCode(); | ||
} | ||
// Catch errors | ||
catch (CacheException error) | ||
{ | ||
context.Console.Error.WriteLine(error.Message); | ||
return error.IsWarning.ToExitCode(); | ||
} | ||
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.CommandLine.Invocation; | ||
using System.CommandLine.IO; | ||
using Corgibytes.Freshli.Cli.CommandOptions; | ||
using Corgibytes.Freshli.Cli.Extensions; | ||
using Corgibytes.Freshli.Cli.Functionality; | ||
using Corgibytes.Freshli.Lib; | ||
|
||
namespace Corgibytes.Freshli.Cli.CommandRunners.Cache | ||
{ | ||
public class CachePrepareCommandRunner : CommandRunner<CachePrepareCommandOptions> | ||
{ | ||
public CachePrepareCommandRunner(IServiceProvider serviceProvider, Runner runner) | ||
: base(serviceProvider, runner) | ||
{ | ||
|
||
} | ||
|
||
public override int Run(CachePrepareCommandOptions options, InvocationContext context) | ||
{ | ||
context.Console.Out.WriteLine($"Preparing cache at {options.CacheDir}"); | ||
try | ||
{ | ||
return Functionality.Cache.Prepare(options.CacheDir).ToExitCode(); | ||
} | ||
catch (CacheException e) | ||
{ | ||
context.Console.Error.WriteLine(e.Message); | ||
return false.ToExitCode(); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
using Corgibytes.Freshli.Cli.CommandOptions; | ||
using System.CommandLine.Invocation; | ||
|
||
namespace Corgibytes.Freshli.Cli.CommandRunners | ||
{ | ||
public interface ICommandRunner<T> where T : CommandOptions.CommandOptions | ||
{ | ||
public int Run(T options); | ||
public int Run(T options, InvocationContext context); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.CommandLine; | ||
using Corgibytes.Freshli.Cli.CommandOptions; | ||
|
||
namespace Corgibytes.Freshli.Cli.Commands.Cache | ||
{ | ||
public class CacheDestroyCommand : RunnableCommand<CacheDestroyCommandOptions> | ||
{ | ||
public CacheDestroyCommand() | ||
: base("destroy", "Deletes the Freshli cache.") | ||
{ | ||
Option<bool> forceOption = new("--force", "Don't prompt to confirm destruction of cache.") | ||
{ | ||
Arity = ArgumentArity.ZeroOrOne | ||
}; | ||
|
||
AddOption(forceOption); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Corgibytes.Freshli.Cli.CommandOptions; | ||
|
||
namespace Corgibytes.Freshli.Cli.Commands.Cache | ||
{ | ||
public class CachePrepareCommand : RunnableCommand<CachePrepareCommandOptions> | ||
{ | ||
public CachePrepareCommand() | ||
: base("prepare", "Ensures the cache directory exists and contains a valid cache database.") | ||
{} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,21 @@ | ||
using System.CommandLine; | ||
using System.CommandLine.Invocation; | ||
using System.CommandLine.NamingConventionBinder; | ||
using Corgibytes.Freshli.Cli.CommandOptions; | ||
using Corgibytes.Freshli.Cli.CommandRunners; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Corgibytes.Freshli.Cli.Commands.Cache; | ||
|
||
namespace Corgibytes.Freshli.Cli.Commands | ||
{ | ||
public class CacheCommand : Command | ||
{ | ||
public CacheCommand() : base("cache", "Manages the local cache database") | ||
public CacheCommand() | ||
: base("cache", "Manages the local cache database") | ||
{ | ||
CachePrepareCommand prepare = new(); | ||
AddCommand(prepare); | ||
|
||
CacheDestroyCommand destroy = new(); | ||
AddCommand(destroy); | ||
} | ||
} | ||
|
||
public class CachePrepareCommand : Command | ||
{ | ||
public CachePrepareCommand() : base("prepare", | ||
"Ensures the cache directory exists and contains a valid cache database.") | ||
{ | ||
Handler = CommandHandler.Create<IHost, InvocationContext, CachePrepareCommandOptions>(Run); | ||
} | ||
|
||
private int Run(IHost host, InvocationContext context, CachePrepareCommandOptions options) | ||
{ | ||
using IServiceScope scope = host.Services.CreateScope(); | ||
ICommandRunner<CachePrepareCommandOptions> runner = scope.ServiceProvider.GetRequiredService<ICommandRunner<CachePrepareCommandOptions>>(); | ||
return runner.Run(options); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.CommandLine; | ||
using System.CommandLine.Invocation; | ||
using System.CommandLine.NamingConventionBinder; | ||
using Corgibytes.Freshli.Cli.CommandRunners; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace Corgibytes.Freshli.Cli.Commands; | ||
|
||
#nullable enable | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of just enabling nullable types in this file, I think it makes sense to enable it for the entire project. I think that belongs in a separate pull request, though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you agree with making it project-wide, then please create an issue for it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd considered it, and even tried it briefly, but it would require a fair bit of refactoring. I'll create an issue to spike it. |
||
public abstract class RunnableCommand<T> : Command where T: CommandOptions.CommandOptions | ||
{ | ||
protected RunnableCommand(string name, string? description = null) : base(name, description) | ||
{ | ||
Handler = CommandHandler.Create<IHost, InvocationContext, T>(Run); | ||
} | ||
|
||
protected virtual int Run(IHost host, InvocationContext context, T options) | ||
{ | ||
using IServiceScope scope = host.Services.CreateScope(); | ||
ICommandRunner<T> runner = scope.ServiceProvider.GetRequiredService<ICommandRunner<T>>(); | ||
return runner.Run(options, context); | ||
} | ||
} | ||
#nullable restore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These variables can be inlined: