HelperFindReplace.UpdateTnsNames

This commit is contained in:
Mike Phares 2023-09-06 21:40:38 -07:00
parent 84cda2e57f
commit 71062e4b69
3 changed files with 170 additions and 2 deletions

65
Helpers/HelperDownload.cs Normal file
View File

@ -0,0 +1,65 @@
using Microsoft.Extensions.Logging;
using System.Net.Http.Json;
namespace File_Folder_Helper.Helpers;
internal static class HelperDownload
{
internal static void SaveJson(ILogger log, string argsZero)
{
string[] lines;
int? jsonBodyLine;
string? userAgent;
string[] segments;
HttpClient httpClient;
HttpRequestMessage httpRequestMessage;
string[] files = Directory.GetFiles(argsZero, "*.post", SearchOption.TopDirectoryOnly);
foreach (string file in files)
{
jsonBodyLine = null;
userAgent = null;
lines = File.ReadAllLines(file);
if (lines.Length < 2)
continue;
httpRequestMessage = new(HttpMethod.Post, lines[1]);
log.LogInformation("reading {fileName}", Path.GetFileName(file));
for (int i = 2; i < lines.Length; i++)
{
if (lines.Length < 1)
continue;
if (lines[i][0] == '#')
continue;
else if (lines[i][0] == '{')
jsonBodyLine = i;
else
{
segments = lines[i].Split(": ");
if (segments.Length < 2)
continue;
if (segments[0] == "User-Agent")
userAgent = segments[1];
else
httpRequestMessage.Headers.Add(segments[0], segments[1]);
}
}
if (jsonBodyLine is not null)
httpRequestMessage.Content = JsonContent.Create(lines[jsonBodyLine.Value]);
httpClient = new(new HttpClientHandler { UseCookies = false }) { BaseAddress = new Uri(lines[0]) };
if (userAgent is not null)
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(userAgent);
Task<HttpResponseMessage> httpResponseMessageTask = httpClient.SendAsync(httpRequestMessage);
httpResponseMessageTask.Wait();
if (!httpResponseMessageTask.Result.IsSuccessStatusCode)
throw new Exception(httpResponseMessageTask.Result.StatusCode.ToString());
Task<Stream> streamTask = httpResponseMessageTask.Result.Content.ReadAsStreamAsync();
streamTask.Wait();
if (!streamTask.Result.CanRead)
throw new NullReferenceException(nameof(streamTask));
using FileStream fileStream = new($"{file}.json", FileMode.CreateNew);
Task task = streamTask.Result.CopyToAsync(fileStream);
task.Wait();
}
}
}

View File

@ -0,0 +1,95 @@
using Microsoft.Extensions.Logging;
using System.Collections.ObjectModel;
namespace File_Folder_Helper.Helpers;
internal static class HelperFindReplace
{
private static string? GetTnsNamesOraFile(List<string> args)
{
string? result = null;
for (int i = 1; i < args.Count; i++)
{
if (args[i].Length == 2 && i + 1 < args.Count)
{
if (args[i][1] == 't')
result = Path.GetFullPath(args[i + 1]);
i++;
}
}
return result;
}
private static ReadOnlyCollection<(string, string)> GetFindReplace(string tnsNamesOraFile)
{
List<(string, string)> results = new();
string[] segments;
string[] lines = File.ReadAllLines(tnsNamesOraFile);
foreach (string line in lines)
{
segments = line.Split('=');
if (segments.Length < 3)
continue;
results.Add((segments[0].Trim(), line));
}
return new(results);
}
private static void FindReplace(ILogger log, string[] files, ReadOnlyCollection<(string, string)> findReplace)
{
bool check;
string[] lines;
string[] segments;
foreach (string file in files)
{
check = false;
lines = File.ReadAllLines(file);
for (int i = 0; i < lines.Length; i++)
{
segments = lines[i].Split('=');
if (segments.Length < 3)
continue;
foreach ((string find, string replace) in findReplace)
{
if (!segments[0].Contains(find))
continue;
if (lines[i] == replace)
continue;
if (!check)
check = true;
lines[i] = replace;
}
}
if (check)
{
log.LogInformation("{file}", file);
File.WriteAllLines(file, lines);
}
}
}
internal static void UpdateTnsNames(ILogger log, List<string> args)
{
string? tnsNamesOraFile = GetTnsNamesOraFile(args);
ReadOnlyCollection<(string, string)> findReplace;
if (!string.IsNullOrEmpty(tnsNamesOraFile) && File.Exists(tnsNamesOraFile))
findReplace = GetFindReplace(tnsNamesOraFile);
else
{
log.LogInformation("<{tnsNamesOraFile}> doesn't exist!", tnsNamesOraFile);
findReplace = new(Array.Empty<(string, string)>());
}
if (findReplace.Count == 0)
log.LogInformation("Count == {count}", findReplace.Count);
else
{
string[] files = Directory.GetFiles(args[0], "tnsNames.ora", SearchOption.AllDirectories);
if (files.Length == 0)
log.LogInformation("Count == {count}", findReplace.Count);
else
FindReplace(log, files, findReplace);
}
}
}

View File

@ -40,11 +40,13 @@ public class Worker : BackgroundService
ConsoleKey.L, ConsoleKey.L,
ConsoleKey.M, ConsoleKey.M,
ConsoleKey.N, ConsoleKey.N,
ConsoleKey.O,
ConsoleKey.R, ConsoleKey.R,
ConsoleKey.S, ConsoleKey.S,
ConsoleKey.T, ConsoleKey.T,
ConsoleKey.U, ConsoleKey.U,
ConsoleKey.V, ConsoleKey.V,
ConsoleKey.X,
ConsoleKey.Z, ConsoleKey.Z,
ConsoleKey.Delete ConsoleKey.Delete
}; };
@ -102,7 +104,7 @@ public class Worker : BackgroundService
_Logger.LogInformation("L) Log Merge (APC Log [0-9(8)]_*.log),"); _Logger.LogInformation("L) Log Merge (APC Log [0-9(8)]_*.log),");
_Logger.LogInformation("N) Create Note Files,"); _Logger.LogInformation("N) Create Note Files,");
_Logger.LogInformation("M) Markdown Wiki Link Verification,"); _Logger.LogInformation("M) Markdown Wiki Link Verification,");
// O _Logger.LogInformation("O) Oracle tnsNames.ora,");
// P // P
// Q // Q
_Logger.LogInformation("R) Rename to old, copy, delete old,"); _Logger.LogInformation("R) Rename to old, copy, delete old,");
@ -111,7 +113,7 @@ public class Worker : BackgroundService
_Logger.LogInformation("U) Links for Hugo,"); _Logger.LogInformation("U) Links for Hugo,");
_Logger.LogInformation("V) VSCode Hope Sort,"); _Logger.LogInformation("V) VSCode Hope Sort,");
// W // W
// X _Logger.LogInformation("X) Download,");
// Y // Y
_Logger.LogInformation("Z) Zip file(s) by date,"); _Logger.LogInformation("Z) Zip file(s) by date,");
_Logger.LogInformation("Delete) Delete empty directories,"); _Logger.LogInformation("Delete) Delete empty directories,");
@ -153,6 +155,9 @@ public class Worker : BackgroundService
Helpers.HelperKanbanMetadata.SetMetadata(_Logger, _AppSettings, _Args[0]); Helpers.HelperKanbanMetadata.SetMetadata(_Logger, _AppSettings, _Args[0]);
Helpers.HelperMarkdown.MarkdownWikiLinkVerification(_AppSettings, _Logger, _Args); Helpers.HelperMarkdown.MarkdownWikiLinkVerification(_AppSettings, _Logger, _Args);
break; break;
case ConsoleKey.O:
Helpers.HelperFindReplace.UpdateTnsNames(_Logger, _Args);
break;
case ConsoleKey.R: case ConsoleKey.R:
Helpers.HelperRenameToOldMoveDeleteOldMerge.RenameToOldMoveDeleteOld(_Logger, _Args[0]); Helpers.HelperRenameToOldMoveDeleteOldMerge.RenameToOldMoveDeleteOld(_Logger, _Args[0]);
break; break;
@ -169,6 +174,9 @@ public class Worker : BackgroundService
case ConsoleKey.V: case ConsoleKey.V:
Helpers.HelperVSCodePossibleExtension.Sort(_Logger, _Args); Helpers.HelperVSCodePossibleExtension.Sort(_Logger, _Args);
break; break;
case ConsoleKey.X:
Helpers.HelperDownload.SaveJson(_Logger, _Args[0]);
break;
case ConsoleKey.Z: case ConsoleKey.Z:
_ = Helpers.HelperZipFilesByDate.ZipFilesByDate(_Logger, _Args[0]); _ = Helpers.HelperZipFilesByDate.ZipFilesByDate(_Logger, _Args[0]);
break; break;