From 6e93a13000a9eb604df6d543c5af130c7c11e862 Mon Sep 17 00:00:00 2001 From: Christoph Hartmann Date: Fri, 29 Apr 2016 02:20:25 +0200 Subject: [PATCH] show error if user is not logged in to compliance server --- lib/bundles/inspec-compliance/cli.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/bundles/inspec-compliance/cli.rb b/lib/bundles/inspec-compliance/cli.rb index d229913e5..70ae77f7a 100644 --- a/lib/bundles/inspec-compliance/cli.rb +++ b/lib/bundles/inspec-compliance/cli.rb @@ -58,6 +58,8 @@ module Compliance desc 'profiles', 'list all available profiles in Chef Compliance' def profiles config = Compliance::Configuration.new + return if !loggedin(config) + profiles = Compliance::API.profiles(config) if !profiles.empty? # iterate over profiles @@ -73,6 +75,9 @@ module Compliance desc 'exec PROFILE', 'executes a Chef Compliance profile' exec_options def exec(*tests) + config = Compliance::Configuration.new + return if !loggedin(config) + # iterate over tests and add compliance scheme tests = tests.map { |t| 'compliance://' + t } @@ -84,7 +89,10 @@ module Compliance desc 'upload PATH', 'uploads a local profile to Chef Compliance' option :overwrite, type: :boolean, default: false, desc: 'Overwrite existing profile on Chef Compliance.' - def upload(path) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, PerceivedComplexity + def upload(path) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, PerceivedComplexity, Metrics/CyclomaticComplexity + config = Compliance::Configuration.new + return if !loggedin(config) + unless File.exist?(path) puts "Directory #{path} does not exist." exit 1 @@ -110,7 +118,6 @@ module Compliance end # determine user information - config = Compliance::Configuration.new if config['token'].nil? || config['user'].nil? error.call('Please login via `inspec compliance login`') end @@ -261,6 +268,12 @@ module Compliance [success, msg] end + + def loggedin(config) + serverknown = !config['server'].nil? + puts 'You need to login first with `inspec compliance login`' if !serverknown + serverknown + end end # register the subcommand to Inspec CLI registry