Skip to content

Commit

Permalink
extract commands (#102)
Browse files Browse the repository at this point in the history
So we can reuse them in other places
  • Loading branch information
mockdeep authored Sep 17, 2024
1 parent fe36bb5 commit 64efe21
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 23 deletions.
1 change: 1 addition & 0 deletions lib/baes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
24 changes: 1 addition & 23 deletions lib/baes/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
7 changes: 7 additions & 0 deletions lib/baes/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

# module to encapsulate helpers
module Baes::Helpers
end

require_relative "helpers/commands"
27 changes: 27 additions & 0 deletions lib/baes/helpers/commands.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 64efe21

Please sign in to comment.