module AppEngine::Util::Gcloud
A collection of utility functions and classes for interacting with an installation of the gcloud SDK.
Public Class Methods
@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/appengine/util/gcloud.rb, line 78 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
@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/appengine/util/gcloud.rb, line 99 def binary_path! value = binary_path raise BinaryNotFound unless value value end
@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/appengine/util/gcloud.rb, line 192 def capture args execute args, capture: true end
@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/appengine/util/gcloud.rb, line 112 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
@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/appengine/util/gcloud.rb, line 131 def current_project! value = current_project raise ProjectNotSet if value.empty? value end
@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/appengine/util/gcloud.rb, line 169 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
@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/appengine/util/gcloud.rb, line 147 def verify! binary_path! current_project! auths = execute ["auth", "list", "--format=value(account)"], capture: true raise GcloudNotAuthenticated if auths.empty? end