From 63dffe3f92ea4c046ee6f17a101938202f5ae95d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 24 Apr 2014 19:54:26 -0700 Subject: [PATCH 1/2] commands/version: check latest version --- plugins/commands/version/command.rb | 60 ++++++++++++++++++++++ plugins/commands/version/plugin.rb | 18 +++++++ templates/locales/en.yml | 17 ++++++ website/docs/source/layouts/layout.erb | 1 + website/docs/source/v2/cli/version.html.md | 15 ++++++ 5 files changed, 111 insertions(+) create mode 100644 plugins/commands/version/command.rb create mode 100644 plugins/commands/version/plugin.rb create mode 100644 website/docs/source/v2/cli/version.html.md diff --git a/plugins/commands/version/command.rb b/plugins/commands/version/command.rb new file mode 100644 index 000000000..0be91d9fd --- /dev/null +++ b/plugins/commands/version/command.rb @@ -0,0 +1,60 @@ +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 diff --git a/plugins/commands/version/plugin.rb b/plugins/commands/version/plugin.rb new file mode 100644 index 000000000..a5498bb86 --- /dev/null +++ b/plugins/commands/version/plugin.rb @@ -0,0 +1,18 @@ +require "vagrant" + +module VagrantPlugins + module CommandVersion + class Plugin < Vagrant.plugin("2") + name "version command" + description <<-DESC + The `version` command prints the currently installed version + as well as the latest available version. + DESC + + command("version") do + require File.expand_path("../command", __FILE__) + Command + end + end + end +end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index f10e84390..4ddbde172 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -223,6 +223,23 @@ en: up some disk space. Press any other key to continue. + version_current: |- + Installed Version: %{version} + version_latest: |- + Latest Version: %{version} + version_latest_installed: |- + You're running an up-to-date version of Vagrant! + version_upgrade_howto: |- + To upgrade to the latest version, visit the downloads page and + download and install the latest version of Vagrant from the URL + below: + + http://www.vagrantup.com/downloads.html + + If you're curious what changed in the latest release, view the + CHANGELOG below: + + https://github.com/mitchellh/vagrant/blob/v%{version}/CHANGELOG.md cfengine_config: classes_array: |- The 'classes' configuration must be an array. diff --git a/website/docs/source/layouts/layout.erb b/website/docs/source/layouts/layout.erb index a44a1afc5..74265d570 100644 --- a/website/docs/source/layouts/layout.erb +++ b/website/docs/source/layouts/layout.erb @@ -123,6 +123,7 @@ >status >suspend >up + >version >More Commands >Machine Readable Output diff --git a/website/docs/source/v2/cli/version.html.md b/website/docs/source/v2/cli/version.html.md new file mode 100644 index 000000000..fe70702c3 --- /dev/null +++ b/website/docs/source/v2/cli/version.html.md @@ -0,0 +1,15 @@ +--- +page_title: "vagrant version - Command-Line Interface" +sidebar_current: "cli-version" +--- + +# Version + +**Command: `vagrant version`** + +This command tells you the version of Vagrant you have installed +as well as the latest version of Vagrant that is currently available. + +In order to determine the latest available Vagrant version, this +command must make a network call. If you only want to see the currently +installed version, use `vagrant --version`. From 6870de3f6db5f3671158efcc6c841d9093135e3a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 24 Apr 2014 20:09:32 -0700 Subject: [PATCH 2/2] core: add Vagrant.latest_version --- lib/vagrant/shared_helpers.rb | 18 ++++++++++++++++++ plugins/commands/version/command.rb | 19 +++++-------------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/vagrant/shared_helpers.rb b/lib/vagrant/shared_helpers.rb index 55d68f200..3fb42c6f6 100644 --- a/lib/vagrant/shared_helpers.rb +++ b/lib/vagrant/shared_helpers.rb @@ -1,4 +1,5 @@ require "pathname" +require "tempfile" module Vagrant # This is the default endpoint of the Vagrant Cloud in @@ -25,6 +26,23 @@ module Vagrant ENV["VAGRANT_INSTALLER_EMBEDDED_DIR"] end + # Returns the latest version of Vagrant that is available. + # + # This makes an HTTP call. + # + # @return [String] + def self.latest_version + # Lazy-require this so that the overhead of this file is low + require "vagrant/util/downloader" + + tf = Tempfile.new("vagrant") + tf.close + url = "http://www.vagrantup.com/latest-version.json" + Vagrant::Util::Downloader.new(url, tf.path).download! + data = JSON.parse(File.read(tf.path)) + data["version"] + end + # This returns whether or not 3rd party plugins should be loaded. # # @return [Boolean] diff --git a/plugins/commands/version/command.rb b/plugins/commands/version/command.rb index 0be91d9fd..100082440 100644 --- a/plugins/commands/version/command.rb +++ b/plugins/commands/version/command.rb @@ -1,8 +1,5 @@ require "json" require "optparse" -require "tempfile" - -require "vagrant/util/downloader" module VagrantPlugins module CommandVersion @@ -25,24 +22,18 @@ module VagrantPlugins "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)) + # Load the latest version + latest = Vagrant.latest_version # Output latest version @env.ui.output(I18n.t( - "vagrant.version_latest", version: data["version"])) - @env.ui.machine("version-latest", data["version"]) + "vagrant.version_latest", version: latest)) + @env.ui.machine("version-latest", latest) # 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"]) + latest = Gem::Version.new(latest) if current >= latest @env.ui.success(" \n" + I18n.t( "vagrant.version_latest_installed"))