diff --git a/lib/baes.rb b/lib/baes.rb index 71fef54..84676bf 100644 --- a/lib/baes.rb +++ b/lib/baes.rb @@ -7,6 +7,7 @@ class Baes::Error < StandardError; end require_relative "baes/configuration" require_relative "baes/configuration/helpers" +require_relative "baes/helpers" require_relative "baes/actions" require_relative "baes/branch" diff --git a/lib/baes/git.rb b/lib/baes/git.rb index 400b082..af9c773 100644 --- a/lib/baes/git.rb +++ b/lib/baes/git.rb @@ -5,6 +5,7 @@ # module to encapsulate git commands in the shell module Baes::Git + extend Baes::Helpers::Commands extend Baes::Configuration::Helpers class GitError < StandardError; end @@ -80,28 +81,5 @@ def delete_branches(branch_names) output.puts("deleting branches") output.puts(run_or_raise("git branch -d #{branch_names.join(" ")}")) end - - private - - def run_or_raise(command) - stdout, stderr, status = Open3.capture3(command) - - unless status.success? - output.puts(stderr) - - raise GitError, "failed to run '#{command}'" - end - - stdout.strip - end - - def run_returning_status(command) - stdout, stderr, status = Open3.capture3(command) - - output.puts(stdout) - output.puts(stderr) unless stderr.empty? - - status - end end end diff --git a/lib/baes/helpers.rb b/lib/baes/helpers.rb new file mode 100644 index 0000000..ac2cd7d --- /dev/null +++ b/lib/baes/helpers.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +# module to encapsulate helpers +module Baes::Helpers +end + +require_relative "helpers/commands" diff --git a/lib/baes/helpers/commands.rb b/lib/baes/helpers/commands.rb new file mode 100644 index 0000000..2fe1e68 --- /dev/null +++ b/lib/baes/helpers/commands.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +# module to encapsulate shell commands +module Baes::Helpers::Commands + # run command and raise error on failure + def run_or_raise(command) + stdout, stderr, status = Open3.capture3(command) + + unless status.success? + output.puts(stderr) + + raise Baes::Git::GitError, "failed to run '#{command}'" + end + + stdout.strip + end + + # run command and return status + def run_returning_status(command) + stdout, stderr, status = Open3.capture3(command) + + output.puts(stdout) + output.puts(stderr) unless stderr.empty? + + status + end +end