Skip to content

Commit

Permalink
Update installer tests for posh-ssh
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremySkinner committed Jun 14, 2018
1 parent 90573f3 commit 1b157e9
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 48 deletions.
3 changes: 3 additions & 0 deletions posh-ssh/src/Installer.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Allows for overriding of module path during test.
$ModuleBasePath = $PSScriptRoot

<#
.SYNOPSIS
Configures your PowerShell profile (startup) script to import the posh-ssh
Expand Down
69 changes: 69 additions & 0 deletions posh-ssh/src/Keys.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
function Get-SshPath($File = 'id_rsa') {
# Avoid paths with path separator char since it is different on Linux/macOS.
# Also avoid ~ as it is invalid if the user is cd'd into say cert:\ or hklm:\.
# Also, apparently using the PowerShell built-in $HOME variable may not cut it for msysGit with has different
# ideas about the path to the user's home dir e.g. /c/Users/Keith
# $homePath = Invoke-NullCoalescing $Env:HOME $Home
$homePath = if ($Env:HOME) {$Env:HOME} else {$Home}
Join-Path $homePath (Join-Path .ssh $File)
}

<#
.SYNOPSIS
Add a key to the SSH agent
.DESCRIPTION
Adds one or more SSH keys to the SSH agent.
.EXAMPLE
PS C:\> Add-SshKey
Adds ~\.ssh\id_rsa to the SSH agent.
.EXAMPLE
PS C:\> Add-SshKey ~\.ssh\mykey, ~\.ssh\myotherkey
Adds ~\.ssh\mykey and ~\.ssh\myotherkey to the SSH agent.
.INPUTS
None.
You cannot pipe input to this cmdlet.
#>
function Add-SshKey([switch]$Quiet) {
if ($env:GIT_SSH -imatch 'plink') {
$pageant = Get-Command pageant -Erroraction SilentlyContinue | Select-Object -First 1 -ExpandProperty Name
$pageant = if ($pageant) { $pageant } else { Find-Pageant }
if (!$pageant) {
if (!$Quiet) {
Write-Warning 'Could not find Pageant'
}
return
}

if ($args.Count -eq 0) {
$keyPath = Join-Path $Env:HOME .ssh
$keys = Get-ChildItem $keyPath/*.ppk -ErrorAction SilentlyContinue | Select-Object -ExpandProperty FullName
if ($keys) {
& $pageant $keys
}
}
else {
foreach ($value in $args) {
& $pageant $value
}
}
}
else {
$sshAdd = Get-Command ssh-add -TotalCount 1 -ErrorAction SilentlyContinue
$sshAdd = if ($sshAdd) { $sshAdd } else { Find-Ssh('ssh-add') }
if (!$sshAdd) {
if (!$Quiet) {
Write-Warning 'Could not find ssh-add'
}
return
}

if ($args.Count -eq 0) {
& $sshAdd
}
else {
foreach ($value in $args) {
& $sshAdd $value
}
}
}
}
3 changes: 2 additions & 1 deletion posh-ssh/src/Posh-Ssh.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
'Start-SshAgent',
'Stop-SshAgent',
'Add-SshKey',
'Get-SshPath'
'Get-SshPath',
'Add-PoshSshToProfile'
)

# Cmdlets to export from this module
Expand Down
89 changes: 89 additions & 0 deletions posh-ssh/src/Utils.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$ModuleBasePath = "$PSScriptRoot\.."

function setenv($key, $value) {
[void][Environment]::SetEnvironmentVariable($key, $value)
Expand Down Expand Up @@ -55,4 +56,92 @@ function Test-PoshSshImportedInScript {
$match = (@(Get-Content $Path -ErrorAction SilentlyContinue) -match 'posh-ssh').Count -gt 0
if ($match) { Write-Verbose "posh-ssh found in '$Path'" }
$match
}

function Get-PSModulePath {
$modulePaths = $Env:PSModulePath -split ';'
$modulePaths
}

function Test-InPSModulePath {
param (
[Parameter(Position=0, Mandatory=$true)]
[ValidateNotNull()]
[string]
$Path
)

$modulePaths = Get-PSModulePath
if (!$modulePaths) { return $false }

$pathStringComparison = Get-PathStringComparison
$Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)
$inModulePath = @($modulePaths | Where-Object { $Path.StartsWith($_.TrimEnd([System.IO.Path]::DirectorySeparatorChar), $pathStringComparison) }).Count -gt 0

if ($inModulePath -and ('src' -eq (Split-Path $Path -Leaf))) {
Write-Warning 'posh-git repository structure is incompatible with %PSModulePath%.'
Write-Warning 'Importing with absolute path instead.'
return $false
}

$inModulePath
}

<#
.SYNOPSIS
Gets the file encoding of the specified file.
.DESCRIPTION
Gets the file encoding of the specified file.
.PARAMETER Path
Path to the file to check. The file must exist.
.EXAMPLE
PS C:\> Get-FileEncoding $profile
Get's the file encoding of the profile file.
.INPUTS
None.
.OUTPUTS
[System.String]
.NOTES
Adapted from http://www.west-wind.com/Weblog/posts/197245.aspx
#>
function Get-FileEncoding($Path) {
if ($PSVersionTable.PSVersion.Major -ge 6) {
$bytes = [byte[]](Get-Content $Path -AsByteStream -ReadCount 4 -TotalCount 4)
}
else {
$bytes = [byte[]](Get-Content $Path -Encoding byte -ReadCount 4 -TotalCount 4)
}

if (!$bytes) { return 'utf8' }

switch -regex ('{0:x2}{1:x2}{2:x2}{3:x2}' -f $bytes[0],$bytes[1],$bytes[2],$bytes[3]) {
'^efbbbf' { return 'utf8' }
'^2b2f76' { return 'utf7' }
'^fffe' { return 'unicode' }
'^feff' { return 'bigendianunicode' }
'^0000feff' { return 'utf32' }
default { return 'ascii' }
}
}

<#
.SYNOPSIS
Gets a StringComparison enum value appropriate for comparing paths on the OS platform.
.DESCRIPTION
Gets a StringComparison enum value appropriate for comparing paths on the OS platform.
.EXAMPLE
PS C:\> $pathStringComparison = Get-PathStringComparison
.INPUTS
None
.OUTPUTS
[System.StringComparison]
#>
function Get-PathStringComparison {
# File system paths are case-sensitive on Linux and case-insensitive on Windows and macOS
if (($PSVersionTable.PSVersion.Major -ge 6) -and $IsLinux) {
[System.StringComparison]::Ordinal
}
else {
[System.StringComparison]::OrdinalIgnoreCase
}
}
Loading

0 comments on commit 1b157e9

Please sign in to comment.