private static async Task ExecuteAsync(OutputContext output, DirectoryInfo directory) { var opulenceFilePath = DirectorySearch.AscendingSearch(directory.FullName, "opulence.json"); if (opulenceFilePath != null) { output.WriteInfoLine($"found 'opulence.json' at '{Path.GetDirectoryName(opulenceFilePath)}'"); return; } output.WriteInfoLine("locating nearest sln file"); var solutionFilePath = DirectorySearch.AscendingWildcardSearch(directory.FullName, "*.sln").FirstOrDefault()?.FullName; if (opulenceFilePath == null && solutionFilePath != null && Confirm(output, $"use '{Path.GetDirectoryName(solutionFilePath)}' as root?")) { opulenceFilePath = Path.Combine(Path.GetDirectoryName(solutionFilePath) !, "opulence.json"); } if (opulenceFilePath == null && Confirm(output, "use project directory as root?")) { opulenceFilePath = Path.Combine(directory.FullName, "opulence.json"); } if (opulenceFilePath == null) { throw new CommandException("cannot determine root directory"); } var config = new OpulenceConfig() { Container = new ContainerConfig() { Registry = new RegistryConfig(), } }; while (true) { output.WriteAlways("entry the container registry hostname (ex: example.azurecr.io): "); var line = Console.ReadLine(); output.WriteAlwaysLine(string.Empty); if (!string.IsNullOrEmpty(line)) { config.Container.Registry.Hostname = line.Trim(); break; } } using var stream = File.OpenWrite(opulenceFilePath); await JsonSerializer.SerializeAsync(stream, config, new JsonSerializerOptions() { WriteIndented = true, }); output.WriteInfo($"initialized opulence config at '{opulenceFilePath}'"); }
private static async Task ExecuteAsync(OutputContext output, DirectoryInfo directory) { output.WriteBanner(); string?solutionFilePath = null; string?opulenceFilePath = null; using (var step = output.BeginStep("Looking For Existing Config...")) { opulenceFilePath = DirectorySearch.AscendingSearch(directory.FullName, "Opulence.csx"); if (opulenceFilePath != null) { output.WriteInfoLine($"Found 'Opulence.csx' at '{opulenceFilePath}'"); step.MarkComplete(); return; } else { output.WriteInfoLine("Not Found"); step.MarkComplete(); } } using (var step = output.BeginStep("Looking For .sln File...")) { solutionFilePath = DirectorySearch.AscendingWildcardSearch(directory.FullName, "*.sln").FirstOrDefault()?.FullName; if (opulenceFilePath == null && solutionFilePath != null && output.Confirm($"Use '{Path.GetDirectoryName(solutionFilePath)}' as Root?")) { opulenceFilePath = Path.Combine(Path.GetDirectoryName(solutionFilePath) !, "Opulence.csx"); step.MarkComplete(); } else { output.WriteInfoLine("Not Found."); step.MarkComplete(); } } if (opulenceFilePath == null && Path.GetFullPath(directory.FullName) != Path.GetFullPath(Environment.CurrentDirectory)) { // User specified a directory other than the current one using (var step = output.BeginStep("Trying Project Directory...")) { if (output.Confirm("Use Project Directory as Root?")) { opulenceFilePath = Path.Combine(directory.FullName, "Opulence.csx"); } step.MarkComplete(); } } if (opulenceFilePath == null) { using (var step = output.BeginStep("Trying Current Directory...")) { if (output.Confirm("Use Current Directory as Root?")) { opulenceFilePath = Path.Combine(directory.FullName, "Opulence.csx"); } step.MarkComplete(); } } if (opulenceFilePath == null) { throw new CommandException("Cannot Determine Root Directory."); } using (var step = output.BeginStep("Writing Opulence.csx ...")) { var hostname = output.Prompt("Enter the Container Registry (ex: 'example.azurecr.io' for Azure or 'example' for dockerhub)"); var services = new List <(string, string)>(); if (solutionFilePath != null && output.Confirm($"Use solution file '{solutionFilePath}' to initialize services?")) { services.AddRange(ReadServicesFromSolution(solutionFilePath)); services.Sort((a, b) => a.Item1.CompareTo(b.Item1)); } using var stream = File.OpenWrite(opulenceFilePath); using var writer = new StreamWriter(stream, Encoding.UTF8, leaveOpen: true); await WriteOpulenceConfigAsync(writer, hostname, services); output.WriteInfoLine($"Initialized Opulence Config at '{opulenceFilePath}'."); step.MarkComplete(); } }