From 6e1f626d7b16ccc387e8fe6ba2af4eb88ae45985 Mon Sep 17 00:00:00 2001 From: Tim Sharpe Date: Sun, 3 Feb 2019 15:40:17 +1100 Subject: [PATCH] (rubocop) Fix Style/IfUnlessModifier --- lib/vmfloaty.rb | 8 ++----- lib/vmfloaty/nonstandard_pooler.rb | 16 ++++--------- lib/vmfloaty/pooler.rb | 36 ++++++++---------------------- lib/vmfloaty/utils.rb | 16 ++++--------- 4 files changed, 19 insertions(+), 57 deletions(-) diff --git a/lib/vmfloaty.rb b/lib/vmfloaty.rb index c2170a3..dfe390a 100644 --- a/lib/vmfloaty.rb +++ b/lib/vmfloaty.rb @@ -300,9 +300,7 @@ class Vmfloaty hostname = args[0] snapshot_sha = args[1] || options.snapshot - if args[1] && options.snapshot - STDERR.puts "Two snapshot arguments were given....using snapshot #{snapshot_sha}" - end + STDERR.puts "Two snapshot arguments were given....using snapshot #{snapshot_sha}" if args[1] && options.snapshot begin revert_req = service.revert(verbose, hostname, snapshot_sha) @@ -419,9 +417,7 @@ class Vmfloaty host_os = args.first - if args.length > 1 - STDERR.puts "Can't ssh to multiple hosts; Using #{host_os} only..." - end + STDERR.puts "Can't ssh to multiple hosts; Using #{host_os} only..." if args.length > 1 service.ssh(verbose, host_os, use_token) exit 0 diff --git a/lib/vmfloaty/nonstandard_pooler.rb b/lib/vmfloaty/nonstandard_pooler.rb index f504686..f5451b7 100644 --- a/lib/vmfloaty/nonstandard_pooler.rb +++ b/lib/vmfloaty/nonstandard_pooler.rb @@ -27,9 +27,7 @@ class NonstandardPooler conn.headers['X-AUTH-TOKEN'] = token if token os_string = os_type.map { |os, num| Array(os) * num }.flatten.join('+') - if os_string.empty? - raise MissingParamError, 'No operating systems provided to obtain.' - end + raise MissingParamError, 'No operating systems provided to obtain.' if os_string.empty? response = conn.post "host/#{os_string}" @@ -45,14 +43,10 @@ class NonstandardPooler end def self.modify(verbose, url, hostname, token, modify_hash) - if token.nil? - raise TokenError, 'Token provided was nil; Request cannot be made to modify VM' - end + raise TokenError, 'Token provided was nil; Request cannot be made to modify VM' if token.nil? modify_hash.each do |key, _value| - unless %i[reason reserved_for_reason].include? key - raise ModifyError, "Configured service type does not support modification of #{key}" - end + raise ModifyError, "Configured service type does not support modification of #{key}" unless %i[reason reserved_for_reason].include? key end if modify_hash[:reason] @@ -84,9 +78,7 @@ class NonstandardPooler end def self.delete(verbose, url, hosts, token) - if token.nil? - raise TokenError, 'Token provided was nil; Request cannot be made to delete VM' - end + raise TokenError, 'Token provided was nil; Request cannot be made to delete VM' if token.nil? conn = Http.get_conn(verbose, url) diff --git a/lib/vmfloaty/pooler.rb b/lib/vmfloaty/pooler.rb index 8eceb8c..f8d8633 100644 --- a/lib/vmfloaty/pooler.rb +++ b/lib/vmfloaty/pooler.rb @@ -24,9 +24,7 @@ class Pooler def self.list_active(verbose, url, token) status = Auth.token_status(verbose, url, token) vms = [] - if status[token] && status[token]['vms'] - vms = status[token]['vms']['running'] - end + vms = status[token]['vms']['running'] if status[token] && status[token]['vms'] vms end @@ -38,9 +36,7 @@ class Pooler conn.headers['X-AUTH-TOKEN'] = token if token os_string = os_type.map { |os, num| Array(os) * num }.flatten.join('+') - if os_string.empty? - raise MissingParamError, 'No operating systems provided to obtain.' - end + raise MissingParamError, 'No operating systems provided to obtain.' if os_string.empty? response = conn.post "vm/#{os_string}" @@ -56,14 +52,10 @@ class Pooler end def self.modify(verbose, url, hostname, token, modify_hash) - if token.nil? - raise TokenError, 'Token provided was nil. Request cannot be made to modify vm' - end + raise TokenError, 'Token provided was nil. Request cannot be made to modify vm' if token.nil? modify_hash.keys.each do |key| - unless %i[tags lifetime disk].include? key - raise ModifyError, "Configured service type does not support modification of #{key}." - end + raise ModifyError, "Configured service type does not support modification of #{key}." unless %i[tags lifetime disk].include? key end conn = Http.get_conn(verbose, url) @@ -84,9 +76,7 @@ class Pooler end def self.disk(verbose, url, hostname, token, disk) - if token.nil? - raise TokenError, 'Token provided was nil. Request cannot be made to modify vm' - end + raise TokenError, 'Token provided was nil. Request cannot be made to modify vm' if token.nil? conn = Http.get_conn(verbose, url) conn.headers['X-AUTH-TOKEN'] = token @@ -98,9 +88,7 @@ class Pooler end def self.delete(verbose, url, hosts, token) - if token.nil? - raise TokenError, 'Token provided was nil. Request cannot be made to delete vm' - end + raise TokenError, 'Token provided was nil. Request cannot be made to delete vm' if token.nil? conn = Http.get_conn(verbose, url) @@ -143,9 +131,7 @@ class Pooler end def self.snapshot(verbose, url, hostname, token) - if token.nil? - raise TokenError, 'Token provided was nil. Request cannot be made to snapshot vm' - end + raise TokenError, 'Token provided was nil. Request cannot be made to snapshot vm' if token.nil? conn = Http.get_conn(verbose, url) conn.headers['X-AUTH-TOKEN'] = token @@ -156,16 +142,12 @@ class Pooler end def self.revert(verbose, url, hostname, token, snapshot_sha) - if token.nil? - raise TokenError, 'Token provided was nil. Request cannot be made to revert vm' - end + raise TokenError, 'Token provided was nil. Request cannot be made to revert vm' if token.nil? conn = Http.get_conn(verbose, url) conn.headers['X-AUTH-TOKEN'] = token - if snapshot_sha.nil? - raise "Snapshot SHA provided was nil, could not revert #{hostname}" - end + raise "Snapshot SHA provided was nil, could not revert #{hostname}" if snapshot_sha.nil? response = conn.post "vm/#{hostname}/snapshot/#{snapshot_sha}" res_body = JSON.parse(response.body) diff --git a/lib/vmfloaty/utils.rb b/lib/vmfloaty/utils.rb index 3f2d433..b9f5a9c 100644 --- a/lib/vmfloaty/utils.rb +++ b/lib/vmfloaty/utils.rb @@ -30,9 +30,7 @@ class Utils # } # } - unless response_body.delete('ok') - raise ArgumentError, "Bad GET response passed to format_hosts: #{response_body.to_json}" - end + raise ArgumentError, "Bad GET response passed to format_hosts: #{response_body.to_json}" unless response_body.delete('ok') # vmpooler reports the domain separately from the hostname domain = response_body.delete('domain') @@ -81,18 +79,14 @@ class Utils case service.type when 'Pooler' tag_pairs = [] - unless host_data['tags'].nil? - tag_pairs = host_data['tags'].map { |key, value| "#{key}: #{value}" } - end + tag_pairs = host_data['tags'].map { |key, value| "#{key}: #{value}" } unless host_data['tags'].nil? duration = "#{host_data['running']}/#{host_data['lifetime']} hours" metadata = [host_data['template'], duration, *tag_pairs] puts "- #{hostname}.#{host_data['domain']} (#{metadata.join(', ')})" when 'NonstandardPooler' line = "- #{host_data['fqdn']} (#{host_data['os_triple']}" line += ", #{host_data['hours_left_on_reservation']}h remaining" - unless host_data['reserved_for_reason'].empty? - line += ", reason: #{host_data['reserved_for_reason']}" - end + line += ", reason: #{host_data['reserved_for_reason']}" unless host_data['reserved_for_reason'].empty? line += ')' puts line else @@ -185,9 +179,7 @@ class Utils service_config.merge! values else # If the user provided a service name at the command line, use that service if posible, or fail - unless config['services'][options.service] - raise ArgumentError, "Could not find a configured service named '#{options.service}' in ~/.vmfloaty.yml" - end + raise ArgumentError, "Could not find a configured service named '#{options.service}' in ~/.vmfloaty.yml" unless config['services'][options.service] # If the service is configured but some values are missing, use the top-level defaults to fill them in service_config.merge! config['services'][options.service]