module Google::Serverless::Exec::Gcloud

A collection of utility functions and classes for interacting with an installation of the gcloud SDK.

Public Class Methods

binary_path() click to toggle source

@private Returns the path to the gcloud binary, or nil if the binary could not be found.

@return [String,nil] Path to the gcloud binary.

# File lib/google/serverless/exec/gcloud.rb, line 79
def binary_path
  unless defined? @binary_path
    @binary_path =
      if ::Gem.win_platform?
        `where gcloud` == "" ? nil : "gcloud"
      else
        path = `which gcloud`.strip
        path.empty? ? nil : path
      end
  end
  @binary_path
end
binary_path!() click to toggle source

@private Returns the path to the gcloud binary. Raises BinaryNotFound if the binary could not be found.

@return [String] Path to the gcloud binary. @raise [BinaryNotFound] The gcloud binary is not present.

# File lib/google/serverless/exec/gcloud.rb, line 100
def binary_path!
  value = binary_path
  raise BinaryNotFound unless value
  value
end
capture(args) click to toggle source

@private Execute a given gcloud command in a subshell, and return the output as a string.

@param args [Array<String>] The gcloud args. @return [String] The command output.

# File lib/google/serverless/exec/gcloud.rb, line 193
def capture args
  execute args, capture: true
end
current_project() click to toggle source

@private Returns the ID of the current project, or nil if no project has been set.

@return [String,nil] ID of the current project.

# File lib/google/serverless/exec/gcloud.rb, line 113
def current_project
  unless defined? @current_project
    params = [
      "config", "list", "core/project", "--format=value(core.project)"
    ]
    @current_project = execute params, capture: true
    @current_project = nil if @current_project.empty?
  end
  @current_project
end
current_project!() click to toggle source

@private Returns the ID of the current project. Raises ProjectNotSet if no project has been set in the gcloud configuration.

@return [String] ID of the current project. @raise [ProjectNotSet] The project config has not been set.

# File lib/google/serverless/exec/gcloud.rb, line 132
def current_project!
  value = current_project
  raise ProjectNotSet if value.empty?
  value
end
execute(args, echo: false, capture: false, assert: true) click to toggle source

@private Execute a given gcloud command in a subshell.

@param args [Array<String>] The gcloud args. @param echo [boolean] Whether to echo the command to the terminal.

Defaults to false.

@param capture [boolean] If true, return the output. If false, return

a boolean indicating success or failure. Defaults to false.

@param assert [boolean] If true, raise GcloudFailed on failure.

Defaults to true.

@return [String,Integer] Either the output or the success status,

depending on the value of the `capture` parameter.
# File lib/google/serverless/exec/gcloud.rb, line 170
def execute args, echo: false, capture: false, assert: true
  cmd_array = [binary_path!] + args
  cmd =
    if ::Gem.win_platform?
      cmd_array.join " "
    else
      ::Shellwords.join cmd_array
    end
  puts cmd if echo
  result = capture ? `#{cmd}` : system(cmd)
  code = $CHILD_STATUS.exitstatus
  raise GcloudFailed, code if assert && code != 0
  result
end
verify!() click to toggle source

@private Verifies that all gcloud related dependencies are satisfied. Specifically, verifies that the gcloud binary is installed and authenticated, and a project has been set.

@raise [BinaryNotFound] The gcloud binary is not present. @raise [ProjectNotSet] The project config has not been set. @raise [GcloudNotAuthenticated] Gcloud has not been authenticated.

# File lib/google/serverless/exec/gcloud.rb, line 148
def verify!
  binary_path!
  current_project!
  auths = execute ["auth", "list", "--format=value(account)"],
                  capture: true
  raise GcloudNotAuthenticated if auths.empty?
end