Class: Gurney::CLI
- Inherits:
-
Object
- Object
- Gurney::CLI
- Defined in:
- lib/gurney/cli.rb,
lib/gurney/cli/option_parser.rb
Defined Under Namespace
Classes: OptionParser
Constant Summary collapse
- HOOK_STDIN_REGEX =
/(?<old>[0-9a-f]{40}) (?<new>[0-9a-f]{40}) refs\/heads\/(?<ref>\w+)/m- CLIENT_HOOK_STDIN_REGEX =
/refs\/heads\/(?<ref>\w+) (?<new>[0-9a-f]{40}) refs\/heads\/(?<remote_ref>\w+) (?<remote_sha>[0-9a-f]{40})/m- MAIN_BRANCHES =
['master', 'main'].freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(cmd_parameter = []) ⇒ CLI
constructor
A new instance of CLI.
- #run ⇒ Object
Constructor Details
#initialize(cmd_parameter = []) ⇒ CLI
Returns a new instance of CLI.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/gurney/cli.rb', line 31 def initialize(cmd_parameter=[]) @options = Gurney::CLI::OptionParser.parse(cmd_parameter) @git = if .hook Git.(ENV['GIT_DIR'] || Dir.pwd) else unless Dir.exist? './.git' raise Gurney::Error.new('Must be run within a git repository') end Git.open('.') end config_file = MAIN_BRANCHES.find do |branch| git_file_reader = GitFileReader.new(git, branch, read_from_git: .hook) file = git_file_reader.read(.config_file) break file if file end if .hook && !config_file # Git hooks are activated by the config file. Without, do nothing. exit 0 end config_file ||= '---' config = Gurney::Config.from_yaml(config_file) .branches ||= config&.branches .branches ||= config&.branches .api_token ||= config&.api_token .api_url ||= config&.api_url .project_id ||= config&.project_id .prefix ||= config&.prefix = [:project_id, :branches, :api_url, :api_token, :prefix].select { |option| .send(option).nil? } # Use the line below in development # missing_options = [:project_id, :branches, :api_token].select { |option| options.send(option).nil? } raise Gurney::Error.new("Incomplete config - missing #{.map(&:inspect).join(', ')}.") unless .empty? end |
Class Method Details
.run(cmd_parameter = []) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/gurney/cli.rb', line 17 def self.run(cmd_parameter=[]) new(cmd_parameter).run rescue SystemExit # Do nothing rescue Gurney::ApiError => e puts "Gurney API error".red puts e..red rescue Gurney::Error => e puts "Gurney error: #{e.}".red rescue Exception puts "Gurney: an unexpected error occurred".red raise end |
Instance Method Details
#run ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/gurney/cli.rb', line 67 def run reporting_branches.each do |branch| git_file_reader = GitFileReader.new(git, branch, read_from_git: .hook || .client_hook, prefix: .prefix) dependencies = DependencyCollector.new(git_file_reader).collect_all api = Gurney::Api.new(base_url: .api_url, token: .api_token) api.post_dependencies(dependencies: dependencies, branch: branch, project_id: .project_id, repo_path: git.repo.path) dependency_counts = dependencies.group_by(&:ecosystem).map{|ecosystem, dependencies| "#{ecosystem}: #{dependencies.count}" }.join(', ') puts "Gurney: reported dependencies (#{dependency_counts})" end end |