Skip to content

Commit 3f16c7d

Browse files
authored
Merge pull request #345 from nblumhardt/update-commands
Implement `update` commands for commonly-used entities
2 parents 2678561 + b5b77cb commit 3f16c7d

22 files changed

+405
-31
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,5 @@ __pycache__/
285285
*.btm.cs
286286
*.odx.cs
287287
*.xsd.cs
288+
289+
.DS_Store
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 Seq.Api;
16+
using SeqCli.Connection;
17+
18+
namespace SeqCli.Cli.Commands.ApiKey;
19+
20+
[Command("apikey", "update",
21+
"Update an existing API key",
22+
Example="seqcli apikey update --json '{...}'")]
23+
class UpdateCommand(SeqConnectionFactory connectionFactory):
24+
Shared.UpdateCommand(connectionFactory, "apikey", nameof(SeqConnection.ApiKeys), "API key");
25+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 Seq.Api;
16+
using SeqCli.Connection;
17+
18+
namespace SeqCli.Cli.Commands.AppInstance;
19+
20+
[Command("appinstance", "update",
21+
"Update an existing app instance",
22+
Example="seqcli appinstance update --json '{...}'")]
23+
class UpdateCommand(SeqConnectionFactory connectionFactory):
24+
Shared.UpdateCommand(connectionFactory, "appinstance", nameof(SeqConnection.AppInstances), "app instance");
25+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 Seq.Api;
16+
using SeqCli.Connection;
17+
18+
namespace SeqCli.Cli.Commands.Feed;
19+
20+
[Command("feed", "update",
21+
"Update an existing NuGet feed",
22+
Example="seqcli feed update --json '{...}'")]
23+
class UpdateCommand(SeqConnectionFactory connectionFactory):
24+
Shared.UpdateCommand(connectionFactory, "feed", nameof(SeqConnection.Feeds), "NuGet feed");
25+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 Seq.Api;
16+
using SeqCli.Connection;
17+
18+
namespace SeqCli.Cli.Commands.RetentionPolicy;
19+
20+
[Command("retention", "update",
21+
"Update an existing retention policy",
22+
Example="seqcli retention update --json '{...}'")]
23+
class UpdateCommand(SeqConnectionFactory connectionFactory):
24+
Shared.UpdateCommand(connectionFactory, "retention", nameof(SeqConnection.RetentionPolicies), "retention policy");
25+
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 Seq.Api;
16+
using SeqCli.Connection;
17+
18+
namespace SeqCli.Cli.Commands.Signal;
19+
20+
[Command("signal", "update",
21+
"Update an existing signal",
22+
Example="seqcli signal update --json '{...}'")]
23+
class UpdateCommand(SeqConnectionFactory connectionFactory):
24+
Shared.UpdateCommand(connectionFactory, "signal", nameof(SeqConnection.Signals));
25+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 Seq.Api;
16+
using SeqCli.Connection;
17+
18+
namespace SeqCli.Cli.Commands.User;
19+
20+
[Command("user", "update",
21+
"Update an existing user",
22+
Example="seqcli user update --json '{...}'")]
23+
class UpdateCommand(SeqConnectionFactory connectionFactory):
24+
Shared.UpdateCommand(connectionFactory, "user", nameof(SeqConnection.Users));
25+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 Seq.Api;
16+
using SeqCli.Connection;
17+
18+
namespace SeqCli.Cli.Commands.Workspace;
19+
20+
[Command("workspace", "update",
21+
"Update an existing workspace",
22+
Example="seqcli workspace update --json '{...}'")]
23+
class UpdateCommand(SeqConnectionFactory connectionFactory):
24+
Shared.UpdateCommand(connectionFactory, "workspace", nameof(SeqConnection.Workspaces));
25+

src/SeqCli/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static async Task<int> Main(string[] args)
6060
}
6161
finally
6262
{
63-
Log.CloseAndFlush();
63+
await Log.CloseAndFlushAsync();
6464
}
6565
}
6666
}

0 commit comments

Comments
 (0)