diff --git a/Source/v2/Meadow.CLI/Commands/Current/File/FileDeleteAllCommand.cs b/Source/v2/Meadow.CLI/Commands/Current/File/FileDeleteAllCommand.cs new file mode 100644 index 00000000..e1dee353 --- /dev/null +++ b/Source/v2/Meadow.CLI/Commands/Current/File/FileDeleteAllCommand.cs @@ -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 +{ + 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); + } +} \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Commands/Current/File/FileDeleteCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/File/FileDeleteCommand.cs index bf3da405..1da2c383 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/File/FileDeleteCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/File/FileDeleteCommand.cs @@ -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)!); @@ -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); } } diff --git a/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs b/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs index a298c6dc..a1490a2a 100644 --- a/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs +++ b/Source/v2/Meadow.Cli/Commands/Current/Firmware/FirmwareWriteCommand.cs @@ -393,6 +393,14 @@ private async Task WriteEspFiles(IMeadowConnection? connection, DeviceInfo? devi }; } + foreach (var file in fileList) + { + if (!File.Exists(file)) + { + throw new CommandException(string.Format(Strings.InvalidFirmwareForSpecifiedPath, file), CommandExitCode.FileNotFound); + } + } + deviceInfo ??= await connection.GetDeviceInfo(CancellationToken); if (deviceInfo == null) { throw new CommandException(Strings.UnableToGetDeviceInfo); } diff --git a/Source/v2/Meadow.Cli/Meadow.CLI.csproj b/Source/v2/Meadow.Cli/Meadow.CLI.csproj index 28711131..e6c07de1 100644 --- a/Source/v2/Meadow.Cli/Meadow.CLI.csproj +++ b/Source/v2/Meadow.Cli/Meadow.CLI.csproj @@ -10,7 +10,7 @@ Wilderness Labs, Inc Wilderness Labs, Inc true - 2.0.64.0 + 2.0.65.0 AnyCPU http://developer.wildernesslabs.co/Meadow/Meadow.CLI/ https://github.com/WildernessLabs/Meadow.CLI diff --git a/Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs b/Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs index 699528fa..b69bee20 100644 --- a/Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs +++ b/Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs @@ -6,5 +6,5 @@ namespace Meadow.CLI; public static class Constants { - public const string CLI_VERSION = "2.0.64.0"; + public const string CLI_VERSION = "2.0.65.0"; } \ No newline at end of file diff --git a/Source/v2/Meadow.Cli/Properties/launchSettings.json b/Source/v2/Meadow.Cli/Properties/launchSettings.json index 66f46684..c6ff980d 100644 --- a/Source/v2/Meadow.Cli/Properties/launchSettings.json +++ b/Source/v2/Meadow.Cli/Properties/launchSettings.json @@ -101,7 +101,7 @@ }, "File Delete All": { "commandName": "Project", - "commandLineArgs": "file delete all" + "commandLineArgs": "file delete-all" }, "File initial": { "commandName": "Project", diff --git a/Source/v2/Meadow.HCom/DeviceInfo.cs b/Source/v2/Meadow.HCom/DeviceInfo.cs index 7c0ceed1..41a62bdb 100644 --- a/Source/v2/Meadow.HCom/DeviceInfo.cs +++ b/Source/v2/Meadow.HCom/DeviceInfo.cs @@ -30,7 +30,7 @@ public string? this[string propname] public string? SerialNumber => this["SerialNo"]; public string? MacAddress => this["WiFiMAC"]; public string? SoftAPMacAddress => this["SoftAPMac"]; - public string?BluetoothMacAddress => this["BtMac"]; + public string? BluetoothMacAddress => this["BtMac"]; /// /// String representation of an unknown MAC address. @@ -82,11 +82,11 @@ public override string ToString() { if (macCount > 1) { - deviceInfo.AppendLine(" MAC Addresses - "); + deviceInfo.AppendLine(" MAC Addresses"); } else { - deviceInfo.AppendLine(" MAC Address - "); + deviceInfo.AppendLine(" MAC Address"); } deviceInfo.Append($"{macAddresses}"); }