Skip to content

Instantly share code, notes, and snippets.

@smashwilson
Created February 13, 2015 14:12
Show Gist options
  • Save smashwilson/aa1c19c0c4d679883f8a to your computer and use it in GitHub Desktop.
Save smashwilson/aa1c19c0c4d679883f8a to your computer and use it in GitHub Desktop.
Automatically convert Perigee calls to client.Request calls.
#!/bin/env ruby
require 'find'
def convert(path)
content = File.read(path)
ncontent = []
line_no = 1
in_req = false
had_req = false
client_name = "client"
content.split(/\n/).each do |line|
skip = false
case
when in_req
case line
when /MoreHeaders: c(lient)?.AuthenticatedHeaders\(\)/
skip = true
when /ReqBody:/
line.gsub! /ReqBody:/, "JSONBody:"
when /Results:/
line.gsub! /Results:/, "JSONResponse:"
when /OkCodes:/
# leave as-is
when /^\s*}\)\s*$/
in_req = false
else
puts
puts "Oh snap! I hit an option I don't understand."
puts "#{path}:#{line_no}"
puts line
puts
end
when !in_req && line =~ /func \w+\((\w+) \*gophercloud\.ServiceClient/
client_name = $1
when !in_req && line =~ /perigee.Request\(.*perigee.Options{\s*$/
line.gsub! /perigee\.Request/, "#{client_name}.Request"
line.gsub! /perigee\.Options/, "gophercloud.RequestOpts"
in_req = true
had_req = true
end
ncontent << line unless skip
line_no += 1
end
if had_req
File.write(path, ncontent.join("\n"))
system "goimports -w -- '#{path}'"
puts "Completed: [#{path}]"
end
end
Find.find(*ARGV) do |path|
next unless path =~ /\.go$/
convert(path)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment