Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Converts a 1Password URL export to a RequestPolicy import file
#!/usr/bin/env ruby
require 'optparse'
require 'uri'
options = {}
opt_parser = OptionParser.new do |opt|
opt.banner = "1Password to RequestPolicy Converter"
opt.on('-i', '--in INPUT', "1password export of URLs") do |input|
options[:input] = input
end
opt.on('-o', '--out OUTPUT', "RequestPolicy style 'ini' file") do |output|
options[:output] = output
end
opt.on('-h', '--help', 'help') do
puts opt_parser
end
end
begin
opt_parser.parse!
mandatory = [:input, :output]
missing = mandatory.select{ |param| options[param].nil? }
if not missing.empty?
puts "Missing options: #{missing.join(', ')}"
puts opt_parser
exit
end
rescue OptionParser::InvalidOption, OptionParser::MissingArgument
puts $!.to_s
puts opt_parser
exit
end
puts "would convert #{options[:input]} -> #{options[:output]}"
parser = URI::Parser.new
open( options[:output], 'w' ) do |file|
file.puts "[origins]"
IO.foreach( options[:input] ) do |line|
next if line.nil?
next if line.chomp.length == 0
line.chomp!
begin
u = parser.parse line
file.puts u.host unless u.host.nil?
rescue URI::InvalidURIError
puts "'#{line}' is not a valid URI"
end
end
file.puts "[destinations]"
file.puts "[origins-to-destinations]"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment