Class: RepoTender::CLI::Org::Add

Inherits:
Dry::CLI::Command
  • Object
show all
Includes:
GlobalOptions, Helpers
Defined in:
lib/repo_tender/cli/org.rb

Instance Method Summary collapse

Methods included from GlobalOptions

included

Methods included from Helpers

fail_with, format_failure, format_ref, parse_ref, same_org?

Instance Method Details

#call(name:, include_archived: false, include_forks: false, plain: nil, json: nil, no_color: nil, quiet: nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/repo_tender/cli/org.rb', line 72

def call(name:, include_archived: false, include_forks: false, plain: nil, json: nil, no_color: nil, quiet: nil, **)
  mode = UI::Mode.resolve(
    flags: {plain: plain, json: json, no_color: no_color, quiet: quiet},
    env: CLI.env,
    out: out
  )
  pastel = Pastel.new(enabled: mode.color)

  parsed = parse_ref(name,
    include_archived: include_archived,
    include_forks: include_forks)
  return fail_with(self, parsed.failure) if parsed.failure?

  new_ref = parsed.success
  paths = CLI.make_paths
  config = Config::Store.load(paths.config_file).success

  if config.orgs.any? { |o| same_org?(o, new_ref) }
    out.puts pastel.yellow("already tracked: #{format_ref(new_ref)}" \
      " (include_archived=#{new_ref.include_archived}, include_forks=#{new_ref.include_forks})")
    return CLI.record_outcome(Outcome.new(exit_code: 0))
  end

  result = Config::Store.update(paths.config_file) do |c|
    Config::Store.with(c, orgs: c.orgs + [new_ref])
  end
  if result.failure?
    return fail_with(self, "failed to update config: #{format_failure(result.failure)}")
  end

  out.puts pastel.green("added: #{format_ref(new_ref)}" \
    " (include_archived=#{new_ref.include_archived}, include_forks=#{new_ref.include_forks})")
  CLI.record_outcome(Outcome.new(exit_code: 0))
end