modifiche x riordino chiavi in webCOnfigSetter

This commit is contained in:
Samuele Locatelli
2024-03-01 17:15:55 +01:00
parent 4c2fc5c6f6
commit c4dd04a221
6 changed files with 159 additions and 20 deletions
+85 -18
View File
@@ -2,6 +2,7 @@
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Xml;
@@ -22,9 +23,10 @@ namespace WebConfigSetter
#region Protected Properties
protected static string baseDir { get; set; } = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);//@"c:\Steamware\WebConfigSetter";
//@"c:\Steamware\WebConfigSetter";
protected static string baseDir { get; set; } = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
#if DEBUG
protected static string baseWebAppDir { get; set; } = @"\\iis02\c$\inetpub\wwwroot\MP\";
protected static string baseWebAppDir { get; set; } = @"\\iis01\c$\inetpub\wwwroot\MP\";
#else
protected static string baseWebAppDir { get; set; } = @"c:\inetpub\wwwroot\MP\";
@@ -34,7 +36,7 @@ namespace WebConfigSetter
protected static string currentSetFile { get; set; } = $@"{confDir}\default.json";
/// <summary>
/// modalità scrittura (altrimenti legge e carica conf parsando i files)
/// Modalità operativa: lettura/scrittura/riorg file conf
/// </summary>
protected static operationMode opMode { get; set; } = operationMode.readConf;
@@ -56,7 +58,7 @@ namespace WebConfigSetter
Console.WriteLine(separatore);
Console.WriteLine("");
Console.WriteLine("Sintassi: WebConfigSetter.exe $1 $2 $3");
Console.WriteLine("$1: Modalità operativa (0=read, 1=write)");
Console.WriteLine("$1: Modalità operativa (0=read [default], 1=write, 2=riorganizzazione)");
Console.WriteLine("$2: WebApp directory di base");
Console.WriteLine("$3: SetName da applicare");
Console.WriteLine(separatore);
@@ -67,16 +69,13 @@ namespace WebConfigSetter
// carico parametri opzionali
if (args.Length > 0)
{
opMode = args[0] == "1" ? operationMode.writeConf : operationMode.readConf;
setOpMode(args[0]);
}
else
{
Console.WriteLine($"Modalità operativa 0=read, 1=write [{opMode}]");
Console.WriteLine($"Modalità operativa 0=read [default], 1=write, 2=riorganizzazione [{opMode}]");
userInput = Console.ReadLine();
if (!string.IsNullOrEmpty(userInput))
{
opMode = userInput == "1" ? operationMode.writeConf : operationMode.readConf;
}
setOpMode(userInput);
}
if (args.Length > 1)
{
@@ -119,20 +118,48 @@ namespace WebConfigSetter
Console.WriteLine(separatore);
}
/// <summary>
/// impostazione modalità operativa da parametro
/// </summary>
/// <param name="currOpt"></param>
private static void setOpMode(string currOpt)
{
switch (currOpt)
{
case "0":
opMode = operationMode.readConf;
break;
case "1":
opMode = operationMode.writeConf;
break;
case "2":
opMode = operationMode.reorgConf;
break;
default:
opMode = operationMode.readConf;
break;
}
}
/// <summary>
/// Esecuzione richiesta principale
/// </summary>
private static void processRequest()
{
// caso 1: acquisisco conf
if (opMode == operationMode.readConf)
switch (opMode)
{
readConfToFiles();
}
// caso 2: effettuo sostituzioni
else
{
writeConfToFiles();
case operationMode.readConf:
readConfToFiles();
break;
case operationMode.writeConf:
writeConfToFiles();
break;
case operationMode.reorgConf:
reorgConfFile();
break;
default:
readConfToFiles();
break;
}
}
@@ -234,6 +261,11 @@ namespace WebConfigSetter
}
}
// STEP 3: riordino blocchi configurazione
currConfig.appSettRules = currConfig.appSettRules.OrderBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value);
currConfig.connStrRules = currConfig.connStrRules.OrderBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value);
currConfig.jsonConnStrRules = currConfig.jsonConnStrRules.OrderBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value);
// serializzo!
string rawData = JsonConvert.SerializeObject(currConfig, Newtonsoft.Json.Formatting.Indented);
@@ -365,6 +397,41 @@ namespace WebConfigSetter
}
}
private static void reorgConfFile()
{
// check file exists...
if (File.Exists(currentSetFile))
{
// leggo il file delle configurazioni da applicare...
string rawData = File.ReadAllText(currentSetFile);
if (!string.IsNullOrEmpty(rawData))
{
// leggo conf
currConfig = JsonConvert.DeserializeObject<SetCliente>(rawData);
// riordino blocchi configurazione
currConfig.appSettRules = currConfig.appSettRules.OrderBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value);
currConfig.connStrRules = currConfig.connStrRules.OrderBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value);
currConfig.jsonConnStrRules = currConfig.jsonConnStrRules.OrderBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value);
// salvo: serializzo!
rawData = JsonConvert.SerializeObject(currConfig, Newtonsoft.Json.Formatting.Indented);
// salvo il file di conf!
File.WriteAllText(currentSetFile, rawData);
}
else
{
Console.WriteLine($"Errore: file conf vuoto: {currentSetFile}");
}
}
else
{
Console.WriteLine($"Errore: file conf non trovato: {currentSetFile}");
}
}
#endregion Private Methods
}
}