Last active
April 1, 2020 00:17
-
-
Save wtnabe/37dd89a0ba89c7d6ac6c334c3809fc16 to your computer and use it in GitHub Desktop.
List up functions with filter
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
#! /usr/bin/env ruby | |
require 'json' | |
# | |
# [param] Proc filter | |
# | |
def main(&filter) | |
puts projects.select { |project| | |
functionEnabled?(services(project['projectId'])) | |
}.map { |project| | |
project_id = project['projectId'] | |
functions(project_id).map { |f| | |
[project_id, f['name']].join("\t") if filter.call(f) | |
} | |
}.flatten | |
end | |
# | |
# [return] Array | |
# | |
def projects | |
JSON.parse(`gcloud projects list --format=json`).select { |project| | |
project['projectId'] !~ /^sys-/ | |
} | |
end | |
# | |
# [param] String project | |
# [return] Hash | |
# | |
def services(project) | |
JSON.parse(`gcloud services list --format=json --project #{project}`) | |
end | |
# | |
# [param] Array services | |
# [return] Boolean | |
# | |
def functionEnabled?(services) | |
services.any? { |service| | |
service.dig('config', 'name') =~ /cloudfunctions/ | |
} | |
end | |
# | |
# [param] String project | |
# [return] Array | |
# | |
def functions(project) | |
JSON.parse(`gcloud functions list --format=json --project #{project}`) | |
end | |
__END__ | |
Usage: | |
puts main { |func| func['runtime'] != 'nodejs10' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment