Skip to content

Commit 1c1749b

Browse files
committed
WIP; app list, appinstance create|list|remove
1 parent 6f77c58 commit 1c1749b

File tree

6 files changed

+180
-2
lines changed

6 files changed

+180
-2
lines changed

src/SeqCli/Cli/Command.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protected virtual async Task<int> Run(string[] unrecognized)
9191

9292
protected virtual Task<int> Run() { return Task.FromResult(0); }
9393

94-
protected virtual void ShowUsageErrors(IEnumerable<string> errors)
94+
protected static void ShowUsageErrors(IEnumerable<string> errors)
9595
{
9696
foreach (var error in errors)
9797
{

src/SeqCli/Cli/Commands/App/DefineCommand.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
using System.Threading.Tasks;
1717
using SeqCli.Apps;
1818
using SeqCli.Apps.Definitions;
19-
using SeqCli.Config;
2019
using SeqCli.Util;
2120

2221
namespace SeqCli.Cli.Commands.App;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using SeqCli.Cli.Features;
5+
using SeqCli.Config;
6+
using SeqCli.Connection;
7+
8+
namespace SeqCli.Cli.Commands.App;
9+
10+
[Command("app", "list", "List installed app packages", Example="seqcli app list")]
11+
class ListCommand : Command
12+
{
13+
readonly SeqConnectionFactory _connectionFactory;
14+
15+
string? _title, _id;
16+
readonly ConnectionFeature _connection;
17+
readonly OutputFormatFeature _output;
18+
19+
string? PackageId => string.IsNullOrWhiteSpace(_title) ? null : _title.Trim();
20+
string? Id => string.IsNullOrWhiteSpace(_id) ? null : _id.Trim();
21+
22+
public ListCommand(SeqConnectionFactory connectionFactory, SeqCliConfig config)
23+
{
24+
if (config == null) throw new ArgumentNullException(nameof(config));
25+
_connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory));
26+
27+
Options.Add(
28+
"package-id=",
29+
"The package id of the app(s) to list",
30+
t => _title = t);
31+
32+
Options.Add(
33+
"i=|id=",
34+
"The id of a single app to list",
35+
t => _id = t);
36+
37+
_output = Enable(new OutputFormatFeature(config.Output));
38+
_connection = Enable<ConnectionFeature>();
39+
}
40+
41+
protected override async Task<int> Run()
42+
{
43+
if (PackageId != null && Id != null)
44+
{
45+
ShowUsageErrors(new[] {"Only one of either `package-id` or `id` can be specified"});
46+
return 1;
47+
}
48+
49+
var connection = _connectionFactory.Connect(_connection);
50+
51+
var list = Id != null ?
52+
new[] { await connection.Apps.FindAsync(Id) } :
53+
(await connection.Apps.ListAsync())
54+
.Where(ak => PackageId == null || PackageId == ak.Name)
55+
.ToArray();
56+
57+
foreach (var app in list)
58+
{
59+
_output.WriteEntity(app);
60+
}
61+
62+
return 0;
63+
}
64+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Seq.Api.Model.AppInstances;
6+
using SeqCli.Cli.Features;
7+
using SeqCli.Config;
8+
using SeqCli.Connection;
9+
using SeqCli.Util;
10+
using Serilog;
11+
12+
namespace SeqCli.Cli.Commands.AppInstance;
13+
14+
[Command("appinstance", "create", "Create a signal",
15+
Example = "seqcli appinstance create -t 'Email Ops' --app hostedapp-314159 -p [email protected]")]
16+
class CreateCommand : Command
17+
{
18+
readonly SeqConnectionFactory _connectionFactory;
19+
20+
readonly ConnectionFeature _connection;
21+
readonly OutputFormatFeature _output;
22+
23+
string? _title, _appId;
24+
readonly Dictionary<string, string> _settings = new();
25+
readonly List<string> _overridable = new();
26+
27+
public CreateCommand(SeqConnectionFactory connectionFactory, SeqCliConfig config)
28+
{
29+
_connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory));
30+
31+
Options.Add(
32+
"t=|title=",
33+
"A title for the app instance",
34+
t => _title = ArgumentString.Normalize(t));
35+
36+
Options.Add(
37+
"app=",
38+
"The id of the installed app package to instantiate",
39+
app => _appId = ArgumentString.Normalize(app));
40+
41+
Options.Add(
42+
"p={=}|property={=}",
43+
"Specify name/value settings for the app, e.g. `-p [email protected] -p Subject=\"Alert!\"`",
44+
(n, v) =>
45+
{
46+
var name = n.Trim();
47+
var valueText = v?.Trim();
48+
_settings.Add(name, valueText ?? "");
49+
});
50+
51+
Options.Add(
52+
"overridable=",
53+
"Specify setting names that may be overridden by users when invoking the app",
54+
s => _overridable.Add(s));
55+
56+
// The command doesn't yet implement "Stream incoming events".
57+
58+
_connection = Enable<ConnectionFeature>();
59+
_output = Enable(new OutputFormatFeature(config.Output));
60+
}
61+
62+
protected override async Task<int> Run()
63+
{
64+
var connection = _connectionFactory.Connect(_connection);
65+
66+
AppInstanceEntity instance = await connection.AppInstances.TemplateAsync(_appId)!;
67+
68+
bool ValidateSettingName(string settingName)
69+
{
70+
if (!instance.Settings!.ContainsKey(settingName))
71+
{
72+
Log.Error("The app does not accept a setting with name {SettingName}; available settings are: {AvailableSettings}", settingName, instance.Settings.Keys.ToArray());
73+
return false;
74+
}
75+
76+
return true;
77+
}
78+
79+
instance.Title = _title;
80+
81+
foreach (var setting in _settings)
82+
{
83+
if (!ValidateSettingName(setting.Key))
84+
return 1;
85+
86+
instance.Settings![setting.Key] = setting.Value;
87+
}
88+
89+
foreach (var overridable in _overridable)
90+
{
91+
if (!ValidateSettingName(overridable))
92+
return 1;
93+
94+
instance.InvocationOverridableSettings!.Add(overridable);
95+
}
96+
97+
instance = await connection.AppInstances.AddAsync(instance);
98+
99+
_output.WriteEntity(instance);
100+
101+
return 0;
102+
}
103+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace SeqCli.Cli.Commands.AppInstance;
2+
3+
public class ListCommand
4+
{
5+
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace SeqCli.Cli.Commands.AppInstance;
2+
3+
public class RemoveCommand
4+
{
5+
6+
}

0 commit comments

Comments
 (0)