Module: MultiRepo::CLI

Defined in:
lib/multi_repo/cli.rb

Constant Summary collapse

HEADER_SIZE =

Logging helpers

80

Class Method Summary collapse

Class Method Details

.common_options(optimist, only: %i[repo repo_set dry_run],, except: nil, repo_set_default: "master") ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/multi_repo/cli.rb', line 45

def self.common_options(optimist, only: %i[repo repo_set dry_run], except: nil, repo_set_default: "master")
  optimist.banner("")
  optimist.banner("Common Options:")

  subset = Array(only).map(&:to_sym) - Array(except).map(&:to_sym)

  if subset.include?(:repo_set)
    optimist.opt :repo_set, "The repo set to work with", :type => :string, :default => repo_set_default, :short => "s"
  end
  if subset.include?(:repo)
    msg = "Individual repo(s) to work with"
    if subset.include?(:repo_set)
      sub_opts = {}
      msg << " from the --repo-set"
    else
      sub_opts = {:required => true}
    end
    optimist.opt :repo, msg, sub_opts.merge(:type => :strings)
  end
  if subset.include?(:dry_run)
    optimist.opt :dry_run, "Execute without making changes", :default => false
  end
end

.each_repo(**kwargs) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/multi_repo/cli.rb', line 8

def self.each_repo(**kwargs)
  raise "no block given" unless block_given?

  repos_for(**kwargs).each do |repo|
    puts header(repo.name)
    yield repo
    puts
  end
end

.header(title, char = "=") ⇒ Object



75
76
77
78
79
# File 'lib/multi_repo/cli.rb', line 75

def self.header(title, char = "=")
  title = " #{title} "
  start = (HEADER_SIZE / 2) - (title.length / 2)
  separator(char).tap { |h| h[start, title.length] = title }
end

.progress_bar(total = 100) ⇒ Object



85
86
87
88
# File 'lib/multi_repo/cli.rb', line 85

def self.progress_bar(total = 100)
  require "progressbar"
  ProgressBar.create(progress_bar_options(total))
end

.progress_bar_options(total = 100) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/multi_repo/cli.rb', line 90

def self.progress_bar_options(total = 100)
  {
    :format => "%j%% |%B| %E",
    :length => HEADER_SIZE,
    :total  => total
  }
end

.repo_for(repo_name, repo_set: nil, dry_run: false) ⇒ Object



39
40
41
42
43
# File 'lib/multi_repo/cli.rb', line 39

def self.repo_for(repo_name, repo_set: nil, dry_run: false)
  Optimist.die(:repo, "must be specified") if repo_name.nil?

  repos_for(repo: repo_name, repo_set: repo_set, dry_run: dry_run).first
end

.repos_for(repo: nil, repo_set: nil, dry_run: false, **_) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/multi_repo/cli.rb', line 18

def self.repos_for(repo: nil, repo_set: nil, dry_run: false, **_)
  repo = Array(repo).delete_blanks
  Optimist.die("options --repo or --repo_set must be specified") if repo.blank? && repo_set.blank?

  if repo_set.present?
    repos = MultiRepo::RepoSet[repo_set]&.deep_dup
    Optimist.die(:repo_set, "#{repo_set.inspect} was not found in the config") if repos.nil?

    if repo.any?
      repo_names = Set.new(repo)
      repos.select! { |r| repo_names.include?(r.name) }
    end

    repos.each { |r| r.dry_run = dry_run }

    repos
  else
    repo.map { |n| MultiRepo::Repo.new(n, dry_run: dry_run) }
  end
end

.separator(char = "*") ⇒ Object



81
82
83
# File 'lib/multi_repo/cli.rb', line 81

def self.separator(char = "*")
  char * HEADER_SIZE
end