Removed Renci.SshNet

This commit is contained in:
Mike Phares 2025-02-24 21:25:49 -07:00
parent d9e394f446
commit 55adcb69aa
3 changed files with 94 additions and 139 deletions

View File

@ -1,7 +1,5 @@
using DiscUtils.Iso9660;
using Microsoft.Extensions.Logging;
using Renci.SshNet;
using Renci.SshNet.Sftp;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO.Compression;
@ -15,7 +13,7 @@ internal static partial class Helper20241217
private record Record(string Directory, Job? Job, string Path);
private record Job(string AlternatePath, string Directory, string Extension, File[] Files, int FilesCount, double FilesTotalLength, int Keep, Target[] Targets);
private record SecureShell(string Host, string Key, string Path, bool Required, string User);
private record SecureShell();
private record ServerMessageBlock(string Path, bool Required);
private record Target(SecureShell? SecureShell, ServerMessageBlock? ServerMessageBlock);
private record File(long LastWriteTicks, long Length, string RelativePath);
@ -138,17 +136,25 @@ internal static partial class Helper20241217
return result;
}
private static void WriteISO(Record record, ReadOnlyCollection<File> files, string path, string directoryName)
private static void WriteISO(char destinationDriveLetter, Record record, ReadOnlyCollection<File> files, string path, string directoryName)
{
string checkFile = $"{destinationDriveLetter}{path[1..]}";
string checkDirectory = Path.GetDirectoryName(checkFile) ?? throw new Exception();
if (!Directory.Exists(checkDirectory))
_ = Directory.CreateDirectory(checkDirectory);
CDBuilder builder = new() { UseJoliet = true, VolumeIdentifier = directoryName.Length < 25 ? directoryName : directoryName[..25] };
foreach (File file in files)
_ = builder.AddFile(file.RelativePath, Path.Combine(record.Directory, file.RelativePath));
builder.Build(path);
builder.Build(checkFile);
}
private static void WriteZIP(Record record, ReadOnlyCollection<File> files, string path)
private static void WriteZIP(char destinationDriveLetter, Record record, ReadOnlyCollection<File> files, string path)
{
using ZipArchive zip = ZipFile.Open(path, ZipArchiveMode.Create);
string checkFile = $"{destinationDriveLetter}{path[1..]}";
string checkDirectory = Path.GetDirectoryName(checkFile) ?? throw new Exception();
if (!Directory.Exists(checkDirectory))
_ = Directory.CreateDirectory(checkDirectory);
using ZipArchive zip = ZipFile.Open(checkFile, ZipArchiveMode.Create);
string directoryEntry;
List<string> directoryEntries = [];
foreach (File file in files)
@ -163,39 +169,17 @@ internal static partial class Helper20241217
_ = zip.CreateEntryFromFile(Path.Combine(record.Directory, file.RelativePath), file.RelativePath);
}
private static void WriteExtension(Record record, Job job, ReadOnlyCollection<File> files, string path)
private static void WriteExtension(char destinationDriveLetter, Record record, Job job, ReadOnlyCollection<File> files, string path)
{
string directoryName = Path.GetFileName(record.Directory);
if (job.Extension.Equals(".iso", StringComparison.OrdinalIgnoreCase))
WriteISO(record, files, path, directoryName);
WriteISO(destinationDriveLetter, record, files, path, directoryName);
else if (job.Extension.Equals(".zip", StringComparison.OrdinalIgnoreCase))
WriteZIP(record, files, path);
WriteZIP(destinationDriveLetter, record, files, path);
else
throw new NotImplementedException();
}
private static void PushTo(Job job, SecureShell secureShell, string path)
{
string remotePath = string.Concat(secureShell.Path, '/', Path.GetFileName(path));
using SftpClient client = new(secureShell.Host, secureShell.User, new PrivateKeyFile(secureShell.Key));
client.Connect();
if (job.Files.Length == 0)
{
string directoryName = Path.GetDirectoryName(secureShell.Path) ?? throw new Exception();
try
{ client.CreateDirectory(Path.GetDirectoryName(directoryName) ?? throw new Exception()); }
catch (Exception) { }
try
{ client.CreateDirectory(directoryName); }
catch (Exception) { }
try
{ client.CreateDirectory(secureShell.Path); }
catch (Exception) { }
}
using FileStream fileStream = System.IO.File.OpenRead(path);
client.UploadFile(fileStream, remotePath);
}
private static void PushTo(ServerMessageBlock serverMessageBlock, string path)
{
string remotePath = Path.Combine(serverMessageBlock.Path, Path.GetFileName(path));
@ -214,15 +198,7 @@ internal static partial class Helper20241217
foreach (Target target in job.Targets)
{
if (target.SecureShell is not null)
{
try
{ PushTo(job, target.SecureShell, path); }
catch (Exception ex)
{
if (target.SecureShell.Required)
results.Add(ex);
}
}
continue;
else if (target.ServerMessageBlock is not null)
{
try
@ -239,23 +215,6 @@ internal static partial class Helper20241217
return results.AsReadOnly();
}
private static void DeleteOld(Job job, SecureShell secureShell, string path)
{
List<string> results = [];
using SftpClient client = new(secureShell.Host, secureShell.User, new PrivateKeyFile(secureShell.Key));
client.Connect();
foreach (ISftpFile file in client.ListDirectory(secureShell.Path))
{
if (file.Name == path)
continue;
if (!file.Name.EndsWith(job.Extension, StringComparison.OrdinalIgnoreCase))
continue;
results.Add(file.FullName);
}
for (int i = job.Keep - 1; i < results.Count; i++)
client.DeleteFile(results[i]);
}
private static void DeleteOld(Job job, ServerMessageBlock serverMessageBlock, string path)
{
List<string> results = [];
@ -276,15 +235,7 @@ internal static partial class Helper20241217
foreach (Target target in job.Targets)
{
if (target.SecureShell is not null)
{
try
{ DeleteOld(job, target.SecureShell, path); }
catch (Exception ex)
{
if (target.SecureShell.Required)
results.Add(ex);
}
}
continue;
else if (target.ServerMessageBlock is not null)
{
try
@ -304,7 +255,7 @@ internal static partial class Helper20241217
private static void Verify(string searchPattern, string[] ignoreFileNames)
{
List<Target> targets = [
new(new SecureShell("free.file.sync.root", "C:/Users/phares/.ssh/id_ed25519", "\\home", true, "root"), null),
new(new SecureShell(), null),
new(null, new ServerMessageBlock("\\\\mesfs.infineon.com\\EC_APC\\DEV", true))
];
string directory = Path.Combine(Environment.CurrentDirectory, ".vscode", "helper");
@ -337,6 +288,7 @@ internal static partial class Helper20241217
ReadOnlyCollection<Exception> exceptions;
string[] ignoreFileNames = args[3].Split('|');
string sourceDirectory = Path.GetFullPath(args[0]);
char destinationDriveLetter = args[4].Split(':')[0][0];
logger.LogInformation("Searching <{sourceDirectory}> with search pattern {searchPattern}", args[0], searchPattern);
if (Debugger.IsAttached)
Verify(searchPattern, ignoreFileNames);
@ -353,7 +305,7 @@ internal static partial class Helper20241217
directoryName = Path.GetFileName(record.Directory);
path = Path.Combine(record.Directory, $"{directoryName}-{DateTime.Now:yyyy-MM-dd-HH-mm-ss-fff}{record.Job.Extension}");
logger.LogInformation("Writing <{directory}> extension", record.Directory);
WriteExtension(record, record.Job, files, path);
WriteExtension(destinationDriveLetter, record, record.Job, files, path);
logger.LogInformation("Pushing <{directory}> extension", record.Directory);
exceptions = PushTo(record.Job, path);
if (exceptions.Count != 0)

View File

@ -110,49 +110,51 @@ internal static partial class Helper20250204
{
}
private static string[] GetTaskLines(string directory, string rootDirectory) =>
[
"{",
"\"version\": \"2.0.0\",",
"\"tasks\": [",
"{",
"\"label\": \"File-Folder-Helper AOT s X Day-Helper-2025-02-04\",",
"\"type\": \"shell\",",
"\"command\": \"L:/DevOps/Mesa_FI/File-Folder-Helper/bin/Release/net8.0/win-x64/publish/File-Folder-Helper.exe\",",
"\"args\": [",
"\"s\",",
"\"X\",",
$"\"{directory}\",",
"\"Day-Helper-2025-02-04\",",
$"\"{rootDirectory}\",",
"],",
"\"problemMatcher\": []",
"},",
"{",
"\"label\": \"File-Folder-Helper AOT s X Day-Helper-2024-06-23\",",
"\"type\": \"shell\",",
"\"command\": \"L:/DevOps/Mesa_FI/File-Folder-Helper/bin/Release/net8.0/win-x64/publish/File-Folder-Helper.exe\",",
"\"args\": [",
"\"s\",",
"\"X\",",
$"\"{directory}\",",
"\"Day-Helper-2024-06-23\",",
"\"*.md\",",
"\"##_Sub-tasks\",",
"\"-_[code-insiders](\",",
"\"index.md\",",
"\"-_[,](\",",
"\"##_Done\",",
"\".kan\",",
$"\"{rootDirectory}\",",
"\"316940400000\",",
"],",
"\"problemMatcher\": []",
"}",
"]",
"}",
];
private static string GetTaskText(string directory, string rootDirectory) =>
string.Join(Environment.NewLine,
[
"{",
"\"version\": \"2.0.0\",",
"\"tasks\": [",
"{",
"\"label\": \"File-Folder-Helper AOT s X Day-Helper-2025-02-04\",",
"\"type\": \"shell\",",
"\"command\": \"L:/DevOps/Mesa_FI/File-Folder-Helper/bin/Release/net8.0/win-x64/publish/File-Folder-Helper.exe\",",
"\"args\": [",
"\"s\",",
"\"X\",",
$"\"{directory}\",",
"\"Day-Helper-2025-02-04\",",
$"\"{rootDirectory}\",",
"],",
"\"problemMatcher\": []",
"},",
"{",
"\"label\": \"File-Folder-Helper AOT s X Day-Helper-2024-06-23\",",
"\"type\": \"shell\",",
"\"command\": \"L:/DevOps/Mesa_FI/File-Folder-Helper/bin/Release/net8.0/win-x64/publish/File-Folder-Helper.exe\",",
"\"args\": [",
"\"s\",",
"\"X\",",
$"\"{directory}\",",
"\"Day-Helper-2024-06-23\",",
"\"*.md\",",
"\"##_Sub-tasks\",",
"\"-_[code-insiders](\",",
"\"index.md\",",
"\"-_[,](\",",
"\"##_Done\",",
"\".kan\",",
$"\"{rootDirectory}\",",
"\"316940400000\",",
"],",
"\"problemMatcher\": []",
"}",
"]",
"}",
]);
string.Join(Environment.NewLine, GetTaskLines(directory, rootDirectory));
private static void WriteTaskFile(string sourceDirectory, string rootDirectory)
{
@ -169,34 +171,36 @@ internal static partial class Helper20250204
private static string GetFilter(ReadOnlyCollection<H1ParamCaseAndState> collection, string filter) =>
string.Join(Environment.NewLine, from l in collection where l.State == filter select $"- [{l.ParamCase}](tasks/{l.ParamCase}.md)");
private static string[] GetIndexLines(WorkItem workItem, H1ParamCaseAndState h1ParamCaseAndState, ReadOnlyCollection<H1ParamCaseAndState> collection) =>
[
"---",
"startedColumns:",
" - 'In Progress'",
"completedColumns:",
" - Done",
"---",
string.Empty,
$"# {workItem.Id} - {h1ParamCaseAndState.H1}",
string.Empty,
"## Backlog",
string.Empty,
GetFilter(collection, "Backlog"),
string.Empty,
"## Todo",
string.Empty,
GetFilter(collection, "ToDo"),
string.Empty,
"## In Progress",
string.Empty,
GetFilter(collection, "In Progress"),
string.Empty,
"## Done",
string.Empty,
GetFilter(collection, "Done"),
string.Empty
];
private static string GetIndexText(WorkItem workItem, H1ParamCaseAndState h1ParamCaseAndState, ReadOnlyCollection<H1ParamCaseAndState> collection) =>
string.Join(Environment.NewLine, [
"---",
"startedColumns:",
" - 'In Progress'",
"completedColumns:",
" - Done",
"---",
string.Empty,
$"# {workItem.Id} - {h1ParamCaseAndState.H1}",
string.Empty,
"## Backlog",
string.Empty,
GetFilter(collection, "Backlog"),
string.Empty,
"## Todo",
string.Empty,
GetFilter(collection, "ToDo"),
string.Empty,
"## In Progress",
string.Empty,
GetFilter(collection, "In Progress"),
string.Empty,
"## Done",
string.Empty,
GetFilter(collection, "Done"),
string.Empty
]);
string.Join(Environment.NewLine, GetIndexLines(workItem, h1ParamCaseAndState, collection));
private static string GetIndexMarkdown(FileInfo fileInfo, ReadOnlyCollection<WorkItem> workItems)
{

View File

@ -18,7 +18,6 @@
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.1" />
<PackageReference Include="runtime.win-x64.Microsoft.DotNet.ILCompiler" Version="8.0.12" />
<PackageReference Include="SSH.NET" Version="2024.2.0" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
<PackageReference Include="TextCopy" Version="6.2.1" />
<PackageReference Include="WindowsShortcutFactory" Version="1.2.0" />