Skip to content

Commit

Permalink
Clean up delete-all command
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianstevens committed Jan 10, 2025
1 parent 5f9dde0 commit 41f42bb
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 29 deletions.
75 changes: 75 additions & 0 deletions Source/v2/Meadow.CLI/Commands/Current/File/FileDeleteAllCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using CliFx.Attributes;
using Meadow.Hcom;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;

[Command("file delete-all", Description = "Deletes all files from the device")]
public class FileDeleteAllCommand : BaseDeviceCommand<FileDeleteCommand>
{
public FileDeleteAllCommand(MeadowConnectionManager connectionManager, ILoggerFactory loggerFactory)
: base(connectionManager, loggerFactory)
{ }

protected override async ValueTask ExecuteCommand()
{
var connection = await GetCurrentConnection();
var device = await GetCurrentDevice();

var state = await device.IsRuntimeEnabled(CancellationToken);

if (state == true)
{
Logger?.LogInformation($"{Strings.DisablingRuntime}...");
await device.RuntimeDisable(CancellationToken);
}

Logger?.LogInformation($"Looking for files...");

var folder = AppTools.SanitizeMeadowFolderName("\\");

var fileList = await connection.GetFileList($"{folder}", false, CancellationToken);

if (fileList == null || fileList.Length == 0)
{
Logger?.LogError($"File delete failed, no files found");
return;
}

foreach (var file in fileList)
{
await DeleteFileRecursive(device, folder, file, CancellationToken);
}
}

private async Task DeleteFileRecursive(IMeadowDevice device, string directoryname, MeadowFileInfo fileInfo, CancellationToken cancellationToken)
{
var meadowFile = AppTools.SanitizeMeadowFilename(Path.Combine(directoryname, fileInfo.Name));

foreach (var folder in AppManager.PersistantFolders)
{
if (meadowFile.StartsWith($"/{AppManager.MeadowRootFolder}/{folder}"))
{
return;
}
}

if (fileInfo.IsDirectory)
{
// Add a backslash as we're a directory and not a file
meadowFile += "/";
var subfolderFiles = await device.GetFileList(meadowFile, false, cancellationToken);

foreach (var subfolderFile in subfolderFiles!)
{
await DeleteFileRecursive(device, meadowFile, subfolderFile, cancellationToken);
}
return;
}

Logger?.LogInformation($"Deleting file '{meadowFile}' from device...");

await device.DeleteFile(meadowFile, cancellationToken);
await Task.Delay(100);
}
}
39 changes: 11 additions & 28 deletions Source/v2/Meadow.Cli/Commands/Current/File/FileDeleteCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,7 @@ protected override async ValueTask ExecuteCommand()
await device.RuntimeDisable(CancellationToken);
}

if (MeadowFile == "all")
{
Logger?.LogInformation($"Looking for files...");
}
else
{
Logger?.LogInformation($"Looking for file {MeadowFile}...");
}
Logger?.LogInformation($"Looking for file {MeadowFile}...");

var folder = AppTools.SanitizeMeadowFolderName(Path.GetDirectoryName(MeadowFile)!);

Expand All @@ -46,30 +39,20 @@ protected override async ValueTask ExecuteCommand()
return;
}

if (MeadowFile == "all")
var requested = Path.GetFileName(MeadowFile);

var exists = fileList?.Any(f => Path.GetFileName(f.Name) == requested) ?? false;

var file = AppTools.SanitizeMeadowFilename(MeadowFile);

if (!exists)
{
foreach (var file in fileList)
{
await DeleteFileRecursive(device, folder, file, CancellationToken);
}
Logger?.LogError($"File '{file}' not found on device");
}
else
{
var requested = Path.GetFileName(MeadowFile);

var exists = fileList?.Any(f => Path.GetFileName(f.Name) == requested) ?? false;

var file = AppTools.SanitizeMeadowFilename(MeadowFile);

if (!exists)
{
Logger?.LogError($"File '{file}' not found on device");
}
else
{
Logger?.LogInformation($"Deleting '{file}'");
await device.DeleteFile(file, CancellationToken);
}
Logger?.LogInformation($"Deleting '{file}'");
await device.DeleteFile(file, CancellationToken);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Cli/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
},
"File Delete All": {
"commandName": "Project",
"commandLineArgs": "file delete all"
"commandLineArgs": "file delete-all"
},
"File initial": {
"commandName": "Project",
Expand Down

0 comments on commit 41f42bb

Please sign in to comment.