hammerPass.lua
1 local HammerPass = {} 2 HammerPass.__index = HammerPass 3 4 -- Metadata 5 HammerPass.name = 'HammerPass' 6 HammerPass.version = '1.1' 7 HammerPass.author = 'Felipe Silveira' 8 HammerPass.homepage = 'https://github.com/silveiralexf/.dotfiles/blob/master/hammerspoon/extensions/HammerPass.lua' 9 HammerPass.license = 'https://github.com/silveiralexf/.dotfiles/blob/master/LICENSE' 10 11 -- default password store directory 12 HammerPass.StoreBaseDir = '~/.password-store' 13 14 function StrEndsWith(str, ending) 15 if str:len() < ending:len() then 16 return false 17 else 18 return str:sub(str:len() - ending:len() + 1, str:len()) == ending 19 end 20 end 21 22 function ListPasswordFiles(dir, files) 23 Ending = '.gpg' 24 local iter_fn, dir_obj = hs.fs.dir(dir) 25 while true do 26 local file = iter_fn(dir_obj) 27 if file == nil then 28 break 29 end 30 if file:sub(1, 1) ~= '.' then 31 if hs.fs.attributes(dir .. '/' .. file)['mode'] == 'directory' then 32 ListPasswordFiles(dir .. '/' .. file, files) 33 else 34 if StrEndsWith(file, Ending) then 35 table.insert(files, { file:sub(1, file:len() - Ending:len()), dir }) 36 end 37 end 38 end 39 end 40 return files 41 end 42 43 function CopyPassword(r) 44 if r ~= nil then 45 Output, Status, type, Rc = hs.execute('pass ' .. r['text'] .. '/' .. r['subText'], true) 46 if Status then 47 CleanOutput = string.gsub(Output, '\n', '') 48 hs.pasteboard.setContents(CleanOutput) 49 end 50 end 51 end 52 53 function ChoosePassword() 54 local c = hs.chooser.new(CopyPassword) 55 local files = {} 56 ListPasswordFiles(HammerPass.StoreBaseDir, files) 57 local choices = {} 58 for _, entry in ipairs(files) do 59 local file, dir = entry[1], entry[2] 60 local dir_name = dir:sub(HammerPass.StoreBaseDir:len() + 2, dir:len()) 61 table.insert(choices, { ['text'] = dir_name, ['subText'] = file, ['uuid'] = file }) 62 end 63 c:choices(choices) 64 c:show() 65 end 66 67 return HammerPass