mirror of
https://github.com/inspec/inspec
synced 2024-11-23 13:13:22 +00:00
display if inspec version is outdated
Signed-off-by: Christoph Hartmann <chris@lollyrock.com>
This commit is contained in:
parent
c193a38f69
commit
f0cdad800f
2 changed files with 26 additions and 0 deletions
|
@ -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
|
||||
|
|
20
lib/utils/latest_version.rb
Normal file
20
lib/utils/latest_version.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue