61 lines
1.7 KiB
Ruby
61 lines
1.7 KiB
Ruby
|
require "json"
|
||
|
require "optparse"
|
||
|
require "tempfile"
|
||
|
|
||
|
require "vagrant/util/downloader"
|
||
|
|
||
|
module VagrantPlugins
|
||
|
module CommandVersion
|
||
|
class Command < Vagrant.plugin("2", :command)
|
||
|
def self.synopsis
|
||
|
"prints current and latest Vagrant version"
|
||
|
end
|
||
|
|
||
|
def execute
|
||
|
opts = OptionParser.new do |o|
|
||
|
o.banner = "Usage: vagrant version"
|
||
|
end
|
||
|
|
||
|
# Parse the options
|
||
|
argv = parse_options(opts)
|
||
|
return if !argv
|
||
|
|
||
|
# Output the currently installed version instantly.
|
||
|
@env.ui.output(I18n.t(
|
||
|
"vagrant.version_current", version: Vagrant::VERSION))
|
||
|
@env.ui.machine("version-installed", Vagrant::VERSION)
|
||
|
|
||
|
# Load the latest installed version to output that.
|
||
|
tf = Tempfile.new("vagrant")
|
||
|
tf.close
|
||
|
url = "http://www.vagrantup.com/latest-version.json"
|
||
|
Vagrant::Util::Downloader.new(url, tf.path).download!
|
||
|
|
||
|
# Parse the JSON result
|
||
|
data = JSON.parse(File.read(tf.path))
|
||
|
|
||
|
# Output latest version
|
||
|
@env.ui.output(I18n.t(
|
||
|
"vagrant.version_latest", version: data["version"]))
|
||
|
@env.ui.machine("version-latest", data["version"])
|
||
|
|
||
|
# Determine if its a new version, and if so, output some more
|
||
|
# information.
|
||
|
current = Gem::Version.new(Vagrant::VERSION)
|
||
|
latest = Gem::Version.new(data["version"])
|
||
|
if current >= latest
|
||
|
@env.ui.success(" \n" + I18n.t(
|
||
|
"vagrant.version_latest_installed"))
|
||
|
return 0
|
||
|
end
|
||
|
|
||
|
# Out of date! Let the user know how to upgrade.
|
||
|
@env.ui.output(" \n" + I18n.t(
|
||
|
"vagrant.version_upgrade_howto", version: latest.to_s))
|
||
|
|
||
|
0
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|