Created
May 23, 2013 19:50
-
-
Save ericzou/5638917 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
command = ARGV.shift | |
args = ARGV | |
puts "arguments: #{command}, args: #{args}" | |
raise "Unknow command" unless ['co', 'pop'].include?(command) | |
file_path = '/tmp/.bstack' | |
module BStack | |
class PopCmd | |
attr_reader :file | |
def initialize(file) | |
@file = file | |
end | |
def run | |
puts "running pop command" | |
@file.pop | |
end | |
end | |
class CheckoutCmd | |
attr_reader :file, :new_branch | |
def initialize(file, new_branch) | |
@file = file | |
@new_branch = new_branch | |
end | |
def run | |
puts "running checkout #{new_branch} command" | |
current_branch = GitCmds.current_branch | |
if GitCmds.git_co_new_branch(new_branch) | |
puts "here" | |
file.push(current_branch) | |
end | |
end | |
end | |
class StackFile | |
PATH = '/tmp/.bstack' | |
def pop | |
File.open(PATH, 'r+') do |f| | |
buffer_arry = f.readlines | |
buffer_arry.pop | |
f.truncate(0) | |
f.puts(buffer_arry.join()) | |
end | |
end | |
def push(branch) | |
File.open(PATH, 'a+') do |f| | |
require 'pry' | |
f.puts(branch) | |
end | |
end | |
end | |
class GitCmds | |
def self.current_branch | |
str = `git rev-parse --abbrev-ref HEAD` | |
str.gsub(/\n/, '') | |
end | |
def self.git_co_new_branch(branch) | |
`git checkout #{branch}` | |
current_branch == branch ? true : false | |
end | |
end | |
class Runner | |
COMMANDS = { pop: BStack::PopCmd, co: BStack::CheckoutCmd } | |
def self. run(command, *args) | |
file = StackFile.new | |
cmd = COMMANDS[command.to_sym].new(file, *args) | |
cmd.run | |
end | |
end | |
end | |
BStack::Runner.run(command, *args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment