class Chef::Knife::Raw

Public Instance Methods

run() click to toggle source
# File lib/chef/knife/raw.rb, line 48
def run
  if name_args.length == 0
    show_usage
    ui.fatal("You must provide the path you want to hit on the server")
    exit(1)
  elsif name_args.length > 1
    show_usage
    ui.fatal("Only one path accepted for knife raw")
    exit(1)
  end

  path = name_args[0]
  data = false
  if config[:input]
    data = IO.read(config[:input])
  end
  begin
    method = config[:method].to_sym

    if config[:pretty]
      chef_rest = RawInputServerAPI.new
      result = chef_rest.request(method, name_args[0], {'Content-Type' => 'application/json'}, data)
      unless result.is_a?(String)
        result = Chef::JSONCompat.to_json_pretty(result)
      end
    else
      chef_rest = RawInputServerAPI.new(:raw_output => true)
      result = chef_rest.request(method, name_args[0], {'Content-Type' => 'application/json'}, data)
    end
    output result
  rescue Timeout::Error => e
    ui.error "Server timeout"
    exit 1
  rescue Net::HTTPServerException => e
    ui.error "Server responded with error #{e.response.code} \"#{e.response.message}\""
    ui.error "Error Body: #{e.response.body}" if e.response.body && e.response.body != ''
    exit 1
  end
end