json to Markdown
This commit is contained in:
parent
67e7108220
commit
b2ab434ec3
17
.vscode/launch.json
vendored
17
.vscode/launch.json
vendored
@ -12,12 +12,17 @@
|
||||
"program": "${workspaceFolder}/bin/Debug/net8.0/win-x64/File-Folder-Helper.dll",
|
||||
"args": [
|
||||
"s",
|
||||
"M",
|
||||
"L:/Git/Notes-Infineon/.Infineon",
|
||||
"-d",
|
||||
"L:/Git/Notes-Infineon/.Infineon/.vscode/helper",
|
||||
"-u",
|
||||
"true"
|
||||
"X",
|
||||
"L:/Git/Notes-Network/.Network/.vscode/helper",
|
||||
"Day-Helper-2024-07-18",
|
||||
"hosts.jsonl",
|
||||
"192.168.0.",
|
||||
"Network",
|
||||
"Wired",
|
||||
".md",
|
||||
"7777",
|
||||
"8888",
|
||||
"9999"
|
||||
],
|
||||
"cwd": "${workspaceFolder}",
|
||||
"console": "integratedTerminal",
|
||||
|
101
Day/2024-Q3/Helper-2024-07-18.cs
Normal file
101
Day/2024-Q3/Helper-2024-07-18.cs
Normal file
@ -0,0 +1,101 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace File_Folder_Helper.Day;
|
||||
|
||||
internal static partial class Helper20240718
|
||||
{
|
||||
|
||||
private record Host([property: JsonPropertyName("a")] string? Id,
|
||||
[property: JsonPropertyName("b")] string? Colon,
|
||||
[property: JsonPropertyName("c")] string? Hyphen,
|
||||
[property: JsonPropertyName("d")] string? Line,
|
||||
[property: JsonPropertyName("e")] string? Count,
|
||||
[property: JsonPropertyName("f")] string? Segments,
|
||||
[property: JsonPropertyName("g")] string? Type,
|
||||
[property: JsonPropertyName("h")] string? Device,
|
||||
[property: JsonPropertyName("i")] string? Name);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true, AllowTrailingCommas = true)]
|
||||
[JsonSerializable(typeof(Host[]))]
|
||||
private partial class HostBSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private record Record(string Key, Dictionary<string, string> KeyValuePairs);
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true)]
|
||||
[JsonSerializable(typeof(Dictionary<string, Dictionary<string, string>>))]
|
||||
private partial class DictionaryDictionaryBSourceGenerationContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
|
||||
private static Host[] GetHosts(ILogger<Worker> logger, string file)
|
||||
{
|
||||
Host[] results;
|
||||
string lines = File.ReadAllText(file);
|
||||
string json = $"[{lines.Replace("\r\n", ",")}]";
|
||||
logger.LogDebug(lines);
|
||||
JsonSerializerOptions jsonSerializerOptions = new() { AllowTrailingCommas = true };
|
||||
results = JsonSerializer.Deserialize<Host[]>(json, jsonSerializerOptions) ?? throw new NullReferenceException();
|
||||
return results;
|
||||
}
|
||||
|
||||
private static ReadOnlyCollection<string> GetLines(Host[] hosts, string argsIP, string title, string wired)
|
||||
{
|
||||
List<string> results = ["flowchart LR", $" subgraph {title}"];
|
||||
int id;
|
||||
string line;
|
||||
string check;
|
||||
List<int> distinct = [];
|
||||
string newLine = $"{Environment.NewLine} ";
|
||||
foreach (Host host in hosts)
|
||||
{
|
||||
if (host.Id is null || host.Hyphen is null || host.Device is null || host.Name is null || host.Hyphen.Length != 17)
|
||||
continue;
|
||||
if (!int.TryParse(host.Id, out id))
|
||||
throw new NotSupportedException($"{host.Id} is not a number");
|
||||
if (distinct.Contains(id))
|
||||
throw new NotSupportedException($"{id} is not distinct!");
|
||||
distinct.Add(id);
|
||||
line = $"{id}(fa:{host.Type}{newLine}{host.Name}{newLine}{host.Colon}{newLine}{host.Hyphen}{newLine}{host.Device}{newLine}https://{host.Name}/)";
|
||||
results.Add(line);
|
||||
}
|
||||
results.Add(" end");
|
||||
results.Add($" subgraph {title}");
|
||||
foreach (Host host in hosts)
|
||||
{
|
||||
if (host.Id is null || host.Hyphen is null || host.Device is null || host.Name is null || host.Hyphen.Length != 17)
|
||||
continue;
|
||||
if (!int.TryParse(host.Id, out id))
|
||||
throw new NotSupportedException($"{host.Id} is not a number");
|
||||
check = host.Type == wired ? "-->" : "-.->";
|
||||
line = $"{id} {check} {argsIP}{host.Line}";
|
||||
results.Add(line);
|
||||
}
|
||||
results.Add(" end");
|
||||
return results.AsReadOnly();
|
||||
}
|
||||
|
||||
internal static void JsonToMarkdown(ILogger<Worker> logger, List<string> args)
|
||||
{
|
||||
Host[] hosts;
|
||||
string title = args[4];
|
||||
string wired = args[5];
|
||||
string argsIP = args[3];
|
||||
string extension = args[6];
|
||||
string searchPattern = args[2];
|
||||
ReadOnlyCollection<string> lines;
|
||||
string sourceDirectory = Path.GetFullPath(args[0]);
|
||||
string[] files = Directory.GetFiles(sourceDirectory, searchPattern, SearchOption.TopDirectoryOnly);
|
||||
foreach (string file in files)
|
||||
{
|
||||
hosts = GetHosts(logger, file);
|
||||
lines = GetLines(hosts, argsIP, title, wired);
|
||||
File.WriteAllText($"{file}{extension}", string.Join(Environment.NewLine, lines));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -79,6 +79,8 @@ internal static class HelperDay
|
||||
Day.Helper20240624.MoveUpOneDirectory(logger, args);
|
||||
else if (args[1] == "Day-Helper-2024-07-11")
|
||||
Day.Helper20240711.GitRemoteRemove(logger, args);
|
||||
else if (args[1] == "Day-Helper-2024-07-18")
|
||||
Day.Helper20240718.JsonToMarkdown(logger, args);
|
||||
else
|
||||
throw new Exception(appSettings.Company);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user