Some parts adapted from golang.org/src/pkg/json/decode.go and golang.org/src/pkg/utf8/utf8.go
Note: A group is a set of parameters defined for a subpart of a config file
By default, agree should take a single character in interactive
# File lib/rhc/helpers.rb, line 265 def agree(*args, &block) #args.push(interactive?.presence) if args.length == 1 block = lambda do |q| q.validate = /\A(?:y|yes|n|no)\Z/ end unless block_given? super *args, &block end
# File lib/rhc/helpers.rb, line 218 def certificate_file(file) file && OpenSSL::X509::Certificate.new(IO.read(File.expand_path(file))) rescue => e debug e raise OptionParser::InvalidOption.new(nil, "The certificate '#{file}' cannot be loaded: #{e.message} (#{e.class})") end
# File lib/rhc/helpers.rb, line 200 def client_from_options(opts) RHC::Rest::Client.new({ :url => openshift_rest_endpoint.to_s, :debug => options.debug, :timeout => options.timeout, :warn => BOUND_WARNING, }.merge!(ssl_options).merge!(opts)) end
# File lib/rhc/helpers.rb, line 454 def collect_env_vars(items) return nil if items.blank? env_vars = [] Array(items).each do |item| if match = item.match(env_var_regex_pattern) name, value = match.captures env_vars << RHC::Rest::EnvironmentVariable.new({ :name => name, :value => value }) elsif File.file? item File.readlines(item).each do |line| if match = line.match(env_var_regex_pattern) name, value = match.captures env_vars << RHC::Rest::EnvironmentVariable.new({ :name => name, :value => value }) end end end end env_vars end
OVERRIDE: Replaces default commander behavior
# File lib/rhc/helpers.rb, line 296 def color(item, *args) if item.is_a? Array item.map{ |i| $terminal.color(i, *args) } else $terminal.color(item, *args) end end
# File lib/rhc/helpers.rb, line 273 def confirm_action(question) return if options.confirm return if !options.noprompt && paragraph{ agree "#{question} (yes|no): " } raise RHC::ConfirmationError end
# File lib/rhc/helpers.rb, line 234 def debug(*args) $terminal.debug(*args) end
# File lib/rhc/helpers.rb, line 240 def debug? $terminal.debug? end
# File lib/rhc/helpers.rb, line 237 def debug_error(*args) $terminal.debug_error(*args) end
# File lib/rhc/helpers.rb, line 257 def deprecated(msg,short = false) raise DeprecatedError.new(msg % ['an error','a warning',0]) if disable_deprecated? warn "Warning: #{msg}\n" % ['a warning','an error',1] end
# File lib/rhc/helpers.rb, line 253 def deprecated_command(correct, short=false) deprecated("This command is deprecated. Please use '#{correct}' instead.", short) end
# File lib/rhc/helpers.rb, line 249 def disable_deprecated? ENV['DISABLE_DEPRECATED'] == '1' end
# File lib/rhc/helpers.rb, line 450 def env_var_regex_pattern /^([a-zA-Z_][\w]*)=(.*)$/ end
# File lib/rhc/helpers.rb, line 291 def error(msg, *args) say color(msg, :red), *args end
# File lib/rhc/helpers.rb, line 244 def exec(cmd) output = Kernel.send(:`, cmd) [$?.exitstatus, output] end
Check if host exists
# File lib/rhc/helpers.rb, line 393 def host_exists?(host) # :nocov: # Patch for BZ840938 to support Ruby 1.8 on machines without /etc/resolv.conf dns = Resolv::DNS.new((Resolv::DNS::Config.default_config_hash || {})) dns.getresources(host, Resolv::DNS::Resource::IN::A).any? # :nocov: end
# File lib/rhc/helpers.rb, line 401 def hosts_file_contains?(host) with_tolerant_encoding do begin resolver = Resolv::Hosts.new resolver.getaddress host rescue => e debug "Error while resolving with Resolv::Hosts: #{e.message}(#{e.class})\n #{e.backtrace.join("\n ")}" end end end
# File lib/rhc/helpers.rb, line 283 def info(msg, *args) say color(msg, :cyan), *args end
Output helpers
# File lib/rhc/helpers.rb, line 230 def interactive? $stdin.tty? and $stdout.tty? and not options.noprompt end
Platform helpers
# File lib/rhc/helpers.rb, line 385 def jruby? ; RUBY_PLATFORM =~ /java/ end
# File lib/rhc/helpers.rb, line 388 def mac? ; RbConfig::CONFIG['host_os'] =~ /^darwin/ end
# File lib/rhc/helpers.rb, line 158 def openshift_online_server? openshift_server =~ /openshift.redhat.com$/ end
# File lib/rhc/helpers.rb, line 190 def openshift_rest_endpoint uri = to_uri((options.server rescue nil) || ENV['LIBRA_SERVER'] || "openshift.redhat.com") uri.path = '/broker/rest/api' if uri.path.blank? || uri.path == '/' uri end
# File lib/rhc/helpers.rb, line 155 def openshift_server to_host((options.server rescue nil) || ENV['LIBRA_SERVER'] || "openshift.redhat.com") end
# File lib/rhc/helpers.rb, line 161 def openshift_url "https://#{openshift_server}" end
# File lib/rhc/helpers.rb, line 310 def pluralize(count, s) count == 1 ? "#{count} #{s}" : "#{count} #{s}s" end
results
highline helper which creates a paragraph with a header to distinguish the final results of a command from other output
# File lib/rhc/helpers.rb, line 377 def results(&block) section(:top => 1, :bottom => 0) do say "RESULT:" yield end end
# File lib/rhc/helpers.rb, line 151 def role_name(s) ROLES[s.downcase] end
Run a command and export its output to the user. Output is not capturable on all platforms.
# File lib/rhc/helpers.rb, line 433 def run_with_tee(cmd) status, stdout, stderr = nil if windows? #:nocov: TODO: Test block system(cmd) status = $?.exitstatus #:nocov: else stdout, stderr = [$stdout, $stderr].map{ |t| StringTee.new(t) } status = Open4.spawn(cmd, 'stdout' => stdout, 'stderr' => stderr, 'quiet' => true) stdout, stderr = [stdout, stderr].map(&:string) end [status, stdout, stderr] end
# File lib/rhc/helpers.rb, line 176 def ssh_string(ssh_url) return nil if ssh_url.blank? uri = URI.parse(ssh_url) "#{uri.user}@#{uri.host}" rescue => e RHC::Helpers.debug_error(e) ssh_url end
# File lib/rhc/helpers.rb, line 185 def ssh_string_parts(ssh_url) uri = URI.parse(ssh_url) [uri.host, uri.user] end
# File lib/rhc/helpers.rb, line 209 def ssl_options { :ssl_version => options.ssl_version, :client_cert => certificate_file(options.ssl_client_cert), :ca_file => options.ssl_ca_file && File.expand_path(options.ssl_ca_file), :verify_mode => options.insecure ? OpenSSL::SSL::VERIFY_NONE : nil, }.delete_if{ |k,v| v.nil? } end
# File lib/rhc/helpers.rb, line 279 def success(msg, *args) say color(msg, :green), *args end
This will format table headings for a consistent look and feel
If a heading isn't explicitly defined, it will attempt to look up the parts If those aren't found, it will capitalize the string
# File lib/rhc/helpers.rb, line 317 def table_heading(value) # Set the default proc to look up undefined values headings = Hash.new do |hash,key| items = key.to_s.split('_') # Look up each piece individually hash[key] = items.length > 1 ? # Recusively look up the heading for the parts items.map{|x| headings[x.to_sym]}.join(' ') : # Capitalize if this part isn't defined items.first.capitalize end # Predefined headings (or parts of headings) headings.merge!({ :creation_time => "Created", :expires_in_seconds => "Expires In", :uuid => "ID", :id => 'ID', :current_scale => "Current", :scales_from => "Minimum", :scales_to => "Maximum", :gear_sizes => "Allowed Gear Sizes", :consumed_gears => "Gears Used", :max_gears => "Gears Allowed", :max_domains => "Domains Allowed", :compact_members => "Members", :gear_info => "Gears", :plan_id => "Plan", :url => "URL", :ssh_string => "SSH", :connection_info => "Connection URL", :gear_profile => "Gear Size", :visible_to_ssh? => 'Available', :downloaded_cartridge_url => 'From', :auto_deploy => 'Deployment', :sha1 => 'SHA1', :ref => 'Git Reference' }) headings[value] end
# File lib/rhc/helpers.rb, line 165 def to_host(s) s =~ %r(^http(?:s)?://) ? URI(s).host : s end
# File lib/rhc/helpers.rb, line 168 def to_uri(s) begin URI(s =~ %r(^http(?:s)?://) ? s : "https://#{s}") rescue URI::InvalidURIError raise RHC::InvalidURIException.new(s) end end
# File lib/rhc/helpers.rb, line 196 def token_for_user options.token or (token_store.get(options.rhlogin, options.server) if options.rhlogin && options.use_authorization_tokens) end
# File lib/rhc/helpers.rb, line 387 def unix? ; !jruby? && !windows? end
# File lib/rhc/helpers.rb, line 287 def warn(msg, *args) say color(msg, :yellow), *args end
# File lib/rhc/helpers.rb, line 386 def windows? ; RUBY_PLATFORM =~ /win(32|dows|ce)|djgpp|(ms|cyg|bcc)win|mingw32/ end
# File lib/rhc/helpers.rb, line 412 def with_tolerant_encoding(&block) # :nocov: if RUBY_VERSION.to_f >= 1.9 orig_default_internal = Encoding.default_internal Encoding.default_internal = 'ISO-8859-1' else orig_default_kcode = $KCODE $KCODE = 'N' end yield ensure if RUBY_VERSION.to_f >= 1.9 Encoding.default_internal = orig_default_internal else $KCODE = orig_default_kcode end # :nocov: end