Allow users to remove all versions of a box with --all
This commit is contained in:
commit
a8afb40173
|
@ -15,6 +15,7 @@ module Vagrant
|
||||||
box_provider = env[:box_provider]
|
box_provider = env[:box_provider]
|
||||||
box_provider = box_provider.to_sym if box_provider
|
box_provider = box_provider.to_sym if box_provider
|
||||||
box_version = env[:box_version]
|
box_version = env[:box_version]
|
||||||
|
box_remove_all_versions = env[:box_remove_all_versions]
|
||||||
|
|
||||||
boxes = {}
|
boxes = {}
|
||||||
env[:box_collection].all.each do |n, v, p|
|
env[:box_collection].all.each do |n, v, p|
|
||||||
|
@ -53,7 +54,7 @@ module Vagrant
|
||||||
if all_versions.length == 1
|
if all_versions.length == 1
|
||||||
# There is only one version, just use that.
|
# There is only one version, just use that.
|
||||||
box_version = all_versions.first
|
box_version = all_versions.first
|
||||||
else
|
elsif not box_remove_all_versions
|
||||||
# There are multiple versions, we can't choose.
|
# There are multiple versions, we can't choose.
|
||||||
raise Errors::BoxRemoveMultiVersion,
|
raise Errors::BoxRemoveMultiVersion,
|
||||||
name: box_name,
|
name: box_name,
|
||||||
|
@ -68,48 +69,53 @@ module Vagrant
|
||||||
versions: all_versions.sort.map { |k| " * #{k}" }.join("\n")
|
versions: all_versions.sort.map { |k| " * #{k}" }.join("\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
box = env[:box_collection].find(
|
versions_to_remove = [box_version]
|
||||||
box_name, box_provider, box_version)
|
versions_to_remove = all_versions if box_remove_all_versions
|
||||||
|
|
||||||
# Verify that this box is not in use by an active machine,
|
versions_to_remove.sort.each do |version_to_remove|
|
||||||
# otherwise warn the user.
|
box = env[:box_collection].find(
|
||||||
users = box.in_use?(env[:machine_index]) || []
|
box_name, box_provider, box_version)
|
||||||
users = users.find_all { |u| u.valid?(env[:home_path]) }
|
|
||||||
if !users.empty?
|
|
||||||
# Build up the output to show the user.
|
|
||||||
users = users.map do |entry|
|
|
||||||
"#{entry.name} (ID: #{entry.id})"
|
|
||||||
end.join("\n")
|
|
||||||
|
|
||||||
force_key = :force_confirm_box_remove
|
# Verify that this box is not in use by an active machine,
|
||||||
message = I18n.t(
|
# otherwise warn the user.
|
||||||
"vagrant.commands.box.remove_in_use_query",
|
users = box.in_use?(env[:machine_index]) || []
|
||||||
name: box.name,
|
users = users.find_all { |u| u.valid?(env[:home_path]) }
|
||||||
provider: box.provider,
|
if !users.empty?
|
||||||
version: box.version,
|
# Build up the output to show the user.
|
||||||
users: users) + " "
|
users = users.map do |entry|
|
||||||
|
"#{entry.name} (ID: #{entry.id})"
|
||||||
|
end.join("\n")
|
||||||
|
|
||||||
# Ask the user if we should do this
|
force_key = :force_confirm_box_remove
|
||||||
stack = Builder.new.tap do |b|
|
message = I18n.t(
|
||||||
b.use Confirm, message, force_key
|
"vagrant.commands.box.remove_in_use_query",
|
||||||
|
name: box.name,
|
||||||
|
provider: box.provider,
|
||||||
|
version: box.version,
|
||||||
|
users: users) + " "
|
||||||
|
|
||||||
|
# Ask the user if we should do this
|
||||||
|
stack = Builder.new.tap do |b|
|
||||||
|
b.use Confirm, message, force_key
|
||||||
|
end
|
||||||
|
|
||||||
|
result = env[:action_runner].run(stack, env)
|
||||||
|
if !result[:result]
|
||||||
|
# They said "no", so continue with the next box
|
||||||
|
next
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
result = env[:action_runner].run(stack, env)
|
env[:ui].info(I18n.t("vagrant.commands.box.removing",
|
||||||
if !result[:result]
|
name: box.name,
|
||||||
# They said "no", so just return
|
provider: box.provider,
|
||||||
return @app.call(env)
|
version: box.version))
|
||||||
end
|
box.destroy!
|
||||||
|
|
||||||
|
# Passes on the removed box to the rest of the middleware chain
|
||||||
|
env[:box_removed] = box
|
||||||
end
|
end
|
||||||
|
|
||||||
env[:ui].info(I18n.t("vagrant.commands.box.removing",
|
|
||||||
name: box.name,
|
|
||||||
provider: box.provider,
|
|
||||||
version: box.version))
|
|
||||||
box.destroy!
|
|
||||||
|
|
||||||
# Passes on the removed box to the rest of the middleware chain
|
|
||||||
env[:box_removed] = box
|
|
||||||
|
|
||||||
@app.call(env)
|
@app.call(env)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -27,6 +27,10 @@ module VagrantPlugins
|
||||||
"The specific version of the box to remove") do |v|
|
"The specific version of the box to remove") do |v|
|
||||||
options[:version] = v
|
options[:version] = v
|
||||||
end
|
end
|
||||||
|
|
||||||
|
o.on("--all", "Remove all available versions of the box") do |a|
|
||||||
|
options[:all] = a
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Parse the options
|
# Parse the options
|
||||||
|
@ -50,6 +54,7 @@ module VagrantPlugins
|
||||||
box_provider: options[:provider],
|
box_provider: options[:provider],
|
||||||
box_version: options[:version],
|
box_version: options[:version],
|
||||||
force_confirm_box_remove: options[:force],
|
force_confirm_box_remove: options[:force],
|
||||||
|
box_remove_all_versions: options[:all],
|
||||||
})
|
})
|
||||||
|
|
||||||
# Success, exit status 0
|
# Success, exit status 0
|
||||||
|
|
|
@ -556,8 +556,8 @@ en:
|
||||||
You requested to remove the box '%{name}' with provider
|
You requested to remove the box '%{name}' with provider
|
||||||
'%{provider}'. This box has multiple versions. You must
|
'%{provider}'. This box has multiple versions. You must
|
||||||
explicitly specify which version you want to remove with
|
explicitly specify which version you want to remove with
|
||||||
the `--box-version` flag. The available versions for this
|
the `--box-version` flag or specify the `--all` flag to remove all
|
||||||
box are:
|
versions. The available versions for this box are:
|
||||||
|
|
||||||
%{versions}
|
%{versions}
|
||||||
box_remove_not_found: |-
|
box_remove_not_found: |-
|
||||||
|
|
|
@ -128,13 +128,16 @@ This command removes a box from Vagrant that matches the given name.
|
||||||
|
|
||||||
If a box has multiple providers, the exact provider must be specified
|
If a box has multiple providers, the exact provider must be specified
|
||||||
with the `--provider` flag. If a box has multiple versions, you can select
|
with the `--provider` flag. If a box has multiple versions, you can select
|
||||||
what versions to delete with the `--box-version` flag.
|
what versions to delete with the `--box-version` flag or remove all versions
|
||||||
|
with the `--all` flag.
|
||||||
|
|
||||||
## Options
|
## Options
|
||||||
|
|
||||||
* `--box-version VALUE` - Version of version constraints of the boxes to
|
* `--box-version VALUE` - Version of version constraints of the boxes to
|
||||||
remove. See documentation on this flag for `box add` for more details.
|
remove. See documentation on this flag for `box add` for more details.
|
||||||
|
|
||||||
|
* `--all` - Remove all available versions of a box.
|
||||||
|
|
||||||
* `--force` - Forces removing the box even if an active Vagrant
|
* `--force` - Forces removing the box even if an active Vagrant
|
||||||
environment is using it.
|
environment is using it.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue