Class: CemWinSpec::WinExec::BaseCmd

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/cem_win_spec/win_exec/cmd/base_cmd.rb

Direct Known Subclasses

LocalCmd, WinRMCmd

Constant Summary collapse

COMMAND_SEPARATOR =
'; '
PUPPET_VER_TO_RUBY_VER =
{
  '7' => '278',
  '8' => '322',
}.freeze
TOOL_DIR_BY_RUBY_VER =
{
  '278' => 'C:/tools/ruby/2.7.8',
  '322' => 'C:/tools/ruby/3.2.2',
}.freeze

Constants included from Logging

Logging::LEVEL_MAP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#current_log_format, current_log_format, current_log_level, #current_log_level, included, log_setup!, #log_setup!, logger, #logger, new_log_formatter, #new_log_formatter, new_log_level, #new_log_level

Constructor Details

#initialize(working_dir = nil, puppet_version: nil, **env_vars) ⇒ BaseCmd

Returns a new instance of BaseCmd.



22
23
24
25
26
# File 'lib/cem_win_spec/win_exec/cmd/base_cmd.rb', line 22

def initialize(working_dir = nil, puppet_version: nil, **env_vars)
  @working_dir = working_dir
  self.puppet_version = puppet_version
  self.env_vars = env_vars
end

Instance Attribute Details

#env_varsObject

Returns the value of attribute env_vars.



20
21
22
# File 'lib/cem_win_spec/win_exec/cmd/base_cmd.rb', line 20

def env_vars
  @env_vars
end

#puppet_versionObject

Returns the value of attribute puppet_version.



20
21
22
# File 'lib/cem_win_spec/win_exec/cmd/base_cmd.rb', line 20

def puppet_version
  @puppet_version
end

#working_dirObject

Returns the value of attribute working_dir.



20
21
22
# File 'lib/cem_win_spec/win_exec/cmd/base_cmd.rb', line 20

def working_dir
  @working_dir
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


56
57
58
# File 'lib/cem_win_spec/win_exec/cmd/base_cmd.rb', line 56

def available?
  raise NotImplementedError
end

#command(cmd, ruby_cmd: true, add_envs: true, chdir: true, **_kwargs) ⇒ String

Formats the command to be run

Parameters:

  • cmd (String, Array[String])

    the command to be run

  • ruby_cmd (Boolean) (defaults to: true)

    Whether the command is a ruby command and should be ran in a ruby context. If false, the command will not set up the Uru ruby environment.

  • add_envs (Boolean) (defaults to: true)

    Whether to add the environment variables to the command

  • chdir (Boolean) (defaults to: true)

    Whether to change the working directory to the working dir

Returns:

  • (String)

    the formatted command



80
81
82
83
84
85
86
87
# File 'lib/cem_win_spec/win_exec/cmd/base_cmd.rb', line 80

def command(cmd, ruby_cmd: true, add_envs: true, chdir: true, **_kwargs)
  cmd = cmd.join(COMMAND_SEPARATOR) if cmd.is_a?(Array)
  cmd = [cmd]
  cmd.unshift(change_ruby_version_cmd) if ruby_cmd && ruby_version # executes third
  env_vars.each { |k, v| cmd.unshift(set_env_var_cmd(k, v)) } if add_envs && env_vars.any? # executes second
  cmd.unshift(change_working_dir_cmd(working_dir)) if chdir && working_dir # executes first
  cmd.join(COMMAND_SEPARATOR)
end

#ruby_versionString?

Returns the ruby version that will be used to execute the command The ruby version is determined by the puppet version that is set

Returns:

  • (String, nil)

    the ruby version or nil if the puppet version is not set



67
68
69
70
71
# File 'lib/cem_win_spec/win_exec/cmd/base_cmd.rb', line 67

def ruby_version
  return nil unless puppet_version

  PUPPET_VER_TO_RUBY_VER[puppet_version.split('.')[0]]
end

#run(cmd, *_args, **_kwargs) ⇒ Object

Raises:

  • (NotImplementedError)


60
61
62
# File 'lib/cem_win_spec/win_exec/cmd/base_cmd.rb', line 60

def run(cmd, *_args, **_kwargs)
  raise NotImplementedError
end