-
Notifications
You must be signed in to change notification settings - Fork 588
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2652 from FoothillSolutions/port-ssh-module
Porting SSH module to FAKE 5
- Loading branch information
Showing
11 changed files
with
179 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Auto-Generated by FAKE; do not edit | ||
namespace System | ||
open System.Reflection | ||
|
||
[<assembly: AssemblyTitleAttribute("FAKE - F# Make SSH operations")>] | ||
[<assembly: AssemblyProductAttribute("FAKE - F# Make")>] | ||
[<assembly: AssemblyVersionAttribute("5.21.1")>] | ||
[<assembly: AssemblyInformationalVersionAttribute("5.21.1")>] | ||
[<assembly: AssemblyFileVersionAttribute("5.21.1")>] | ||
[<assembly: AssemblyMetadataAttribute("BuildDate","2022-01-31")>] | ||
do () | ||
|
||
module internal AssemblyVersionInformation = | ||
let [<Literal>] AssemblyTitle = "FAKE - F# Make SSH operations" | ||
let [<Literal>] AssemblyProduct = "FAKE - F# Make" | ||
let [<Literal>] AssemblyVersion = "5.21.1" | ||
let [<Literal>] AssemblyInformationalVersion = "5.21.1" | ||
let [<Literal>] AssemblyFileVersion = "5.21.1" | ||
let [<Literal>] AssemblyMetadata_BuildDate = "2022-01-31" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<TargetFrameworks>netstandard2.0;net472</TargetFrameworks> | ||
<AssemblyName>Fake.Net.SSH</AssemblyName> | ||
<OutputType>Library</OutputType> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<DefineConstants>$(DefineConstants)</DefineConstants> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' "> | ||
<DefineConstants>$(DefineConstants);RELEASE</DefineConstants> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Compile Include="AssemblyInfo.fs" /> | ||
<Compile Include="SSH.fs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\Fake.Core.Environment\Fake.Core.Environment.fsproj" /> | ||
<ProjectReference Include="..\Fake.Core.Process\Fake.Core.Process.fsproj" /> | ||
<ProjectReference Include="..\Fake.Core.Trace\Fake.Core.Trace.fsproj" /> | ||
</ItemGroup> | ||
<Import Project="..\..\..\.paket\Paket.Restore.targets" /> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
namespace Fake.Net | ||
|
||
open System | ||
open Fake.Core | ||
|
||
[<RequireQualifiedAccess>] | ||
/// Contains a task which allows to perform SSH operations | ||
module SSH = | ||
|
||
/// The SSH parameter type. | ||
type SSHParams = | ||
{ /// Path of the scp.exe | ||
ToolPath : string | ||
/// Path of the private key file (optional) | ||
PrivateKeyPath : string | ||
/// remote User | ||
RemoteUser : string | ||
RemoteHost : string | ||
RemotePort : string | ||
} | ||
|
||
/// The SSH default parameters | ||
let SSHDefaults : SSHParams = | ||
{ ToolPath = if Environment.isMono then "ssh" else "ssh.exe" | ||
RemoteUser = "fake" | ||
RemoteHost = "localhost" | ||
RemotePort = "22" | ||
PrivateKeyPath = null | ||
} | ||
|
||
let private getTarget sshParams = | ||
match sshParams.RemotePort with | ||
| "22" -> $"%s{sshParams.RemoteUser}@%s{sshParams.RemoteHost}" | ||
| _ -> $"%s{sshParams.RemoteUser}@%s{sshParams.RemoteHost}:%s{sshParams.RemotePort}" | ||
|
||
let private getPrivateKey privateKeyPath = | ||
if String.IsNullOrEmpty privateKeyPath then "" else $"-i \"%s{privateKeyPath}\"" | ||
|
||
let buildArguments sshParams command = | ||
let target = sshParams |> getTarget | ||
let privateKey = sshParams.PrivateKeyPath |> getPrivateKey | ||
$"%s{privateKey} %s{target} %s{Args.toWindowsCommandLine [command]}" |> String.trim | ||
|
||
/// Performs a command via SSH. | ||
/// ## Parameters | ||
/// | ||
/// - `setParams` - Function used to manipulate the default SSHParams value. | ||
/// - `command` - The target path. Can be something like user@host:directory/TargetFile or a local path. | ||
/// | ||
/// ## Sample | ||
/// | ||
/// SSH (fun p -> { p with ToolPath = "tools/ssh.exe" }) command | ||
let SSH setParams command = | ||
let (sshParams : SSHParams) = setParams SSHDefaults | ||
let target = sshParams |> getTarget | ||
let args = buildArguments sshParams command | ||
|
||
Trace.tracefn $"%s{sshParams.ToolPath} %s{args}" | ||
|
||
let result = CreateProcess.fromRawCommandLine sshParams.ToolPath args | ||
|> CreateProcess.withTimeout(TimeSpan.MaxValue) | ||
|> Proc.run | ||
if result.ExitCode <> 0 then failwithf $"Error during SSH. Target: %s{target} Command: %s{command}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
group netcore | ||
|
||
FSharp.Core | ||
NETStandard.Library |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module Fake.Net.SSHTests | ||
|
||
open Expecto | ||
open Fake.Net | ||
|
||
[<Tests>] | ||
let tests = | ||
testList "Fake.Net.SSH.Tests" [ | ||
testCase "Test all arguments are mapped correctly" <| fun _ -> | ||
let args: SSH.SSHParams = | ||
{ ToolPath = "ssh" | ||
RemoteUser = "fake-user" | ||
RemoteHost = "localhost" | ||
RemotePort = "22" | ||
PrivateKeyPath = "private-key-path" } | ||
let sshCommand = "pwd" | ||
let cmd = SSH.buildArguments args sshCommand | ||
|
||
Expect.equal cmd "-i \"private-key-path\" fake-user@localhost pwd" "expected proper arguments formatting" | ||
|
||
testCase "Test ssh target is mapped correctly when a custom port is used" <| fun _ -> | ||
let args: SSH.SSHParams = | ||
{ ToolPath = "ssh" | ||
RemoteUser = "fake-user" | ||
RemoteHost = "localhost" | ||
RemotePort = "2222" | ||
PrivateKeyPath = null } | ||
let sshCommand = "pwd" | ||
let cmd = SSH.buildArguments args sshCommand | ||
|
||
Expect.equal cmd "fake-user@localhost:2222 pwd" "expected proper arguments formatting" | ||
|
||
testCase "Test PrivateKeyPath is mapped correctly when it's empty" <| fun _ -> | ||
let args: SSH.SSHParams = | ||
{ ToolPath = "ssh" | ||
RemoteUser = "fake-user" | ||
RemoteHost = "localhost" | ||
RemotePort = "22" | ||
PrivateKeyPath = null } | ||
let sshCommand = "pwd" | ||
let cmd = SSH.buildArguments args sshCommand | ||
|
||
Expect.equal cmd "fake-user@localhost pwd" "expected proper arguments formatting" | ||
] |