From 45d95194996a4e27d37db03fe89604f91d3a8cce Mon Sep 17 00:00:00 2001 From: Stafford Williams Date: Tue, 14 Apr 2020 14:34:28 +1000 Subject: [PATCH] add push command --- src/tye/Program.PushCommand.cs | 76 ++++++++++++++++++++++++++++++++++ src/tye/Program.cs | 1 + 2 files changed, 77 insertions(+) create mode 100644 src/tye/Program.PushCommand.cs diff --git a/src/tye/Program.PushCommand.cs b/src/tye/Program.PushCommand.cs new file mode 100644 index 000000000..5519a6e70 --- /dev/null +++ b/src/tye/Program.PushCommand.cs @@ -0,0 +1,76 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; +using System.CommandLine; +using System.CommandLine.Invocation; +using System.IO; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Tye.ConfigModel; + +namespace Microsoft.Tye +{ + static partial class Program + { + public static Command CreatePushCommand() + { + var command = new Command("push", "build and push application containers to registry") + { + CommonArguments.Path_Required, + StandardOptions.Interactive, + StandardOptions.Verbosity, + }; + + command.AddOption(new Option(new[] { "-f", "--force" }) + { + Description = "Override validation and force push.", + Required = false + }); + + command.Handler = CommandHandler.Create(async (console, path, verbosity, interactive, force) => + { + // Workaround for https://github.com/dotnet/command-line-api/issues/723#issuecomment-593062654 + if (path is null) + { + throw new CommandException("No project or solution file was found."); + } + + var output = new OutputContext(console, verbosity); + + output.WriteInfoLine("Loading Application Details..."); + var application = await ApplicationFactory.CreateAsync(output, path); + if (application.Services.Count == 0) + { + throw new CommandException($"No services found in \"{application.Source.Name}\""); + } + + await ExecutePushAsync(new OutputContext(console, verbosity), application, environment: "production", interactive, force); + }); + + return command; + } + + private static async Task ExecutePushAsync(OutputContext output, ApplicationBuilder application, string environment, bool interactive, bool force) + { + await application.ProcessExtensionsAsync(ExtensionContext.OperationKind.Deploy); + + var steps = new List() + { + new CombineStep() { Environment = environment, }, + new PublishProjectStep(), + new BuildDockerImageStep() { Environment = environment, }, + new PushDockerImageStep() { Environment = environment, }, + }; + + ApplyRegistryAndDefaults(output, application, interactive, requireRegistry: true); + + var executor = new ServiceExecutor(output, application, steps); + foreach (var service in application.Services) + { + await executor.ExecuteAsync(service); + } + } + } +} diff --git a/src/tye/Program.cs b/src/tye/Program.cs index 454dc5c1d..77e7d6784 100644 --- a/src/tye/Program.cs +++ b/src/tye/Program.cs @@ -27,6 +27,7 @@ public static Task Main(string[] args) command.AddCommand(CreateGenerateCommand()); command.AddCommand(CreateRunCommand(args)); command.AddCommand(CreateBuildCommand()); + command.AddCommand(CreatePushCommand()); command.AddCommand(CreateDeployCommand()); // Show commandline help unless a subcommand was used.