collect-repositories/exe/collect-repos

103 lines
2.9 KiB
Text
Raw Normal View History

2022-08-15 22:52:49 +00:00
#!/usr/bin/env ruby
# frozen_string_literal: true
# vim: set ts=2 sw=2 et ft=ruby:
require 'find'
require 'thor'
require 'table_print'
require 'tty-spinner'
2022-08-17 16:49:21 +00:00
require 'colorize'
2022-08-15 22:52:49 +00:00
class ListRepos < Thor
desc 'list [SELECTOR]', 'List repositories'
method_option :root, type: :string, aliases: '-r'
2022-08-17 16:49:21 +00:00
def initialize(*args)
String.disable_colorization = true unless ENV['NO_COLOR'].nil?
super
end
2022-08-17 16:50:03 +00:00
def list(selector_str='')
trap 'SIGINT' do
system 'tput cnorm' # show cursor (fix)
exit! 130
end
2022-08-15 22:52:49 +00:00
basedir = options['root'] || '.'
projects = []
spinner = TTY::Spinner.new(
2022-08-17 16:50:03 +00:00
hide_cursor: true,
2022-08-15 22:52:49 +00:00
clear: true
)
selector = self.class.build_selector(selector_str)
2022-08-17 16:49:21 +00:00
selector_str2 = selector.map { |k, v| "#{k}=#{v.to_s.colorize(:yellow)}" }.join(' AND ')
puts "SELECTOR: #{selector_str2}"
2022-08-15 22:52:49 +00:00
## COLLECT
spinner.auto_spin
2022-08-17 16:50:03 +00:00
system 'tput civis' # hide cursor (fix)
2022-08-15 22:52:49 +00:00
Find.find(basedir) do |path|
next unless path =~ %r{.*/.git/config$}
project_root = File.dirname(File.dirname(path)).gsub(%r{^#{basedir}/}, '')
2022-08-15 22:52:49 +00:00
lines = File.readlines(path)
projects << {
path: project_root,
github: lines.select { |line| line =~ /github\.com/ }.any?,
gitlab: lines.select { |line| line =~ /gitlab\.com/ }.any?,
2022-08-17 16:50:35 +00:00
bitbucket: lines.select { |line| line =~ /bitbucket\.(com|org)/ }.any?,
gitea: lines.select { |line| line =~ /code\.(dinlas\.)?apps\.glenux\.net/ }.any?,
mr: self.class.mr_enabled?(project_root, lines)
2022-08-15 22:52:49 +00:00
}
end
spinner.stop
2022-08-17 16:50:03 +00:00
system 'tput cnorm' # show cursor (fix)
puts ''
2022-08-15 22:52:49 +00:00
## REDUCE
projects_selected = projects.select do |vals|
res = true
selector.each do |k, v|
2022-08-15 22:52:49 +00:00
res &&= (vals[k] == v)
end
res
end
## DISPLAY
# require 'pp'
# pp projects_selected
tp.set :max_width, 100
2022-08-17 16:50:35 +00:00
columns = [:path] + ([:github, :gitlab, :bitbucket, :gitea, :mr] - selector.keys)
tp projects_selected, *columns
end
def self.mr_enabled?(project_root, _lines)
system "cd #{project_root} && mr status >/dev/null 2>&1"
$?.success?
2022-08-15 22:52:49 +00:00
end
def self.build_selector(str)
str.split(',').map do |keyval|
name, value = keyval.split('=')
{ name.to_sym => (value =~ /true/i) ? true : false }
end
.inject({}, &:merge)
end
end
ListRepos.start(ARGV)
# find ~/src -ipath '*/.git/config' -exec sh -c "grep -l 'github.com' {} \
# | sed -e 's,^,github ,' -e 's,.git/config$,,'" \;
#
# find ~/src -ipath '*/.git/config' -exec sh -c "grep -l 'bitbucket.com' {} \
# | sed -e 's,^,bitbucket ,' -e 's,.git/config$,,'" \;
#
# find ~/src -ipath '*/.git/config' -exec sh -c "grep -l 'gitlab.com' {} \
# | sed -e 's,^,gitlab ,' -e 's,.git/config$,,'" \;
#
# find ~/src -ipath '*/.git/config' -exec sh -c "grep -l 'code.dinlas.apps.glenux.net' {} \
# | sed -e 's,^,gitea-glenux ,' -e 's,.git/config$,,'" \;