-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Differentiate between options and arguments
i.e., we now use `--help` rather than a `help` sub-command.
- Loading branch information
1 parent
6b694ef
commit 9c19650
Showing
2 changed files
with
86 additions
and
37 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,81 @@ | ||
require "optparse" | ||
require "git_tracker/prepare_commit_message" | ||
require "git_tracker/hook" | ||
require "git_tracker/repository" | ||
require "git_tracker/version" | ||
|
||
module GitTracker | ||
module Runner | ||
def self.call(cmd_arg = "help", *args) | ||
command = cmd_arg.tr("-", "_") | ||
class Runner | ||
def self.call(*args, io: $stdout) | ||
args << "--help" if args.empty? | ||
options = {} | ||
|
||
abort("[git_tracker] command: '#{cmd_arg}' does not exist.") unless respond_to?(command) | ||
OptionParser.new { |optparse| | ||
optparse.banner = <<~BANNER | ||
git-tracker is a Git hook used during the normal lifecycle of committing, | ||
rebasing, merging, etc… This hook must be initialized into each repository | ||
in which you wish to use it. | ||
send(command, *args) | ||
usage: git-tracker init | ||
BANNER | ||
|
||
optparse.on("-h", "--help", "Prints this help") do | ||
io.puts(optparse) | ||
options[:exit] = true | ||
end | ||
|
||
optparse.on("-v", "--version", "Prints the git-tracker version number") do | ||
io.puts("git-tracker #{VERSION}") | ||
options[:exit] = true | ||
end | ||
}.parse!(args) | ||
|
||
return if options.fetch(:exit, false) | ||
|
||
command, *others = args | ||
|
||
new(command: command, arguments: others, options: options).call | ||
end | ||
|
||
def initialize(command:, arguments:, options:) | ||
@command = command | ||
@arguments = arguments | ||
@options = options | ||
end | ||
|
||
def self.prepare_commit_msg(*args) | ||
PrepareCommitMessage.call(*args) | ||
def call | ||
abort("[git_tracker] command: '#{command}' does not exist.") unless sub_command | ||
|
||
send(sub_command) | ||
end | ||
|
||
def self.init | ||
private | ||
|
||
SUB_COMMANDS = { | ||
init: :init, | ||
install: :install, | ||
"prepare-commit-msg": :prepare_commit_msg | ||
}.freeze | ||
private_constant :SUB_COMMANDS | ||
|
||
attr_reader :arguments, :command, :options | ||
|
||
def init | ||
Hook.init(at: Repository.root) | ||
end | ||
|
||
def self.install | ||
warn("`git-tracker install` is deprecated. Please use `git-tracker init`", uplevel: 1) | ||
def install | ||
warn("`git-tracker install` is deprecated. Please use `git-tracker init`.") | ||
|
||
init | ||
end | ||
|
||
def self.help | ||
puts <<~HELP | ||
git-tracker #{VERSION} is installed. | ||
def prepare_commit_msg | ||
PrepareCommitMessage.call(*arguments) | ||
end | ||
|
||
Remember, git-tracker is a hook which Git interacts with during its normal | ||
lifecycle of committing, rebasing, merging, etc. You need to initialize this | ||
hook by running `git-tracker init` from each repository in which you wish to | ||
use it. Cheers! | ||
HELP | ||
def sub_command | ||
@sub_command ||= SUB_COMMANDS.fetch(command.intern, false) | ||
end | ||
end | ||
end |
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