commands/box: Introduce a StateFile for keeping track of downloaded boxes

This commit is contained in:
Fabio Rehm 2013-10-04 21:28:05 -03:00
parent 670a441a99
commit 17fd5f9e4e
3 changed files with 42 additions and 0 deletions

View File

@ -40,6 +40,7 @@ module VagrantPlugins
:box_url => argv[1],
:box_force => options[:force],
:box_download_insecure => options[:insecure],
:box_state_file => StateFile.new(@env.home_path.join('boxes.json'))
})
# Success, exit status 0

View File

@ -11,5 +11,7 @@ module VagrantPlugins
Command::Root
end
end
autoload :StateFile, File.expand_path("../state_file", __FILE__)
end
end

View File

@ -0,0 +1,39 @@
require "json"
module VagrantPlugins
module CommandBox
# This is a helper to deal with the boxes state file that Vagrant
# uses to track the boxes that have been downloaded.
class StateFile
def initialize(path)
@path = path
@data = {}
@data = JSON.parse(@path.read) if @path.exist?
@data["boxes"] ||= {}
end
# Add a downloaded box to the state file.
#
# @param [Box] box The Box object that was added
# @param [String] url The URL from where the box was downloaded
def add_box(box, url)
box_key = "#{box.name}-#{box.provider}"
@data["boxes"][box_key] = {
"url" => url,
"downloaded_at" => Time.now.utc.to_s
}
save!
end
# This saves the state back into the state file.
def save!
@path.open("w+") do |f|
f.write(JSON.dump(@data))
end
end
end
end
end