class Omnitest::Psychic
The primary interface for using Psychic
as an API.
Detects scripts and tools that can run tasks in the instance’s working directory, so that Psychic
can act as a universal task/script selection and execution system.
Constants
- DEFAULT_PARAMS_FILE
- VERSION
Attributes
@return [Dir] Current working directory for running commands.
@return [Dir] Current working directory for running commands.
@return [Hash] Environment variables to use when executing commands.
The default is to pass all environment variables to the command.
@return [String] A name for logging and reporting. The default value is the name of the current working directory.
@return [Hash] Additional options
@return [String] The Operating System to target. Autodetected if unset.
@return [Hash] Parameters to use as input for scripts.
Public Class Methods
Creates a new Psychic
instance that can be used to execute tasks and scripts. All options are @params [Hash] opts @option opts [Dir] :cwd sets the current working directory @option opts [Logger] :logger assigns a logger @option opts [Hash] :env sets environment variables @option opts [String] :name a name for logging and reporting @option opts [String] :os the target operating system @option opts [String] :interactive run psychic in interactive mode, where it will prompt for input
# File lib/omnitest/psychic.rb, line 80 def initialize(opts = { cwd: Dir.pwd }) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize opts[:cwd] ||= Dir.pwd # must be a string on windows... @cwd = opts[:cwd] = Pathname(opts[:cwd]).to_s @opts = opts init_attr(:name) { File.basename cwd } init_hints init_attr(:logger) { new_logger } init_attr(:env) { ENV.to_hash } init_attr(:os) { RbConfig::CONFIG['host_os'] } init_attrs :cli, :interactive, :parameter_mode, :restore_mode, :print @shell_opts = select_shell_opts @parameters = load_parameters(opts[:parameters]) end
Public Instance Methods
Executes a command using the options set on this Psychic
instance.
@param [String] command the command to execute @param [*args] *args additional arguments to join to the command @return [ExecutionResult] the result of running the command @raises [ExecutionError] if the command
@example
psychic.execute('echo', 'hello', 'world') #<Omnitest::Shell::ExecutionResult:0x007fdfe15208f0 @command="echo hello world", @exitstatus=0, @stderr="", @stdout="hello world\n">
@example
psychic.execute('foo') # Omnitest::Shell::ExecutionError: No such file or directory - foo
# File lib/omnitest/psychic.rb, line 109 def execute(command, *args) shell_opts = @shell_opts.dup shell_opts.merge!(args.shift) if args.first.is_a? Hash full_cmd = [command, *args].join(' ') logger.banner("Executing: #{full_cmd}") shell.execute(full_cmd, shell_opts) end
@return [Boolean] true if Psychic
is in interactive mode and will prompt for decisions
# File lib/omnitest/psychic.rb, line 139 def interactive? @opts[:interactive] end
Detects the Operating System family for the selected Operating System. @return [Symbol] The operating system family for {#os}.
# File lib/omnitest/psychic.rb, line 123 def os_family case os when /mswin|msys|mingw|cygwin|bccwin|wince|emc/ :windows when /darwin|mac os/ :macosx when /linux/ :linux when /solaris|bsd/ :unix else :unknown end end
# File lib/omnitest/psychic.rb, line 117 def workflow(name = 'workflow', options = {}, &block) Workflow.new(self, name, options, &block) end
Private Instance Methods
# File lib/omnitest/psychic.rb, line 145 def init_attr(var) var_name = "@#{var}" var_value = @opts[var] var_value = yield if var_value.nil? && block_given? instance_variable_set(var_name, var_value) end
# File lib/omnitest/psychic.rb, line 152 def init_attrs(*vars) vars.each do | var | init_attr var end end
# File lib/omnitest/psychic.rb, line 158 def init_hints hint_data = Omnitest::Core::Util.symbolized_hash(@opts[:hints] || load_hints || {}) @hints = Hints.new hint_data @opts.merge! Omnitest::Core::Util.symbolized_hash(@hints.options) end
# File lib/omnitest/psychic.rb, line 169 def load_hints hints_file = Dir.glob("#{@cwd}/psychic.{yaml,yml}", File::FNM_CASEFOLD).first YAML.load(File.read(hints_file)) unless hints_file.nil? end
# File lib/omnitest/psychic.rb, line 174 def load_parameters(parameters) if parameters.nil? || parameters.is_a?(String) load_parameters_file(parameters) else parameters end end
# File lib/omnitest/psychic.rb, line 182 def load_parameters_file(file = nil) if file.nil? file ||= File.expand_path(DEFAULT_PARAMS_FILE, cwd) return {} unless File.exist? file end # Just return it as a template, not as YAML File.read(file) end
# File lib/omnitest/psychic.rb, line 164 def select_shell_opts # Make sure to delete any option that isn't a MixLib::ShellOut option @opts.select { |key, _| Omnitest::Shell::AVAILABLE_OPTIONS.include? key } end