Skip to content

Commit

Permalink
Merge pull request #613 from WildernessLabs/cli-2.0.65
Browse files Browse the repository at this point in the history
Cli 2.0.65
  • Loading branch information
ctacke authored Jan 10, 2025
2 parents 5f9dde0 + e23d264 commit 382bd86
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 34 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
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Cli/Meadow.CLI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Authors>Wilderness Labs, Inc</Authors>
<Company>Wilderness Labs, Inc</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>2.0.64.0</PackageVersion>
<PackageVersion>2.0.65.0</PackageVersion>
<Platforms>AnyCPU</Platforms>
<PackageProjectUrl>http://developer.wildernesslabs.co/Meadow/Meadow.CLI/</PackageProjectUrl>
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.CLI</RepositoryUrl>
Expand Down
2 changes: 1 addition & 1 deletion Source/v2/Meadow.Cli/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
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
6 changes: 3 additions & 3 deletions Source/v2/Meadow.HCom/DeviceInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"];

/// <summary>
/// String representation of an unknown MAC address.
Expand Down Expand Up @@ -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}");
}
Expand Down

0 comments on commit 382bd86

Please sign in to comment.