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 an instance of an installed app" ,
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+ }
0 commit comments