Module: GClouder::GCloud

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Config::CLIArgs

check, #cli_args, cli_args, load, valid_resources

Methods included from Config::Project

load, #project, project

Methods included from Helpers

#hash_to_args, #module_exists?, #to_arg, #to_deep_merge_hash, #valid_json?

Methods included from Logging

#add, #bad, #change, #debug, #error, #fatal, #good, #info, log, loggers, #remove, report, #resource_state, setup, #warn, #warning

Methods included from Shell

#shell

Class Method Details

.included(klass) ⇒ Object



11
12
13
# File 'lib/gclouder/gcloud.rb', line 11

def self.included(klass)
  klass.extend GCloud
end

Instance Method Details

#gcloud(command, force: false, failure: true, silent: false, project_id: nil, config_relative: false) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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) ⇒ Object

Raises:

  • (StandardError)


39
40
41
42
43
# 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