Skip to content

Commit

Permalink
Merge pull request #484 from WildernessLabs/delete_all
Browse files Browse the repository at this point in the history
Fix delete all and file list
  • Loading branch information
adrianstevens authored Feb 18, 2024
2 parents e441792 + 4d8f1d8 commit 367cbde
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 21 deletions.
31 changes: 25 additions & 6 deletions Source/v2/Meadow.Cli/Commands/Current/File/FileDeleteCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CliFx.Attributes;
using Meadow.Hcom;
using Microsoft.Extensions.Logging;

namespace Meadow.CLI.Commands.DeviceManagement;
Expand All @@ -9,6 +10,8 @@ public class FileDeleteCommand : BaseDeviceCommand<FileDeleteCommand>
[CommandParameter(0, Name = "MeadowFile", IsRequired = true)]
public string MeadowFile { get; init; } = default!;

private const string MeadowRootFolder = "meadow0";

public FileDeleteCommand(MeadowConnectionManager connectionManager, ILoggerFactory loggerFactory)
: base(connectionManager, loggerFactory)
{ }
Expand All @@ -26,10 +29,10 @@ protected override async ValueTask ExecuteCommand()
var folder = Path.GetDirectoryName(MeadowFile)!.Replace(Path.DirectorySeparatorChar, '/');
if (string.IsNullOrWhiteSpace(folder))
{
folder = "/meadow0";
folder = MeadowRootFolder;
}

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

if (fileList == null || fileList.Length == 0)
{
Expand All @@ -39,11 +42,9 @@ protected override async ValueTask ExecuteCommand()

if (MeadowFile == "all")
{
foreach (var f in fileList)
foreach (var file in fileList)
{
var p = Path.GetFileName(f.Name);
Logger?.LogInformation($"Deleting file '{p}' from device...");
await connection.Device.DeleteFile(p, CancellationToken);
await DeleteFileRecursive(connection.Device, file, CancellationToken);
}
}
else
Expand Down Expand Up @@ -71,4 +72,22 @@ protected override async ValueTask ExecuteCommand()
}
}
}

private async Task DeleteFileRecursive(IMeadowDevice device, MeadowFileInfo fileInfo, CancellationToken cancellationToken)
{
if (fileInfo.IsDirectory)
{
var subfolderFiles = await device.GetFileList(fileInfo.Name, false, cancellationToken);

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

var fileName = Path.GetFileName(fileInfo.Name);
Logger?.LogInformation($"Deleting file '{fileName}' from device...");
await device.DeleteFile(fileName, cancellationToken);
}
}
32 changes: 20 additions & 12 deletions Source/v2/Meadow.Cli/Commands/Current/File/FileListCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using CliFx.Attributes;
using Microsoft.Extensions.Logging;
using System.Linq;

namespace Meadow.CLI.Commands.DeviceManagement;

Expand All @@ -10,6 +9,7 @@ public class FileListCommand : BaseDeviceCommand<FileListCommand>
public const int FileSystemBlockSize = 4096;

private const string MeadowRootFolder = "meadow0";
private const string FolderLabel = "[folder]";

[CommandOption("verbose", 'v', IsRequired = false)]
public bool Verbose { get; init; }
Expand Down Expand Up @@ -38,11 +38,11 @@ protected override async ValueTask ExecuteCommand()
}
if (Folder.StartsWith('/') == false)
{
Folder += "/";
Folder = $"/{Folder}";
}
if (Folder.Contains(MeadowRootFolder) == false)
{
Folder += $"/{MeadowRootFolder}";
Folder = $"/{MeadowRootFolder}{Folder}";
}

Logger?.LogInformation($"Getting file list from '{Folder}'...");
Expand Down Expand Up @@ -81,19 +81,27 @@ protected override async ValueTask ExecuteCommand()
totalBlocksUsed += ((file.Size ?? 0) / FileSystemBlockSize) + 1;

var line = $"{file.Name.PadRight(longestFileName)}";
line = $"{line}\t{file.Crc:x8}";

if (file.Size > 1000000)
if(file.IsDirectory)
{
line = $"{line}\t{file.Size / 1000000d,7:0.0} MB ";
}
else if (file.Size > 1000)
{
line = $"{line}\t{file.Size / 1000,7:0} kB ";
line = $"{line}\t{FolderLabel}";
}
else
{
line = $"{line}\t{file.Size,7} bytes";
line = $"{line}\t{file.Crc:x8}";

if (file.Size > 1000000)
{
line = $"{line}\t{file.Size / 1000000d,7:0.0} MB ";
}
else if (file.Size > 1000)
{
line = $"{line}\t{file.Size / 1000,7:0} kB ";
}
else
{
line = $"{line}\t{file.Size,7} bytes";
}
}

Logger?.LogInformation(line);
Expand All @@ -110,7 +118,7 @@ protected override async ValueTask ExecuteCommand()
{
foreach (var file in files)
{
Logger?.LogInformation(file.Name + (file.IsDirectory?" [folder]":string.Empty));
Logger?.LogInformation(file.Name + (file.IsDirectory?FolderLabel:string.Empty));
}

Logger?.LogInformation($"\t{files.Length} file(s)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,14 @@ protected override async ValueTask ExecuteCommand()
|| Path.GetFileName(IndividualFile) == F7FirmwarePackageCollection.F7FirmwareFiles.CoprocApplicationFile
|| Path.GetFileName(IndividualFile) == F7FirmwarePackageCollection.F7FirmwareFiles.CoprocBootloaderFile)
{
await connection.RuntimeDisable();
if(connection == null)
{
connection = await GetConnectionAndDisableRuntime();
}
else
{
await connection.RuntimeDisable();
}

await WriteEspFiles(connection, deviceInfo, package);
}
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.7</PackageVersion>
<PackageVersion>2.0.7.1</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,6 +6,6 @@ namespace Meadow.CLI
{
public static class Constants
{
public const string CLI_VERSION = "2.0.7.0";
public const string CLI_VERSION = "2.0.7.1";
}
}
4 changes: 4 additions & 0 deletions Source/v2/Meadow.Cli/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
"commandName": "Project",
"commandLineArgs": "file delete Juego.pdb"
},
"File Delete All": {
"commandName": "Project",
"commandLineArgs": "file delete all"
},
"File Read": {
"commandName": "Project",
"commandLineArgs": "file read test.txt \"f:\\temp\\test2.txt\""
Expand Down

0 comments on commit 367cbde

Please sign in to comment.