|
| 1 | +// Copyright © Datalust Pty Ltd and Contributors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +using System; |
| 16 | +using System.Collections.Generic; |
| 17 | +using System.Threading.Tasks; |
| 18 | +using SeqCli.Cli.Features; |
| 19 | +using SeqCli.Connection; |
| 20 | +using SeqCli.Templates.Ast; |
| 21 | +using SeqCli.Templates.Import; |
| 22 | +using SeqCli.Templates.Parser; |
| 23 | +using SeqCli.Util; |
| 24 | +using Serilog; |
| 25 | + |
| 26 | +namespace SeqCli.Cli.Commands.Shared; |
| 27 | + |
| 28 | +abstract class UpdateCommand: Command |
| 29 | +{ |
| 30 | + readonly SeqConnectionFactory _connectionFactory; |
| 31 | + |
| 32 | + readonly ConnectionFeature _connection; |
| 33 | + readonly string _resourceGroupName; |
| 34 | + readonly string _entityName; |
| 35 | + |
| 36 | + string? _json; |
| 37 | + bool _jsonStdin; |
| 38 | + |
| 39 | + protected UpdateCommand(SeqConnectionFactory connectionFactory, string commandGroupName, string resourceGroupName, string? entityName = null) |
| 40 | + { |
| 41 | + _connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory)); |
| 42 | + _resourceGroupName = resourceGroupName; |
| 43 | + _entityName = entityName ?? commandGroupName; |
| 44 | + |
| 45 | + Options.Add( |
| 46 | + "json=", |
| 47 | + $"The updated {_entityName} in JSON format; this can be produced using `seqcli {commandGroupName} list --json`", |
| 48 | + p => _json = ArgumentString.Normalize(p)); |
| 49 | + |
| 50 | + Options.Add( |
| 51 | + "json-stdin", |
| 52 | + $"Read the updated {_entityName} as JSON from `STDIN`", |
| 53 | + _ => _jsonStdin = true); |
| 54 | + |
| 55 | + _connection = Enable<ConnectionFeature>(); |
| 56 | + } |
| 57 | + |
| 58 | + protected override async Task<int> Run() |
| 59 | + { |
| 60 | + var connection = _connectionFactory.Connect(_connection); |
| 61 | + |
| 62 | + if (_json == null && !_jsonStdin) |
| 63 | + { |
| 64 | + Log.Error("One of either `--json` or `--json-stdin` must be specified"); |
| 65 | + return 1; |
| 66 | + } |
| 67 | + |
| 68 | + var json = _json ?? await Console.In.ReadToEndAsync(); |
| 69 | + |
| 70 | + if (!JsonTemplateParser.TryParse(json, out var template, out var error, out _)) |
| 71 | + { |
| 72 | + Log.Error("The {EntityName} JSON could not be parsed: {Error}", _entityName, error); |
| 73 | + return 1; |
| 74 | + } |
| 75 | + |
| 76 | + if (template is not JsonTemplateObject obj || |
| 77 | + !obj.Members.TryGetValue("Id", out var idValue) || |
| 78 | + idValue is not JsonTemplateString id) |
| 79 | + { |
| 80 | + Log.Error("The {EntityName} JSON must be an object literal with a valid string `Id` property", _entityName); |
| 81 | + return 1; |
| 82 | + } |
| 83 | + |
| 84 | + var templateName = "JSON"; |
| 85 | + var entityTemplate = new EntityTemplate(_resourceGroupName, templateName, template); |
| 86 | + var state = new TemplateImportState(); |
| 87 | + state.AddOrUpdateCreatedEntityId(templateName, id.Value); |
| 88 | + await TemplateSetImporter.ImportAsync([entityTemplate], connection, new Dictionary<string, JsonTemplate>(), state, merge: false); |
| 89 | + |
| 90 | + return 0; |
| 91 | + } |
| 92 | +} |
0 commit comments