,bw (1076B)
1 #!/usr/bin/env ruby 2 require 'json' 3 require 'open3' 4 abort 'bitwarden-cli (bw) required but not found.' unless system("command -v bw >/dev/null 2>&1") 5 out = `bw list items` 6 abort "User cancelled" if out.chomp.empty? 7 items = JSON.load out 8 query_str = items.map do |r| 9 s = [r['name']] 10 s << " (#{r['login']['username']})" if r.has_key? 'login' 11 s << "\t" + r['id'] 12 s.join 13 end.join("\n") 14 out = Open3.popen2("fzf -d '\t' --with-nth 1") do |i,o| 15 i.puts query_str 16 i.close 17 o.gets.chomp 18 end 19 abort "User cancelled" if out.empty? 20 _, chosen_id = out.split("\t") 21 chosen_item = items.find { |x| x['id'] == chosen_id } 22 list = [] 23 list += chosen_item['fields'].reduce([]) { |acc, x| acc << [x['name'],x['value']] } if chosen_item.has_key? 'fields' 24 list += chosen_item['login'].slice('username', 'password').to_a 25 query_str = list.map.with_index { |r, i| "#{r[0]}\t#{i}" }.join("\n") 26 out = Open3.popen2("fzf -d '\t' --with-nth 1") do |i,o| 27 i.puts query_str 28 i.close 29 o.gets.chomp 30 end 31 abort "User cancelled" if out.empty? 32 _, chosen_i = out.split("\t") 33 print list[chosen_i.to_i][1]