#!/usr/bin/env ruby

require 'heroics'
require 'netrc'
require 'moneta'

netrc = Netrc.read
username, token = netrc['api.heroku.com']
if username && token
  username = username.split('@').first
  options = {
    default_headers: {'Accept' => 'application/vnd.heroku+json; version=3'},
    cache: Moneta.new(:File, dir: "#{Dir.home}/.heroics/heroku-api")}

  schema_url = "https://#{username}:#{token}@api.heroku.com/schema"
  schema = Heroics.download_schema(schema_url, options)

  url = "https://#{username}:#{token}@heroku-api.heroku.com/prefix"
  cli = Heroics.cli_from_schema('heroku-api', STDOUT, schema, url, options)
  cli.run(*ARGV)
else
  puts "ERROR Couldn't find credentials for api.heroku.com in ~/.netrc."
  puts "      Login with the Heroku Toolbelt by running 'heroku login'."
  1
end
