Skip to content

Commit

Permalink
Merge pull request #8 from trossr32/6-add_web_ui_launch_cmdlet
Browse files Browse the repository at this point in the history
fixes #6 - add Invoke-TransmissionWeb cmdlet to open transmission in …
  • Loading branch information
trossr32 authored Sep 9, 2020
2 parents 0fe6536 + 728e2f6 commit 803f90b
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Get-TransmissionSessionStatistics
```powershell
Test-TransmissionPort
Update-TransmissionBlockLists
Invoke-TransmissionWeb
```

### Torrents
Expand Down
68 changes: 68 additions & 0 deletions src/PsTransmission/System/InvokeTransmissionWebCmdlet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Diagnostics;
using System.Management.Automation;
using System.Runtime.InteropServices;
using Flurl;
using PsTransmission.Core.Components;
using Transmission.Base;

namespace Transmission.System
{
[Cmdlet(VerbsLifecycle.Invoke, "TransmissionWeb", HelpUri = "https://github.com/trossr32/ps-transmission-manager")]
public class InvokeTransmissionWebCmdlet : BaseTransmissionCmdlet
{
/// <summary>
/// Implements the <see cref="BeginProcessing"/> method for <see cref="InvokeTransmissionWebCmdlet"/>.
/// </summary>
protected override void BeginProcessing()
{
base.BeginProcessing();
}

/// <summary>
/// Implements the <see cref="ProcessRecord"/> method for <see cref="InvokeTransmissionWebCmdlet"/>.
/// </summary>
protected override void ProcessRecord()
{
string url = new Url(TransmissionContext.Credentials.Host).Root;

try
{
Process.Start(url);

WriteObject("Web UI launched");
}
catch (Exception e)
{
// hack because of this: https://github.com/dotnet/corefx/issues/10361
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
url = url.Replace("&", "^&");

Process.Start(new ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = true });
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Process.Start("xdg-open", url);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Process.Start("open", url);
}
else
{
ThrowTerminatingError(new ErrorRecord(new Exception($"Failed to launch web UI. {e.Message}", e), null, ErrorCategory.OperationStopped, null));
}
}
}

/// <summary>
/// Implements the <see cref="EndProcessing"/> method for <see cref="InvokeTransmissionWebCmdlet"/>.
/// Retrieve all torrents
/// </summary>
protected override void EndProcessing()
{

}
}
}
40 changes: 40 additions & 0 deletions src/PsTransmission/Transmission.dll-Help.xml
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,46 @@
</maml:navigationLink>
</maml:relatedLinks>
</command:command>
<command:command
xmlns:maml="http://schemas.microsoft.com/maml/2004/10"
xmlns:command="http://schemas.microsoft.com/maml/dev/command/2004/10"
xmlns:dev="http://schemas.microsoft.com/maml/dev/2004/10"
xmlns:MSHelp="http://msdn.microsoft.com/mshelp">
<command:details>
<command:name>Invoke-TransmissionWeb</command:name>
<command:verb>Invoke</command:verb>
<command:noun>TransmissionWeb</command:noun>
<maml:description>
<maml:para>
Opens the transmission web UI in the default system browser.
</maml:para>
</maml:description>
</command:details>
<maml:description>
<maml:para>
Opens the transmission web UI in the default system browser.
</maml:para>
</maml:description>
<command:examples>
<command:example>
<maml:title>Example 1: Open web UI</maml:title>
<maml:Introduction>
<maml:paragraph>C:\PS></maml:paragraph>
</maml:Introduction>
<dev:code>Invoke-TransmissionWeb</dev:code>
<dev:remarks>
<maml:para>
Transmission web UI is opened in the system default browser.
</maml:para>
</dev:remarks>
</command:example>
</command:examples>
<maml:relatedLinks>
<maml:navigationLink>
<maml:linkText>https://github.com/trossr32/ps-transmission-manager</maml:linkText>
</maml:navigationLink>
</maml:relatedLinks>
</command:command>
<command:command
xmlns:maml="http://schemas.microsoft.com/maml/2004/10"
xmlns:command="http://schemas.microsoft.com/maml/dev/command/2004/10"
Expand Down
3 changes: 2 additions & 1 deletion src/PsTransmission/Transmission.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
Description = 'A Powershell module that integrates with the Transmission RPC API.'

# Minimum version of the Windows PowerShell engine required by this module
PowerShellVersion = '7'
PowerShellVersion = '7.0'

# Name of the Windows PowerShell host required by this module
# PowerShellHostName = ''
Expand Down Expand Up @@ -80,6 +80,7 @@
'Get-TransmissionSessionStatistics',
'Test-TransmissionPort',
'Update-TransmissionBlockLists',
'Invoke-TransmissionWeb',
'Add-TransmissionTorrents',
'Assert-TransmissionTorrentsVerified',
'Get-TransmissionTorrents',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Flurl.Http" Version="2.4.2" />
<PackageReference Include="Flurl.Http" Version="3.0.0-pre4" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

Expand Down
4 changes: 2 additions & 2 deletions src/Transmission.NetCore.Client/TransmissionClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -540,10 +540,10 @@ private async Task<TransmissionResponse> SendRequestAsync(TransmissionRequest re
}
catch (FlurlHttpException e)
{
if (e.Call.Response.StatusCode != HttpStatusCode.Conflict)
if (e.Call.Response.StatusCode != (int)HttpStatusCode.Conflict)
throw;

SessionId = e.Call.Response.GetHeaderValue("X-Transmission-Session-Id");
SessionId = e.Call.Response.Headers.FirstOrDefault("X-Transmission-Session-Id");

if (SessionId == null)
throw new Exception("Session id error");
Expand Down

0 comments on commit 803f90b

Please sign in to comment.