robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1-- Converted into Lua from https://github.com/cyjake/ssh-config 2-- TODO (siddhantdev): deal with include directives 3 4local M ={} 5 6local whitespace_pattern ='%s' 7local line_break_pattern ='[\r\n]' 8 9---@param param string 10local function is_multi_value_directive ( param ) 11local multi_value_directives ={ 12'globalknownhostsfile' , 13'host' , 14'ipqos' , 15'sendenv' , 16'userknownhostsfile' , 17'proxycommand' , 18'match' , 19'canonicaldomains' , 20} 21 22return vim . list_contains ( multi_value_directives , param : lower ()) 23end 24 25---@param text string The ssh configuration which needs to be parsed 26---@return string[] The parsed host names in the configuration 27function M . parse_ssh_config ( text ) 28local i =1 29local line =1 30 31local function consume () 32if i <= # text then 33local char =text : sub ( i , i ) 34i =i + 1 35return char 36end 37return nil 38end 39 40local chr =consume () 41 42local function parse_spaces () 43local spaces ='' 44while chr and chr : match ( whitespace_pattern ) do 45spaces =spaces .. chr 46chr =consume () 47end 48return spaces 49end 50 51local function parse_linebreaks () 52local breaks ='' 53while chr and chr : match ( line_break_pattern ) do 54line =line + 1 55breaks =breaks .. chr 56chr =consume () 57end 58return breaks 59end 60 61local function parse_parameter_name () 62local param ='' 63while chr and not chr : match ( '[ \t=]' ) do 64param =param .. chr 65chr =consume () 66end 67return param 68end 69 70local function parse_separator () 71local sep =parse_spaces () 72if chr == '=' then 73sep =sep .. chr 74chr =consume () 75end 76return sep .. parse_spaces () 77end 78 79local function parse_value () 80local val ={} 81local quoted , escaped =false , false 82 83while chr and not chr : match ( line_break_pattern ) do 84if escaped then 85table . insert ( val , chr == '"' and chr or '\\' .. chr ) 86escaped =false 87elseif chr == '"' and ( # val == 0 or quoted ) then 88quoted =not quoted 89elseif chr == '\\' then 90escaped =true 91elseif chr == '#' and not quoted then 92break 93else 94table . insert ( val , chr ) 95end 96chr =consume () 97end 98 99if quoted or escaped then 100error ( 'Unexpected line break at line ' .. line ) 101end 102 103return vim . trim ( table . concat ( val )) 104end 105 106local function parse_comment () 107while chr and not chr : match ( line_break_pattern ) do 108chr =consume () 109end 110end 111 112---@return string[] 113local function parse_multiple_values () 114local results ={} 115local val ={} 116local quoted =false 117local escaped =false 118 119while chr and not chr : match ( line_break_pattern ) do 120if escaped then 121table . insert ( val , chr == '"' and chr or '\\' .. chr ) 122escaped =false 123elseif chr == '"' then 124quoted =not quoted 125elseif chr == '\\' then 126escaped =true 127elseif quoted then 128table . insert ( val , chr ) 129elseif chr : match ( '[ \t=]' ) then 130if # val > 0 then 131table . insert ( results , vim . trim ( table . concat ( val ))) 132val ={} 133end 134elseif chr == '#' and # results > 0 then 135break 136else 137table . insert ( val , chr ) 138end 139chr =consume () 140end 141 142if quoted or escaped then 143error ( 'Unexpected line break at line ' .. line ) 144end 145 146if # val > 0 then 147table . insert ( results , vim . trim ( table . concat ( val ))) 148end 149 150return results 151end 152 153local function parse_directive () 154local param =parse_parameter_name () 155local multiple =is_multi_value_directive ( param ) 156local _ =parse_separator () 157local value =multiple and parse_multiple_values () or parse_value () 158 159local result ={ 160param =param , 161value =value , 162} 163 164return result 165end 166 167local function parse_line () 168local _ =parse_spaces () 169if chr == '#' then 170parse_comment () 171return nil 172end 173local node =parse_directive () 174local _ =parse_linebreaks () 175 176return node 177end 178 179local hostnames ={} 180 181---@param value string 182local function is_valid ( value ) 183return not ( value : find ( '[?*!]' ) or vim . list_contains ( hostnames , value )) 184end 185 186while chr do 187local node =parse_line () 188if node then 189-- This is done just to assign the type 190node . value =node . value ---@type string[] 191if node . param : lower () == 'match' and node . value then 192local current =nil 193for ind , val in ipairs ( node . value ) do 194if 195val : lower () == 'host' 196and ind + 1 <= # node . value 197and is_valid ( assert ( node . value [ ind + 1 ])) 198then 199current =node . value [ ind + 1 ] 200end 201end 202if current then 203table . insert ( hostnames , current ) 204end 205elseif node . param : lower () == 'host' and node . value then 206for _ , value in ipairs ( node . value ) do 207if is_valid ( value ) then 208table . insert ( hostnames , value ) 209end 210end 211end 212end 213end 214 215return hostnames 216end 217 218---@param filename string 219---@return string[] The hostnames configured in the file located at filename 220function M . parse_config ( filename ) 221local file =io . open ( filename , 'r' ) 222if not file then 223error ( 'Cannot read ssh configuration file' ) 224end 225local config_string =file : read ( '*a' ) 226file : close () 227 228return M . parse_ssh_config ( config_string ) 229end 230 231---@return string[] The hostnames configured in the ssh configuration file 232--- located at "~/.ssh/config". 233--- Note: This does not currently process `Include` directives in the 234--- configuration file. 235function M . get_hosts () 236local config_path =vim . fs . normalize ( '~/.ssh/config' ) ---@type string 237 238return M . parse_config ( config_path ) 239end 240 241return M