Class: Space::Src::CLI::Org::Remove

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

Instance Method Summary collapse

Methods included from GlobalOptions

included

Methods included from Helpers

fail_with, format_failure, format_ignored, format_ref, parse_ref, same_org?

Instance Method Details

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



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/space_src/cli/org.rb', line 128

def call(name:, plain: nil, json: nil, no_color: nil, quiet: nil, **)
  # The flags don't affect the identity match for remove; we
  # match on (host, name) only. This matches the user's
  # expectation that "remove" targets the org, not the flag
  # combination they added it with.
  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)
  return fail_with(self, parsed.failure) if parsed.failure?

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

  kept = config.orgs.reject { |o| same_org?(o, target) }
  if kept.size == config.orgs.size
    return fail_with(self, "not tracked: #{format_ref(target)}")
  end

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

  out.puts pastel.green("removed: #{format_ref(target)}")
  CLI.record_outcome(Outcome.new(exit_code: 0))
end