diff --git a/lib/inspec/cli.rb b/lib/inspec/cli.rb index 9b2a1413e..964157462 100644 --- a/lib/inspec/cli.rb +++ b/lib/inspec/cli.rb @@ -9,6 +9,7 @@ require 'thor' require 'json' require 'pp' require 'utils/json_log' +require 'utils/latest_version' require 'inspec/base_cli' require 'inspec/plugins' require 'inspec/runner_mock' @@ -223,6 +224,11 @@ class Inspec::InspecCLI < Inspec::BaseCLI # rubocop:disable Metrics/ClassLength desc 'version', 'prints the version of this tool' def version puts Inspec::VERSION + # display outdated version + latest = LatestInSpecVersion.new.latest + if Gem::Version.new(Inspec::VERSION) < Gem::Version.new(latest) + puts "\nYour version of InSpec is out of date! The latest version is #{latest}." + end end private diff --git a/lib/utils/latest_version.rb b/lib/utils/latest_version.rb new file mode 100644 index 000000000..78659e58d --- /dev/null +++ b/lib/utils/latest_version.rb @@ -0,0 +1,20 @@ +# encoding: utf-8 +# author: Christoph Hartmann + +require 'json' +require 'net/http' + +class LatestInSpecVersion + # fetches the latest version from rubygems server + def latest + uri = URI('https://rubygems.org/api/v1/gems/inspec.json') + res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') {|http| + http.read_timeout = 0.5 + http.get(uri.path) + } + inspec_info = JSON.parse(res.body) + inspec_info['version'] + rescue Exception # rubocop:disable Lint/RescueException + nil + end +end