Sha256: 0b5ead1d8ce9f22f8bc11ffea91c95f5bac0aa22abb76439f198feb1a1cfb7a9

Contents?: true

Size: 1.36 KB

Versions: 4

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module Examples
    module Standard
      module V1
        class Gemfile
          module Services
            class RunShellCommand
              include ConvenientService::Standard::V1::Config

              attr_reader :command, :debug

              step :validate_command,
                in: :command

              step :result,
                in: :command

              def initialize(command:, debug: false)
                @command = command
                @debug = debug
              end

              def result
                ##
                # NOTE: When the command exit code is 0, `system` return `true`, and `false` otherwise.
                # - https://ruby-doc.org/core-3.1.2/Kernel.html#method-i-system
                # - https://stackoverflow.com/a/37329716/12201472
                #
                if system(command)
                  success
                else
                  error(message: "#{command} returned non-zero exit code")
                end
              end

              private

              def validate_command
                return failure(command: "Command is `nil`") if command.nil?
                return failure(command: "Command is empty") if command.empty?

                success
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
convenient_service-0.17.0 lib/convenient_service/examples/standard/v1/gemfile/services/run_shell_command.rb
convenient_service-0.16.0 lib/convenient_service/examples/standard/v1/gemfile/services/run_shell_command.rb
convenient_service-0.15.0 lib/convenient_service/examples/standard/v1/gemfile/services/run_shell_command.rb
convenient_service-0.14.0 lib/convenient_service/examples/standard/v1/gemfile/services/run_shell_command.rb