diff --git a/src/SeafileCli/Argparse/AuthorizationOptions.cs b/src/SeafileCli/Argparse/AuthorizationOptions.cs new file mode 100644 index 0000000..addfc26 --- /dev/null +++ b/src/SeafileCli/Argparse/AuthorizationOptions.cs @@ -0,0 +1,30 @@ +using System.Threading.Tasks; +using CommandLine; +using SeafClient; + +namespace SeafileCli.Argparse +{ + public class AuthorizationOptions : CommonOptions + { + [Option('u', "username", HelpText = "The username used for authorization.")] + public string Username { get; set; } + + [Option('p', "password", HelpText = "The password used for authorization.")] + public string Password { get; set; } + + [Option('t', "token", + HelpText = + "The API Token used for authorization. If the token is used, username and password are not required.")] + public string Token { get; set; } + + public async Task GetSession() + { + if (!string.IsNullOrEmpty(Token)) + { + return await SeafSession.FromToken(ServerUri, Token); + } + + return await SeafSession.Establish(ServerUri, Username, Password.ToCharArray()); + } + } +} \ No newline at end of file diff --git a/src/SeafileCli/Argparse/CommonOptions.cs b/src/SeafileCli/Argparse/CommonOptions.cs index 664f9f9..7a361e0 100644 --- a/src/SeafileCli/Argparse/CommonOptions.cs +++ b/src/SeafileCli/Argparse/CommonOptions.cs @@ -1,7 +1,5 @@ using System; -using System.Threading.Tasks; using CommandLine; -using SeafClient; namespace SeafileCli.Argparse { @@ -10,25 +8,6 @@ public class CommonOptions [Option('s', "server", HelpText = "URL to the server. e.g. https://seafile.example.com/", Required = true)] public string Server { get; set; } - [Option('u', "username", HelpText = "The username used for authorization.")] - public string Username { get; set; } - - [Option('p', "password", HelpText = "The password used for authorization.")] - public string Password { get; set; } - - [Option('t', "token", HelpText = "The API Token used for authorization. If the token is used, username and password are not required.")] - public string Token { get; set; } - public Uri ServerUri => new Uri(Server); - - public async Task GetSession() - { - if (!string.IsNullOrEmpty(Token)) - { - return await SeafSession.FromToken(ServerUri, Token); - } - - return await SeafSession.Establish(ServerUri, Username, Password.ToCharArray()); - } } } \ No newline at end of file diff --git a/src/SeafileCli/Argparse/Options.cs b/src/SeafileCli/Argparse/Options.cs index c67c5ae..1e300fd 100644 --- a/src/SeafileCli/Argparse/Options.cs +++ b/src/SeafileCli/Argparse/Options.cs @@ -10,15 +10,20 @@ public class Options { public Options() { - TokenVerb = new CommonOptions(); + TokenVerb = new AuthorizationOptions(); + AccountInfoVerb = new AuthorizationOptions(); + ServerInfoVerb = new CommonOptions(); UploadVerb = new UploadSubOptions(); } [VerbOption("token", HelpText = "Retrieves the authorization token with the provided username and password.")] - public CommonOptions TokenVerb { get; set; } + public AuthorizationOptions TokenVerb { get; set; } [VerbOption("account-info", HelpText = "Retrieves the user account information.")] - public CommonOptions AccountInfoVerb { get; set; } + public AuthorizationOptions AccountInfoVerb { get; set; } + + [VerbOption("server-info", HelpText = "Retrieves the server information.")] + public CommonOptions ServerInfoVerb { get; set; } [VerbOption("upload", HelpText = "Uploads files and folders to seafile.")] public UploadSubOptions UploadVerb { get; set; } diff --git a/src/SeafileCli/Argparse/UploadSubOptions.cs b/src/SeafileCli/Argparse/UploadSubOptions.cs index 09c98b0..b6ccec2 100644 --- a/src/SeafileCli/Argparse/UploadSubOptions.cs +++ b/src/SeafileCli/Argparse/UploadSubOptions.cs @@ -2,12 +2,16 @@ namespace SeafileCli.Argparse { - public class UploadSubOptions : CommonOptions + public class UploadSubOptions : AuthorizationOptions { - [Option('l', "library", HelpText = "The name of the remote library, where the files should be uploaded to.", Required = true)] + [Option('l', "library", HelpText = "The name of the remote library, where the files should be uploaded to.", + Required = true)] public string Library { get; set; } - [Option('d', "directory", HelpText = "The remote directory, where the files should be uploaded to. Path seperator is the forward slash (/).", DefaultValue = "/")] + [Option('d', "directory", + HelpText = + "The remote directory, where the files should be uploaded to. Path seperator is the forward slash (/).", + DefaultValue = "/")] public string Directory { get; set; } [OptionArray('f', "files", HelpText = "The files to upload.", Required = true)] diff --git a/src/SeafileCli/Program.cs b/src/SeafileCli/Program.cs index a9667ac..fcdea24 100644 --- a/src/SeafileCli/Program.cs +++ b/src/SeafileCli/Program.cs @@ -25,13 +25,16 @@ public static void Main(string[] args) switch (invokedVerb) { case "token": - new TokenHandler((CommonOptions) invokedVerbInstance).Run(); + new TokenHandler((AuthorizationOptions) invokedVerbInstance).Run(); break; case "upload": new UploadHandler((UploadSubOptions) invokedVerbInstance).Run(); break; case "account-info": - new AccountInfoHandler((CommonOptions) invokedVerbInstance).Run(); + new AccountInfoHandler((AuthorizationOptions) invokedVerbInstance).Run(); + break; + case "server-info": + new ServerInfoHandler((CommonOptions) invokedVerbInstance).Run(); break; default: Environment.Exit(Parser.DefaultExitCodeFail); diff --git a/src/SeafileCli/SeafileCli.csproj b/src/SeafileCli/SeafileCli.csproj index 256f565..74416fa 100644 --- a/src/SeafileCli/SeafileCli.csproj +++ b/src/SeafileCli/SeafileCli.csproj @@ -58,12 +58,14 @@ Properties\SolutionInfo.cs + + diff --git a/src/SeafileCli/VerbHandler/AccountInfoHandler.cs b/src/SeafileCli/VerbHandler/AccountInfoHandler.cs index d1e2439..3e8a4f1 100644 --- a/src/SeafileCli/VerbHandler/AccountInfoHandler.cs +++ b/src/SeafileCli/VerbHandler/AccountInfoHandler.cs @@ -8,9 +8,9 @@ namespace SeafileCli.VerbHandler /// public class AccountInfoHandler : IVerbHandler { - private readonly CommonOptions _options; + private readonly AuthorizationOptions _options; - public AccountInfoHandler(CommonOptions options) + public AccountInfoHandler(AuthorizationOptions options) { _options = options; } diff --git a/src/SeafileCli/VerbHandler/ServerInfoHandler.cs b/src/SeafileCli/VerbHandler/ServerInfoHandler.cs new file mode 100644 index 0000000..5e7eea7 --- /dev/null +++ b/src/SeafileCli/VerbHandler/ServerInfoHandler.cs @@ -0,0 +1,30 @@ +using System; +using SeafClient; +using SeafileCli.Argparse; + +namespace SeafileCli.VerbHandler +{ + /// + /// Retrieves the server information and prints it on the console. + /// + public class ServerInfoHandler : IVerbHandler + { + private readonly CommonOptions _options; + + public ServerInfoHandler(CommonOptions options) + { + _options = options; + } + + public void Run() + { + var serverInfo = SeafSession.GetServerInfo(_options.ServerUri).Result; + Console.WriteLine($"Version: {serverInfo.Version}"); + Console.WriteLine("Features:"); + foreach (var feature in serverInfo.Features) + { + Console.WriteLine($"- {feature}"); + } + } + } +} diff --git a/src/SeafileCli/VerbHandler/TokenHandler.cs b/src/SeafileCli/VerbHandler/TokenHandler.cs index e7d1a0d..55ed197 100644 --- a/src/SeafileCli/VerbHandler/TokenHandler.cs +++ b/src/SeafileCli/VerbHandler/TokenHandler.cs @@ -8,9 +8,9 @@ namespace SeafileCli.VerbHandler /// public class TokenHandler : IVerbHandler { - private readonly CommonOptions _options; + private readonly AuthorizationOptions _options; - public TokenHandler(CommonOptions options) + public TokenHandler(AuthorizationOptions options) { _options = options; }