tesses-cms/Tesses.CMS.Cli/Program.cs

222 lines
6.2 KiB
C#
Raw Permalink Normal View History

2023-12-16 01:40:05 +00:00
using Tesses.CMS.Client;
2024-07-28 22:59:28 +00:00
using CommandLine;
using System.Net;
using Newtonsoft.Json;
2023-12-16 01:40:05 +00:00
2024-07-28 22:59:28 +00:00
var prefs=Prefs.Create(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),"tcms.json"));
2023-12-16 01:40:05 +00:00
2024-07-28 22:59:28 +00:00
var res =Parser.Default.ParseArguments<MoviesOptions,ShowsOptions,UsersOptions,EventsOptions,EndpointOptions>(args);
res = await res.WithParsedAsync<MoviesOptions>(MoviesCallback);
res = await res.WithParsedAsync<ShowsOptions>(ShowsCallback);
res = await res.WithParsedAsync<UsersOptions>(UsersCallback);
res = await res.WithParsedAsync<EndpointOptions>(EndpointCallback);
res = res.WithParsed<EventsOptions>(EventsCallback);
void EventsCallback(EventsOptions options)
{
Console.WriteLine("About to read events");
using(var cms = new TessesCMSClient(prefs.Url))
{
using(CancellationTokenSource src=new CancellationTokenSource()){
cms.StartEvents((evt)=>{
Console.WriteLine($"Type: {evt.Type}");
Console.WriteLine($"Username: {evt.Username}");
Console.WriteLine($"Userpropername: {evt.UserProperName}");
Console.WriteLine($"Name: {evt.Name}");
Console.WriteLine($"ProperName: {evt.ProperName}");
bool hasBody = !string.IsNullOrWhiteSpace(evt.Body);
if(!string.IsNullOrWhiteSpace(evt.Description))
{
Console.WriteLine("Description:");
Console.WriteLine(evt.Description);
if(hasBody)
Console.WriteLine();
}
if(hasBody)
{
Console.WriteLine("Body:");
Console.WriteLine(evt.Body);
}
Console.WriteLine();
},src.Token);
Console.ReadLine();
src.Cancel();
}
}
}
async Task ShowsCallback(ShowsOptions options)
2023-12-16 01:40:05 +00:00
{
2024-07-28 22:59:28 +00:00
throw new NotImplementedException();
2023-12-16 01:40:05 +00:00
}
2024-07-28 22:59:28 +00:00
async Task UsersCallback(UsersOptions options)
{
if(options.Login)
{
Console.Write("Email: ");
string? email=Console.ReadLine();
if(string.IsNullOrWhiteSpace(email))
{
Console.WriteLine("Email is empty");
return;
}
string password = ReadLine.ReadPassword($"Password for {email}: ");
using(var clt = new TessesCMSClient(prefs.Url))
{
var cookie = await clt.Users.GetCookieAsync(email,password);
if(cookie.Success)
{
if(!string.IsNullOrWhiteSpace(prefs.Session))
{
await clt.Users.SetCookieAsync(prefs.Session);
await clt.Users.LogoutAsync();
}
prefs.Session = cookie.Cookie;
prefs.Save();
}
}
}
else if(options.Logout)
{
if(string.IsNullOrWhiteSpace(prefs.Session))
{
Console.WriteLine("Not logged in");
return;
}
using(var clt = new TessesCMSClient(prefs.Url))
{
await clt.Users.SetCookieAsync(prefs.Session);
await clt.Users.LogoutAsync();
prefs.Session="";
prefs.Save();
}
Console.WriteLine("Logged out");
}
else {
using(var clt = new TessesCMSClient(prefs.Url))
{
await foreach(var user in clt.Users.GetUsersAsync())
{
Console.WriteLine($"Username: {user.Username}");
Console.WriteLine($"Name: {user.ProperName}");
Console.WriteLine("About:");
Console.WriteLine(user.AboutMe);
Console.WriteLine();
}
}
}
}
2023-12-16 01:40:05 +00:00
2024-07-28 22:59:28 +00:00
async Task EndpointCallback(EndpointOptions options)
{
if(string.IsNullOrWhiteSpace(options.Url))
{
Console.WriteLine($"Current Endpoint: {prefs.Url}");
}
else
{
prefs.Url = options.Url;
prefs.Save();
Console.WriteLine($"Set Current Endpoint To: {options.Url}");
}
}
async Task MoviesCallback(MoviesOptions options)
{
if(options.List)
{
using(var clt = new TessesCMSClient(prefs.Url))
{
await foreach(var movie in clt.Movies.GetMoviesAsync(options.Username))
{
Console.WriteLine($"Title (ProperName): {movie.ProperName}");
Console.WriteLine($"Name: {movie.Name}");
Console.WriteLine($"Created: {movie.CreationTime}");
Console.WriteLine($"Last Updated: {movie.LastUpdated}");
Console.WriteLine("Description: ");
Console.WriteLine(movie.Description);
Console.WriteLine();
}
}
}
}
[Verb("endpoint",false,new string[]{"ep"},HelpText ="Change endpoint")]
internal class EndpointOptions
{
[Value(0,Required =false,HelpText = "Main Page URL for placing request")]
public string Url {get;set;} = "";
}
[Verb("users",false,HelpText = "User accounts")]
internal class UsersOptions
{
[Option('l',"login",Required =false,HelpText = "Login")]
public bool Login {get;set;}=false;
[Option('o',"logout",Required = false,HelpText = "Logout")]
public bool Logout {get;set;}=false;
}
internal class ShowsOptions
{
}
[Verb("movies",false,HelpText ="The movies")]
internal class MoviesOptions
{
[Option('l',"list",HelpText = "List movies")]
public bool List {get;set;}=false;
[Value(0,Required =true,HelpText = "Username")]
public string Username {get;set;}="";
}
internal class Prefs
{
[JsonProperty("url")]
public string Url {get;set;}="https://tessesstudios.com/";
[JsonProperty("session")]
public string Session {get;set;}="";
[JsonIgnore]
string filename{get;set;}="";
public static Prefs Create(string path)
{
if(File.Exists(path))
{
var res=JsonConvert.DeserializeObject<Prefs>(File.ReadAllText(path)) ?? new Prefs();
res.filename = path;
return res;
}
else
{
return new Prefs(){filename=path};
}
}
public void Save()
{
File.WriteAllText(filename,JsonConvert.SerializeObject(this));
}
}
[Verb("events",false,new string[]{"evts"},HelpText ="Read server sent events (for debugging)")]
internal class EventsOptions
{
}