module GClouder::GCloud

Public Class Methods

included(klass) click to toggle source
# File lib/gclouder/gcloud.rb, line 11
def self.included(klass)
  klass.extend GCloud
end

Public Instance Methods

gcloud(command, force: false, failure: true, silent: false, project_id: nil, config_relative: false) click to toggle source
# File lib/gclouder/gcloud.rb, line 15
def gcloud(command, force: false, failure: true, silent: false, project_id: nil, config_relative: false)
  project_id = verify(project_id)

  GClouder::Project::ID.switch(project_id)

  if cli_args[:dry_run] && !force
    debug "# gcloud --quiet --format json --project=#{project_id} #{command}" if cli_args[:debug]
    GClouder::Project::ID.reset
    return
  end

  if config_relative
    Dir.chdir(File.dirname(GClouder::Config::CLIArgs.cli_args[:config])) do
      result = shell("gcloud --quiet --format json --project=#{project_id} #{command}", failure: failure, silent: silent)
    end
  else
      result = shell("gcloud --quiet --format json --project=#{project_id} #{command}", failure: failure, silent: silent)
  end

  GClouder::Project::ID.reset

  valid_json?(result) ? JSON.parse(result.to_s) : result
end
verify(project_id) click to toggle source
# File lib/gclouder/gcloud.rb, line 39
def verify(project_id)
  project_id ||= project["project_id"]
  return project_id if project_id
  raise StandardError, "project_id not detected"
end