Skip to content

Instantly share code, notes, and snippets.

@suhlig
Created June 29, 2019 12:10
Show Gist options
  • Save suhlig/c295e3980f6e8c5d72e0870247517823 to your computer and use it in GitHub Desktop.
Save suhlig/c295e3980f6e8c5d72e0870247517823 to your computer and use it in GitHub Desktop.
List all cf apps across all orgs and spaces
#!/usr/bin/env ruby
require 'json'
require 'shellwords'
HIERARCHY = %w(/ org space app)
def curl(url)
while url
JSON.parse(`cf curl #{Shellwords.escape(url)}`).tap do |result|
raise "Error #{result['error_code']}: #{result['description']}" if result['error_code']
yield result # streaming: handle each page as we get it
url = result['next_url']
end
end
end
def indent(level)
' ' * level
end
def dump(entity, level = 0)
type = HIERARCHY[level]
puts "#{indent(level)} #{type} #{entity['name']} #{entity['state']}"
child_type = HIERARCHY[level + 1]
curl(entity["#{child_type}s_url"]) do |entities|
# warn "#{indent(level + 1)} #{entities['total_results']} #{child_type}s"
entities['resources'].each do |child|
dump(child['entity'], level + 1)
end
end
end
dump('orgs_url' => '/v2/organizations')
__END__
TODO:
--skip-org
--skip-space
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment