Created
September 2, 2014 03:51
-
-
Save marshallmick007/e307b877f0ff0de5520f to your computer and use it in GitHub Desktop.
Converts a 1Password URL export to a RequestPolicy import file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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