Class: Gurney::CLI

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(cmd_parameter = []) ⇒ CLI

Returns a new instance of CLI.

Raises:



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
# File 'lib/gurney/cli.rb', line 31

def initialize(cmd_parameter=[])
  @options = Gurney::CLI::OptionParser.parse(cmd_parameter)
  @git = if options.hook
    Git.bare(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|
    file = read_file(options.hook, branch, options.config_file)
    break file if file
  end
  if options.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)

  options.branches ||= config&.branches
  options.branches ||= config&.branches
  options.api_token ||= config&.api_token
  options.api_url ||= config&.api_url
  options.project_id ||= config&.project_id

  missing_options = [:project_id, :branches, :api_url, :api_token].select { |option| options.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 #{missing_options.map(&:inspect).join(', ')}.") unless missing_options.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.message.red
rescue Gurney::Error => e
  puts "Gurney error: #{e.message}".red
rescue Exception
  puts "Gurney: an unexpected error occurred".red
  raise
end

Instance Method Details

#runObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gurney/cli.rb', line 65

def run
  reporting_branches.each do |branch|
    dependencies = []

    yarn_source = Gurney::Source::Yarn.new(yarn_lock: read_file(options.hook || options.client_hook, branch, 'yarn.lock'))
    dependencies.concat yarn_source.dependencies || []

    bundler_source = Gurney::Source::Bundler.new(gemfile_lock: read_file(options.hook || options.client_hook, branch, 'Gemfile.lock'))
    dependencies.concat bundler_source.dependencies || []

    ruby_version_source = Gurney::Source::RubyVersion.new(ruby_version: read_file(options.hook || options.client_hook, branch, '.ruby-version'))
    dependencies.concat ruby_version_source.dependencies || []

    dependencies.compact!

    api = Gurney::Api.new(base_url: options.api_url, token: options.api_token)
    api.post_dependencies(dependencies: dependencies, branch: branch, project_id: options.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