Fix WebConfigSetter
This commit is contained in:
+73
-54
@@ -40,6 +40,7 @@ namespace WebConfigSetter
|
||||
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
string userInput = "";
|
||||
if (args == null || args.Length == 0)
|
||||
{
|
||||
Console.WriteLine(separatore);
|
||||
@@ -51,21 +52,40 @@ namespace WebConfigSetter
|
||||
Console.WriteLine("");
|
||||
Console.WriteLine("Nessuna parametro fornito, verranno ora richiesti i parametri");
|
||||
Console.WriteLine(separatore);
|
||||
|
||||
// chiedo tutti i dati...
|
||||
string userInput = "";
|
||||
}
|
||||
// carico parametri opzionali
|
||||
if (args.Length > 0)
|
||||
{
|
||||
opMode = args[0] == "1" ? operationMode.writeConf : operationMode.readConf;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Modalità operativa 0=read, 1=write [{opMode}]");
|
||||
userInput = Console.ReadLine();
|
||||
if (!string.IsNullOrEmpty(userInput))
|
||||
{
|
||||
opMode = userInput == "1" ? operationMode.writeConf : operationMode.readConf;
|
||||
}
|
||||
}
|
||||
if (args.Length > 1)
|
||||
{
|
||||
baseWebAppDir = args[1];
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"WebApp directory di base [{baseWebAppDir}]");
|
||||
userInput = Console.ReadLine();
|
||||
if (!string.IsNullOrEmpty(userInput))
|
||||
{
|
||||
baseWebAppDir = userInput;
|
||||
}
|
||||
}
|
||||
if (args.Length > 2)
|
||||
{
|
||||
setName = args[2];
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"SetName da applicare [{setName}]");
|
||||
userInput = Console.ReadLine();
|
||||
if (!string.IsNullOrEmpty(userInput))
|
||||
@@ -73,23 +93,6 @@ namespace WebConfigSetter
|
||||
setName = userInput;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// carico parametri opzionali
|
||||
|
||||
if (args.Length > 0)
|
||||
{
|
||||
baseWebAppDir = args[0];
|
||||
}
|
||||
if (args.Length > 1)
|
||||
{
|
||||
baseWebAppDir = args[1];
|
||||
}
|
||||
if (args.Length > 2)
|
||||
{
|
||||
setName = args[2];
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine(separatore);
|
||||
Console.WriteLine($"Modalità operativa: {opMode}");
|
||||
@@ -171,49 +174,65 @@ namespace WebConfigSetter
|
||||
|
||||
private static void writeConfToFiles()
|
||||
{
|
||||
// leggo il file delle configurazioni da applicare...
|
||||
string rawData = File.ReadAllText(currentSetFile);
|
||||
currConfig = JsonConvert.DeserializeObject<SetCliente>(rawData);
|
||||
|
||||
// cerca i file we.config dalla folder richiesta in giù...
|
||||
string[] configFiles = Directory.GetFiles(baseWebAppDir, "web.config", SearchOption.AllDirectories);
|
||||
XmlDocument doc = new XmlDocument();
|
||||
|
||||
foreach (var item in configFiles)
|
||||
// check file exists...
|
||||
if (File.Exists(currentSetFile))
|
||||
{
|
||||
Console.WriteLine($"Found: {item}");
|
||||
|
||||
doc = new XmlDocument();
|
||||
doc.Load(item);
|
||||
|
||||
// cerco in appConfig
|
||||
XmlNodeList appSettingsList = doc.SelectNodes("configuration/appSettings/*");
|
||||
|
||||
for (int i = 0; i < appSettingsList.Count; i++)
|
||||
// leggo il file delle configurazioni da applicare...
|
||||
string rawData = File.ReadAllText(currentSetFile);
|
||||
if (!string.IsNullOrEmpty(rawData))
|
||||
{
|
||||
if (appSettingsList[i].Attributes["key"].Value.Contains("ConnectionString"))
|
||||
currConfig = JsonConvert.DeserializeObject<SetCliente>(rawData);
|
||||
|
||||
// cerca i file we.config dalla folder richiesta in giù...
|
||||
string[] configFiles = Directory.GetFiles(baseWebAppDir, "web.config", SearchOption.AllDirectories);
|
||||
XmlDocument doc = new XmlDocument();
|
||||
|
||||
foreach (var item in configFiles)
|
||||
{
|
||||
if (currConfig.appSettRules.ContainsKey(appSettingsList[i].Attributes["key"].Value))
|
||||
Console.Write($"Found: {item} --> ");
|
||||
|
||||
doc = new XmlDocument();
|
||||
doc.Load(item);
|
||||
|
||||
// cerco in appConfig
|
||||
XmlNodeList appSettingsList = doc.SelectNodes("configuration/appSettings/*");
|
||||
|
||||
for (int i = 0; i < appSettingsList.Count; i++)
|
||||
{
|
||||
// effettuo sostituzione
|
||||
appSettingsList[i].Attributes["value"].Value = currConfig.appSettRules[appSettingsList[i].Attributes["key"].Value];
|
||||
if (appSettingsList[i].Attributes["key"].Value.Contains("ConnectionString"))
|
||||
{
|
||||
if (currConfig.appSettRules.ContainsKey(appSettingsList[i].Attributes["key"].Value))
|
||||
{
|
||||
// effettuo sostituzione
|
||||
appSettingsList[i].Attributes["value"].Value = currConfig.appSettRules[appSettingsList[i].Attributes["key"].Value];
|
||||
}
|
||||
}
|
||||
}
|
||||
// cerco in connectionStrings
|
||||
XmlNodeList connectionStringsList = doc.SelectNodes("configuration/connectionStrings/*");
|
||||
|
||||
for (int i = 0; i < connectionStringsList.Count; i++)
|
||||
{
|
||||
if (currConfig.connStrRules.ContainsKey(connectionStringsList[i].Attributes["name"].Value))
|
||||
{
|
||||
// effettuo sostituzione
|
||||
connectionStringsList[i].Attributes["connectionString"].Value = currConfig.connStrRules[connectionStringsList[i].Attributes["name"].Value];
|
||||
}
|
||||
}
|
||||
|
||||
// ora lo SALVO (il documento...)
|
||||
doc.Save(item);
|
||||
Console.WriteLine(" values updated!");
|
||||
}
|
||||
}
|
||||
// cerco in connectionStrings
|
||||
XmlNodeList connectionStringsList = doc.SelectNodes("configuration/connectionStrings/*");
|
||||
|
||||
for (int i = 0; i < connectionStringsList.Count; i++)
|
||||
else
|
||||
{
|
||||
if (currConfig.connStrRules.ContainsKey(connectionStringsList[i].Attributes["name"].Value))
|
||||
{
|
||||
// effettuo sostituzione
|
||||
connectionStringsList[i].Attributes["connectionString"].Value = currConfig.connStrRules[connectionStringsList[i].Attributes["name"].Value];
|
||||
}
|
||||
Console.WriteLine($"Errore: file conf vuoto: {currentSetFile}");
|
||||
}
|
||||
|
||||
// ora lo SALVO (il documento...)
|
||||
doc.Save(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"Errore: file conf non trovato: {currentSetFile}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user