sshUtils.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
$ssh_block = {
      param($commandName, $wordToComplete)
      $pattern = 'Host '+$commandName;
      Get-Content ~/.ssh/config | Select-String -Pattern $pattern | ForEach-object { $_.ToString().Substring(5) } | Sort-Object
}

$npm_run_block = {
      param($wordToComplete,$ast,$cursorPosition)
      if($ast.ToString().contains('npm run')) {
            $output = Get-Content ./package.json | ConvertFrom-Json | Select -expand scripts | Get-Member -MemberType NoteProperty | ForEach-object {$_.Name} | Sort-Object
      }elseif(($ast  -replace " $wordToComplete$",'').ToString() -eq 'npm') {
            $output = @('run','ci','install','update','audit', 'cache', 'clean-install', 
                  'ls', 'publish', 'rebuild', 'version'
            )
      }

      return $output|where{$_.contains($wordToComplete) }
}

function Mount-ssh{
      param($profile,$letter="X")
      $env:PATH = "C:\Program Files\SSHFS-Win\bin;"+$env:PATH
      $source = $profile+":/"
      $cmd = "C:\Program Files\SSHFS-Win\bin\sshfs.exe"
      $args = "-f `"$source`"", "`"${letter}:`"","-o rw,fsname=sshfs@`"$source`",subtype=sshfs,idmap=user,StrictHostKeyChecking=no,volname=`"$profile`:`""
      Start-Process $cmd $args
}

# sshfs-win cmd volDev:/ Y: -o idmap=user,StrictHostKeyChecking=no,UseNetworkDrive=yes

Register-ArgumentCompleter -Native -CommandName ssh -ScriptBlock $ssh_block
Register-ArgumentCompleter -Native -CommandName Mount-ssh -ScriptBlock $ssh_block
Register-ArgumentCompleter -Native -CommandName scp -ScriptBlock $ssh_block
Register-ArgumentCompleter -Native -CommandName npm -ScriptBlock $npm_run_block


Export-ModuleMember -Function Mount-ssh