Skip to content

Commit

Permalink
add server-info verb
Browse files Browse the repository at this point in the history
  • Loading branch information
corux committed Jul 13, 2016
1 parent cf9e449 commit e941ac8
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 33 deletions.
30 changes: 30 additions & 0 deletions src/SeafileCli/Argparse/AuthorizationOptions.cs
Original file line number Diff line number Diff line change
@@ -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<SeafSession> GetSession()
{
if (!string.IsNullOrEmpty(Token))
{
return await SeafSession.FromToken(ServerUri, Token);
}

return await SeafSession.Establish(ServerUri, Username, Password.ToCharArray());
}
}
}
21 changes: 0 additions & 21 deletions src/SeafileCli/Argparse/CommonOptions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Threading.Tasks;
using CommandLine;
using SeafClient;

namespace SeafileCli.Argparse
{
Expand All @@ -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<SeafSession> GetSession()
{
if (!string.IsNullOrEmpty(Token))
{
return await SeafSession.FromToken(ServerUri, Token);
}

return await SeafSession.Establish(ServerUri, Username, Password.ToCharArray());
}
}
}
11 changes: 8 additions & 3 deletions src/SeafileCli/Argparse/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
10 changes: 7 additions & 3 deletions src/SeafileCli/Argparse/UploadSubOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
7 changes: 5 additions & 2 deletions src/SeafileCli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/SeafileCli/SeafileCli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,14 @@
<Compile Include="..\SolutionInfo.cs">
<Link>Properties\SolutionInfo.cs</Link>
</Compile>
<Compile Include="Argparse\AuthorizationOptions.cs" />
<Compile Include="Argparse\CommonOptions.cs" />
<Compile Include="Argparse\Options.cs" />
<Compile Include="Argparse\UploadSubOptions.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SeafSessionExtensions.cs" />
<Compile Include="VerbHandler\ServerInfoHandler.cs" />
<Compile Include="VerbHandler\IVerbHandler.cs" />
<Compile Include="VerbHandler\AccountInfoHandler.cs" />
<Compile Include="VerbHandler\TokenHandler.cs" />
Expand Down
4 changes: 2 additions & 2 deletions src/SeafileCli/VerbHandler/AccountInfoHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace SeafileCli.VerbHandler
/// </summary>
public class AccountInfoHandler : IVerbHandler
{
private readonly CommonOptions _options;
private readonly AuthorizationOptions _options;

public AccountInfoHandler(CommonOptions options)
public AccountInfoHandler(AuthorizationOptions options)
{
_options = options;
}
Expand Down
30 changes: 30 additions & 0 deletions src/SeafileCli/VerbHandler/ServerInfoHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using SeafClient;
using SeafileCli.Argparse;

namespace SeafileCli.VerbHandler
{
/// <summary>
/// Retrieves the server information and prints it on the console.
/// </summary>
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}");
}
}
}
}
4 changes: 2 additions & 2 deletions src/SeafileCli/VerbHandler/TokenHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace SeafileCli.VerbHandler
/// </summary>
public class TokenHandler : IVerbHandler
{
private readonly CommonOptions _options;
private readonly AuthorizationOptions _options;

public TokenHandler(CommonOptions options)
public TokenHandler(AuthorizationOptions options)
{
_options = options;
}
Expand Down

0 comments on commit e941ac8

Please sign in to comment.