aboutsummaryrefslogtreecommitdiff
path: root/cmnext-cli/src/Commands/ChannelCommands.cs
blob: f184b0d83779b274c26232b639373829b3168307 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* Copyright (c) 2024 Vaughn Nugent
* 
* Package: CMNext.Cli
* File: Program.cs 
*
* CMNext.Cli is free software: you can redistribute it and/or modify 
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 2 of the License,
* or (at your option) any later version.
*
* CMNext.Cli is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License 
* along with CMNext.Cli. If not, see http://www.gnu.org/licenses/.
*/

using CMNext.Cli.Exceptions;
using CMNext.Cli.Model.Channels;

using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

using Typin.Attributes;
using Typin.Console;
using Typin.Utilities;


namespace CMNext.Cli.Commands
{
    public sealed class ChannelCommands
    {
        [Command("channels", Description = "Performs operations against blog channels within your cms")]
        public sealed class ChannelCommand : BaseCommand
        { }

        [Command("channels list", Description = "Lists all blog channels within your cms")]
        public sealed class ChannelList(ChannelStore store, ConsoleLogProvider logger) : ListCommand
        {
            public override string Id { get; set; } = string.Empty;

            [CommandOption("search", 's', Description = "Show only results for a channel by it's name")]
            public string Search { get; set; } = string.Empty;

            public override async ValueTask ExecuteAsync(IConsole console)
            {
                CancellationToken cancellation = console.GetCancellationToken();
                logger.SetVerbose(Verbose);

                ChannelMeta[]? channels = await store.ListAsync(cancellation);

                if (channels is null)
                {
                    console.Error.WriteLine("No channels found");
                    return;
                }

                if(!string.IsNullOrWhiteSpace(Search))
                {
                    channels = channels.Where(c => c.Name.Contains(Search, StringComparison.OrdinalIgnoreCase)).ToArray();
                }

                console.WithForegroundColor(ConsoleColor.Green, c => c.Output.WriteLine($"Found {channels.Length} channels"));

                TableUtils.Write(
                        console.Output,
                        channels,
                        ["Id", "Name", "Path", "Index", "Content", "Feed"],
                        null,
                        c => c.Id,
                        c => c.Name,
                        c => c.Path,
                        c => $"/{c.IndexFileName}",
                        c => $"/{c.ContentDir}",
                        c => (c.Feed != null) ? "*" : ""
                    );
            }
        }

        [Command("channels delete", Description = "Deletes a channel by it's id")]
        public sealed class ChannelDelete(ChannelStore store, ConsoleLogProvider logger) : DeleteCommand
        {
            [CommandOption("channel", 'c', Description = "Specifies the channel to delete", IsRequired = true)]
            public override string Id { get; set; } = string.Empty;

            public override async ValueTask ExecuteAsync(IConsole console)
            {
                CancellationToken cancellation = console.GetCancellationToken();
                logger.SetVerbose(Verbose);
                try
                {
                    await store.DeleteAsync(Id, cancellation);
                    console.WithForegroundColor(ConsoleColor.Green, c => c.Output.WriteLine($"Deleted channel {Id}"));
                }
                catch (EntityNotFoundException)
                {
                    console.WithForegroundColor(ConsoleColor.Red, c => c.Error.WriteLine($"Channel {Id} does not exist"));
                    return;
                }
            }
        }

        [Command("channels create", Description = "Creates a new channel")]
        public sealed class ChannelCreateCommand(ChannelStore store, ConsoleLogProvider logger) : BaseCommand
        {
            [CommandOption("name", 'n', Description = "Specifies the name of the channel", IsRequired = true)]
            public string Name { get; set; } = string.Empty;

            [CommandOption("path", 'p', Description = "Specifies the path of the channel", IsRequired = true)]
            public string Path { get; set; } = string.Empty;

            [CommandOption("index", 'i', Description = "Specifies the index file name of the channel")]
            public string Index { get; set; } = "index.json";

            [CommandOption("content", 'c', Description = "Specifies the content directory of the channel", IsRequired = true)]
            public string Content { get; set; } = string.Empty;

            public async override ValueTask ExecuteAsync(IConsole console)
            {
                CancellationToken cancellation = console.GetCancellationToken();
                logger.SetVerbose(Verbose);

                ChannelMeta channel = new ()
                {
                    Name = Name,
                    Path = Path,
                    IndexFileName = Index,
                    ContentDir = Content,
                };

                await store.CreateAsync(channel, cancellation);
                console.WithForegroundColor(ConsoleColor.Green, c => c.Output.WriteLine("New channel created successfully"));
            }
        }

        [Command("channels set", Description = "Updates a channel")]
        public sealed class ChannelUpdateCommand(ChannelStore store, ConsoleLogProvider logger) : BaseCommand
        {
            [CommandOption("channel", 'c', Description = "Specifies the channel to update", IsRequired = true)]
            public string Channel { get; set; } = string.Empty;

            [CommandOption("name", 'n', Description = "Specifies the name of the channel")]
            public string Name { get; set; } = string.Empty;

            [CommandOption("index", 'i', Description = "Specifies the index file name of the channel")]
            public string Index { get; set; } = string.Empty;

            [CommandOption("content", Description = "Specifies the content directory of the channel")]
            public string Content { get; set; } = string.Empty;

            public async override ValueTask ExecuteAsync(IConsole console)
            {
                CancellationToken cancellation = console.GetCancellationToken();
                logger.SetVerbose(Verbose);

                ChannelMeta? channel = await store.GetAsync(Channel, cancellation);

                if (channel is null)
                {
                    console.WithForegroundColor(ConsoleColor.Red, c => c.Error.WriteLine($"Channel {Channel} does not exist"));
                    return;
                }

                console.Output.WriteLine($"Found channel {Channel}");

                if (!string.IsNullOrWhiteSpace(Name))
                {
                    channel.Name = Name;
                    console.Output.WriteLine($"Setting channel name to {Name}");
                }

                if (!string.IsNullOrWhiteSpace(Index))
                {
                    channel.IndexFileName = Index;
                    console.Output.WriteLine($"Setting channel index file name to {Index}");
                }

                if (!string.IsNullOrWhiteSpace(Content))
                {
                    channel.ContentDir = Content;
                    console.Output.WriteLine($"Setting channel content directory to {Content}");
                }

                await store.UpdateAsync(channel, cancellation);
                console.WithForegroundColor(ConsoleColor.Green, c => c.Output.WriteLine("Channel updated successfully"));
            }
        }

        [Command("channels feed get", Description = "Gets the RSS feed data for a channel if its set")]
        public sealed class ChannelFeedGetCommand(ChannelStore store, ConsoleLogProvider logger) : BaseCommand
        {
            [CommandOption("channel", 'c', Description = "Specifies the channel to get the feed for", IsRequired = true)]
            public string Channel { get; set; } = string.Empty;

            public async override ValueTask ExecuteAsync(IConsole console)
            {
                CancellationToken cancellation = console.GetCancellationToken();
                logger.SetVerbose(Verbose);

                ChannelMeta? channel = await store.GetAsync(Channel, cancellation);

                if (channel is null)
                {
                    console.WithForegroundColor(ConsoleColor.Red, c => c.Error.WriteLine($"Channel {Channel} does not exist"));
                    return;
                }

                console.Output.WriteLine($"Found channel {Channel}");

                if (channel.Feed is null)
                {
                    console.WithForegroundColor(ConsoleColor.Red, c => c.Error.WriteLine($"Channel {Channel} does not have a feed"));
                    return;
                }
                
                console.Output.WriteLine($"Found feed for channel {Channel}");

                console.ForegroundColor = ConsoleColor.Gray;
                console.Output.WriteLine($"  Path: {channel.Feed.Path}");
                console.Output.WriteLine($"  Description: {channel.Feed.Description}");
                console.Output.WriteLine($"  Author: {channel.Feed.Author}");
                console.Output.WriteLine($"  Contact: {channel.Feed.Contact}");
                console.Output.WriteLine($"  Image: {channel.Feed.ImagePath}");               
                console.Output.WriteLine($"  Link: {channel.Feed.Url}");

                console.ResetColor();
            }
        }

        [Command("channels feed set", Description = "Sets the RSS feed data for a channel")]
        public sealed class ChannelFeedSetCommand(ChannelStore store, ConsoleLogProvider logger) : BaseCommand
        {
            [CommandOption("channel", 'c', Description = "Specifies the channel to set the feed for", IsRequired = true)]
            public string Channel { get; set; } = string.Empty;

            [CommandOption("path", 'p', Description = "Specifies the path of the feed")]
            public string Path { get; set; } = string.Empty;

            [CommandOption("description", 'd', Description = "Specifies the description of the feed")]
            public string Description { get; set; } = string.Empty;

            [CommandOption("author", 'a', Description = "Specifies the author of the feed")]
            public string Author { get; set; } = string.Empty;

            [CommandOption("contact", Description = "Specifies the contact email address of the feed")]
            public string Contact { get; set; } = string.Empty;

            [CommandOption("image", 'i', Description = "Specifies the image of the feed")]
            public string Image { get; set; } = string.Empty;

            [CommandOption("link", 'l', Description = "Specifies the link of the feed")]
            public string Link { get; set; } = string.Empty;

            public async override ValueTask ExecuteAsync(IConsole console)
            {
                CancellationToken cancellation = console.GetCancellationToken();
                logger.SetVerbose(Verbose);

                ChannelMeta? channel = await store.GetAsync(Channel, cancellation);

                if (channel is null)
                {
                    console.WithForegroundColor(ConsoleColor.Red, c => c.Error.WriteLine($"Channel {Channel} does not exist"));
                    return;
                }

                console.Output.WriteLine($"Found channel {Channel}");

                bool modified = false;

                //Allow creating a new feed if the channel doesn't have one
                channel.Feed ??= new ChannelFeed();

                if (!string.IsNullOrWhiteSpace(Path))
                {
                    channel.Feed.Path = Path;
                    console.Output.WriteLine($"Setting feed path to {Path}");
                    modified = true;
                }
                if (!string.IsNullOrWhiteSpace(Contact))
                {
                    channel.Feed.Contact = Contact;
                    console.Output.WriteLine($"Setting feed contact to {Contact}");
                    modified = true;
                }

                if (!string.IsNullOrWhiteSpace(Description))
                {
                    channel.Feed.Description = Description;
                    console.Output.WriteLine($"Setting feed description to {Description}");
                    modified = true;
                }

                if (!string.IsNullOrWhiteSpace(Author))
                {
                    channel.Feed.Author = Author;
                    console.Output.WriteLine($"Setting feed author to {Author}");
                    modified = true;
                }

                if (!string.IsNullOrWhiteSpace(Image))
                {
                    channel.Feed.ImagePath = Image;
                    console.Output.WriteLine($"Setting feed image to {Image}");
                    modified = true;
                }

                if (!string.IsNullOrWhiteSpace(Link))
                {
                    channel.Feed.Url = Link;
                    console.Output.WriteLine($"Setting feed link to {Link}");
                    modified = true;
                }

                if (!modified)
                {
                    console.WithForegroundColor(ConsoleColor.Yellow, c => c.Error.WriteLine($"No changes made to channel {Channel}"));
                    return;
                }

                await store.UpdateAsync(channel, cancellation);
                console.WithForegroundColor(ConsoleColor.Green, c => c.Output.WriteLine("Channel feed updated successfully"));
            }

            [Command("channels feed delete", Description = "Deletes the RSS feed data for a channel")]
            public sealed class ChannelFeedDeleteCommand(ChannelStore store, ConsoleLogProvider logger) : BaseCommand
            {
                [CommandOption("channel", 'c', Description = "Specifies the channel to delete the feed for", IsRequired = true)]
                public string Channel { get; set; } = string.Empty;

                public async override ValueTask ExecuteAsync(IConsole console)
                {
                    CancellationToken cancellation = console.GetCancellationToken();
                    logger.SetVerbose(Verbose);

                    ChannelMeta? channel = await store.GetAsync(Channel, cancellation);

                    if (channel is null)
                    {
                        console.WithForegroundColor(ConsoleColor.Red, c => c.Error.WriteLine($"Channel {Channel} does not exist"));
                        return;
                    }

                    console.Output.WriteLine($"Found channel {Channel}");

                    if (channel.Feed is null)
                    {
                        console.WithForegroundColor(ConsoleColor.Red, c => c.Error.WriteLine($"Channel {Channel} does not have a feed"));
                        return;
                    }

                    console.Output.WriteLine($"Found feed for channel {Channel}");

                    channel.Feed = null;

                    await store.UpdateAsync(channel, cancellation);
                    console.WithForegroundColor(ConsoleColor.Green, c => c.Output.WriteLine("Channel feed deleted successfully"));
                }
            }

        }
    }

    

}