// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1 using Npgsql; namespace Progmasoft.Progmaweb.Api.ViGet; /// /// The commands that manage the Tazel plugin catalog on the server, while publishing is not open through the API: /// viget publish-tazel-plugin <jar> and viget yank-tazel-plugin <Publisher> <Name> /// <Version> [--undo]. They run with the configuration of the service, as its user. /// internal static class ViGetCommand { /// Whether the arguments of the program ask for a ViGet command rather than the web service. public static bool IsCommand(string[] args) => args is ["viget", ..]; /// Runs a ViGet command. /// The services of the API, not started. /// The arguments after viget. /// Where results go. /// Where errors go. /// Cancels the command. /// The exit code: 0 on success, 1 when the command was refused, 2 for a wrong command line. public static async Task RunAsync( IServiceProvider services, string[] args, TextWriter output, TextWriter errors, CancellationToken cancellationToken) { // The service applies the schema when it starts; a command may run before it ever did. if (services.GetService() is { } dataSource) { await ViGetSchema.ApplyAsync(dataSource, cancellationToken); } switch (args) { case ["publish-tazel-plugin", string path]: { if (!File.Exists(path)) { await errors.WriteLineAsync($"{path} does not exist."); return 1; } TazelPluginPublishing publishing = services.GetRequiredService(); PublishResult result = await publishing.PublishAsync( await File.ReadAllBytesAsync(path, cancellationToken), cancellationToken); if (!result.Published) { await errors.WriteLineAsync(result.Error); return 1; } TazelPluginDescriptor plugin = result.Plugin!; await output.WriteLineAsync( $"Published {plugin.Publisher}.{plugin.Name} {plugin.Version}: " + $"https://viget.progmasoft.com/dslplugins/tazel/{plugin.Publisher}/{plugin.Name}"); return 0; } case ["yank-tazel-plugin", string publisher, string name, string version, .. var rest] when rest is [] or ["--undo"]: { bool yanked = rest is []; ITazelPluginCatalog catalog = services.GetRequiredService(); if (!await catalog.SetYankedAsync(publisher, name, version, yanked, cancellationToken)) { await errors.WriteLineAsync($"{publisher}.{name} has no version {version}."); return 1; } await output.WriteLineAsync( $"{publisher}.{name} {version} is {(yanked ? "yanked" : "no longer yanked")}."); return 0; } default: await errors.WriteLineAsync( "usage: viget publish-tazel-plugin \n" + " viget yank-tazel-plugin [--undo]"); return 2; } } }