Class: NextcloudReleaseAgent::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/nextcloud_release_agent/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(logger:, dry_run: false) ⇒ Shell

Returns a new instance of Shell.



53
54
55
56
# File 'lib/nextcloud_release_agent/cli.rb', line 53

def initialize(logger:, dry_run: false)
  @logger = logger
  @dry_run = dry_run
end

Instance Method Details

#capture(*command, chdir:, allow_failure: false, env: {}) ⇒ Object

Raises:



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/nextcloud_release_agent/cli.rb', line 58

def capture(*command, chdir:, allow_failure: false, env: {})
  printable = command.shelljoin
  @logger.info("#{@dry_run ? 'would run' : 'running'}: #{printable}")
  return CommandResult.new(stdout: "", stderr: "", status: 0) if @dry_run

  stdout, stderr, status = Open3.capture3(env, *command, chdir: chdir)
  result = CommandResult.new(stdout: stdout, stderr: stderr, status: status.exitstatus)
  return result if status.success? || allow_failure

  raise Error, "command failed (#{status.exitstatus}): #{printable}\n#{stderr.strip}"
end

#system!(*command, chdir:, allow_failure: false, env: {}) ⇒ Object

Raises:



70
71
72
73
74
75
# File 'lib/nextcloud_release_agent/cli.rb', line 70

def system!(*command, chdir:, allow_failure: false, env: {})
  result = capture(*command, chdir: chdir, allow_failure: allow_failure, env: env)
  return result if result.status.zero? || allow_failure

  raise Error, "command failed (#{result.status}): #{command.shelljoin}\n#{result.stderr.strip}"
end