Example #1
0
        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();
            }
        }