Skip to content

Commit

Permalink
Add knife api plugin for making api requests from the command line
Browse files Browse the repository at this point in the history
  • Loading branch information
stevendanna committed Mar 24, 2012
1 parent 8a242ed commit e41bd85
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions plugins/api.rb
@@ -0,0 +1,40 @@
class Api < Chef::Knife
banner "knife api METHOD PATH [REQUEST BODY] (options)"

option :raw,
:long => "--raw",
:short => "-R",
:description => "Show raw response. Only applies to GET",
:default => false

def run
method, path, request = @name_args
if !method || !path
show_usage
ui.fatal "You must specify a method and a path"
exit 1
end

response = case method.downcase.to_sym
when :get
if config[:raw]
::File.read rest.get_rest path, true
else
rest.get_rest path
end
when :delete
ui.confirm "DELETE is a destructive method. Do you want to call DELETE on #{path}"
rest.delete_rest path
when :post
raise ArgumentError, "Request Body Required" unless request
# HACK: Use eval to turn string into a Hash
rest.post_rest path, eval(request)
when :put
raise ArgumentError, "Request Body Required" unless request
rest.put_rest path, eval(request)
else
raise ArgumentError, "Unknown method #{method}"
end
ui.output(ui.format_for_display response)
end
end

0 comments on commit e41bd85

Please sign in to comment.