// See https://aka.ms/new-console-template for more information using ConfMan.IOB.CApp; using Spectre.Console; using System.Diagnostics; using System.Net; using static System.Net.Mime.MediaTypeNames; AnsiConsole.Write( new FigletText("ConfMan.IOB") .LeftAligned() .Color(Color.Blue1)); var rule = new Rule("[green]IOB configuration file manager[/]"); rule.Alignment = Justify.Left; AnsiConsole.Write(rule); // recupero configurazione... CAConf currConf = new CAConf(); if (File.Exists("conf.yaml")) { currConf = CMan.readConf("conf.yaml"); } AnsiConsole.WriteLine(); AnsiConsole.MarkupLineInterpolated($"Conf file type: [yellow]{currConf.FileType}[/]"); var typeOk = AnsiConsole.Confirm("Confermi tipo file?"); while (!typeOk) { currConf.FileType = AnsiConsole.Ask("Inserisci il type desiderato:"); AnsiConsole.WriteLine(currConf.FileType); typeOk = AnsiConsole.Confirm("Confermi tipo file?"); } // aggiungo "*" se mancasse... if (currConf.FileType.Substring(0, 1) != "*") { currConf.FileType = $"*{currConf.FileType}"; } AnsiConsole.MarkupLineInterpolated($"Confermato file type: [green]{currConf.FileType}[/]"); var path = new TextPath(currConf.ConfDir); AnsiConsole.WriteLine($"Default conf path:"); AnsiConsole.Write(path); //AnsiConsole.WriteLine(); var fileList = Directory.GetFiles(currConf.ConfDir, currConf.FileType); AnsiConsole.MarkupLineInterpolated($"Trovati [yellow]{fileList.Count()}[/] file..."); var pathOK = AnsiConsole.Confirm("Confermi il Path?"); while (!pathOK) { currConf.ConfDir = AnsiConsole.Ask("Inserisci il percorso completo:"); if (!string.IsNullOrEmpty(currConf.ConfDir)) { path = new TextPath(currConf.ConfDir); AnsiConsole.Write(path); // verifico esista la directory... if (!Directory.Exists(currConf.ConfDir)) { AnsiConsole.MarkupLine("Attenzione: path inserito [red]non[/] valido!"); } else { // recupero il numero di file ini cercati fileList = Directory.GetFiles(currConf.ConfDir, currConf.FileType); AnsiConsole.MarkupLineInterpolated($"Trovati [yellow]{fileList.Count()}[/] file..."); pathOK = AnsiConsole.Confirm("Confermi il Path?"); } } } rule = new Rule("[green]TAGS[/] selection"); rule.Alignment = Justify.Left; AnsiConsole.Write(rule); string hostName = Dns.GetHostName(); var hostAddress = Dns.GetHostAddresses(hostName); string hostAddr = ""; if (hostAddress != null) { if (hostAddress.Count() > 1) { var ipList = hostAddress.Select(x => $"{x}").ToList(); hostAddr = AnsiConsole.Prompt( new SelectionPrompt() .Title("Selezione indirizzo [green]IP[/]") .MoreChoicesText("[grey](Move up and down to reveal more)[/]") .AddChoices(ipList)); } else { hostAddr = $"{hostAddress[0]}"; } } var custName = AnsiConsole.Ask("Inserisci valore per [yellow]Customer[/]:", currConf.Customer); var HostOS = AnsiConsole.Ask("Inserisci valore per [yellow]OS[/]:", currConf.OS); var HostName = AnsiConsole.Ask("Inserisci valore per [yellow]HostName[/]:", hostName); var HostAddr = AnsiConsole.Ask("Inserisci valore per [yellow]HostAddress (IP)[/]:", hostAddr); // salvo i tagList... Dictionary newTags = new Dictionary(); newTags.Add("Customer", custName); newTags.Add("HostOS", HostOS); newTags.Add("HostName", HostName); newTags.Add("HostAddr", HostAddr); AnsiConsole.WriteLine(); // ora processa tutti i file, andando ad aggiungere in coda i TAGS selezionati... Stopwatch sw = new Stopwatch(); sw.Start(); foreach (var confFile in fileList) { TagManager tMan = new TagManager(confFile); tMan.addTagList(newTags); } sw.Stop(); var elTime = sw.Elapsed; rule = new Rule("File update [green]Completed[/]!"); rule.Alignment = Justify.Left; AnsiConsole.Write(rule); AnsiConsole.MarkupLineInterpolated($"Processati [yellow]{fileList.Count()}[/] file in {elTime.TotalMilliseconds}ms");