forked from stevendanna/knife-hacks
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add knife api plugin for making api requests from the command line
- Loading branch information
1 parent
8a242ed
commit e41bd85
Showing
1 changed file
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |