Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rubocop cleanup #49

Merged
merged 57 commits into from
Feb 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
1272343
(rubocop) Fix Style/StringLiterals
rodjek Feb 2, 2019
1cf00a5
(rubocop) Fix Style/ZeroLengthPredicate
rodjek Feb 2, 2019
02e49e5
(rubocop) Fix Style/ColonMethodCall
rodjek Feb 2, 2019
e0cbf89
(rubocop) Fix Style/FrozenStringLiteralComment
rodjek Feb 2, 2019
0d95977
(rubocop) Disable Style/Documentation for now
rodjek Feb 2, 2019
42014ae
(rubocop) Fix Style/RedundantReturn
rodjek Feb 2, 2019
fcf7154
(rubocop) Style/HashSyntax to use hash_rockets for consistency
rodjek Feb 3, 2019
874a6e7
(rubocop) Fix Style/ClassCheck
rodjek Feb 3, 2019
79f7645
(rubocop) Fix Style/AndOr
rodjek Feb 3, 2019
d95c694
(rubocop) Fix Style/BracesAroundHashParameters
rodjek Feb 3, 2019
c7c8e48
(rubocop) Fix Style/ExpandPathArguments
rodjek Feb 3, 2019
7bafee3
(rubocop) Fix Style/IfUnlessModifier
rodjek Feb 3, 2019
12c1795
(rubocop) Fix Style/RescueStandardError
rodjek Feb 3, 2019
5ad2130
(rubocop) Fix Style/StringLiteralsInInterpolation
rodjek Feb 3, 2019
d25732b
(rubocop) Fix Style/NegatedIf
rodjek Feb 3, 2019
397bbd4
(rubocop) Fix Style/RedundantFreeze
rodjek Feb 3, 2019
cdb9b0c
(rubocop) Fix Style/NestedParenthesizedCalls
rodjek Feb 3, 2019
095ac9e
(rubocop) Fix Style/WordArray
rodjek Feb 3, 2019
1aea79a
(rubocop) Fix Style/SymbolArray
rodjek Feb 3, 2019
58f64b2
(rubocop) Fix Style/TrailingCommaInHashLiteral
rodjek Feb 3, 2019
0e5dd9b
(rubocop) Fix Style/TrailingCommaInArrayLiteral
rodjek Feb 3, 2019
692577a
(rubocop) Fix Style/TrailingCommaInArguments
rodjek Feb 3, 2019
613121f
(rubocop) Fix Style/BlockDelimiters
rodjek Feb 3, 2019
dec621e
(rubocop) Fix Style/ConditionalAssignment
rodjek Feb 3, 2019
6c4fe83
(rubocop) Fix Style/IfInsideElse
rodjek Feb 3, 2019
b7b08c9
(rubocop) Fix Style/MissingRespondToMissing
rodjek Feb 3, 2019
6a771a8
(rubocop) Fix Style/GuardClause
rodjek Feb 3, 2019
b16e3fc
(rubocop) Fix Layout/SpaceAroundOperators
rodjek Feb 3, 2019
7cd0256
(rubocop) Fix Layout/SpaceInsideBlockBraces
rodjek Feb 3, 2019
eb0d312
(rubocop) Fix Layout/SpaceAroundEqualsInParameterDefault
rodjek Feb 3, 2019
b8971c0
(rubocop) Fix Layout/SpaceInsideLiteralHashBraces
rodjek Feb 3, 2019
fd753ba
(rubocop) Fix Layout/SpaceBeforeBlockBraces
rodjek Feb 3, 2019
6f40397
(rubocop) Fix Layout/DotPosition
rodjek Feb 3, 2019
b2ac1dd
(rubocop) Fix Layout/MultilineMethodCallIndentation
rodjek Feb 3, 2019
edaa3e5
(rubocop) Fix Layout/EmptyLinesAroundBlockBody
rodjek Feb 3, 2019
bb9e821
(rubocop) Fix Layout/EmptyLinesAroundClassBody
rodjek Feb 3, 2019
e6d1fbf
(rubocop) Fix Layout/TrailingBlankLines
rodjek Feb 3, 2019
e8f420a
(rubocop) Fix Layout/EmptyLineAfterGuardClause
rodjek Feb 3, 2019
cecea8b
(rubocop) Fix Layout/SpaceAfterNot
rodjek Feb 3, 2019
9afae18
(rubocop) Fix Layout/SpaceAfterComma
rodjek Feb 3, 2019
f2ab052
(rubocop) Fix Layout/ExtraSpacing
rodjek Feb 3, 2019
f2167f8
(rubocop) Fix Layout/CaseIndentation
rodjek Feb 3, 2019
851009b
(rubocop) Fix Layout/AlignHash & Layout/IndentHash
rodjek Feb 3, 2019
7d9545f
(rubocop) Fix Layout/IndentHeredoc
rodjek Feb 3, 2019
c78ad33
(rubocop) Fix Layout/LeadingCommentSpace
rodjek Feb 3, 2019
c97af3f
(rubocop) Fix Performance/RegexpMatch
rodjek Feb 3, 2019
2a05f0d
(rubocop) Fix Lint/ScriptPermission
rodjek Feb 3, 2019
f0863e7
(rubocop) Fix Lint/UnusedMethodArgument
rodjek Feb 3, 2019
63eb49c
(rubocop) Fix Lint/UnusedBlockArgument
rodjek Feb 3, 2019
66993b4
(rubocop) Fix Gemspec/OrderedDependencies
rodjek Feb 3, 2019
e1379a8
(rubocop) Fix Naming/PredicateName
rodjek Feb 3, 2019
e50d4b3
(rubocop) Fix Naming/HeredocDelimiterNaming
rodjek Feb 3, 2019
af6dc74
(rubocop) Fix Naming/UncommunicativeMethodParamName
rodjek Feb 3, 2019
426e9ce
(rubocop) Fix Style/ConditionalAssignment
rodjek Feb 3, 2019
6e1f626
(rubocop) Fix Style/IfUnlessModifier
rodjek Feb 3, 2019
893f936
(rubocop) Defer Metrics cop changes for now
rodjek Feb 3, 2019
4440164
Run rubocop during CI
rodjek Feb 3, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
inherit_from: .rubocop_todo.yml

AllCops:
TargetRubyVersion: 2.4

Style/Documentation:
Enabled: False
Style/HashSyntax:
EnforcedStyle: hash_rockets
Style/TrailingCommaInHashLiteral:
EnforcedStyleForMultiline: comma
Style/TrailingCommaInArrayLiteral:
EnforcedStyleForMultiline: comma
Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: comma

Layout/AlignHash:
EnforcedHashRocketStyle: table
Layout/IndentHash:
EnforcedStyle: consistent
46 changes: 46 additions & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2019-02-03 15:31:20 +1100 using RuboCop version 0.63.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 8
Metrics/AbcSize:
Max: 319

# Offense count: 25
# Configuration parameters: CountComments, ExcludedMethods.
# ExcludedMethods: refine
Metrics/BlockLength:
Max: 278

# Offense count: 1
# Configuration parameters: CountBlocks.
Metrics/BlockNesting:
Max: 4

# Offense count: 4
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 394

# Offense count: 4
Metrics/CyclomaticComplexity:
Max: 55

# Offense count: 13
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/MethodLength:
Max: 391

# Offense count: 3
Metrics/PerceivedComplexity:
Max: 63

# Offense count: 193
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
# URISchemes: http, https
Metrics/LineLength:
Max: 285
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ sudo: false
language: ruby
rvm:
- 2.4
script: rspec spec
script: bundle exec rake rubocop spec
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec

gem 'rake', require: false
gem 'rake', :require => false

group :test do
gem 'rspec', '~> 3.5.0'
Expand Down
6 changes: 4 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'rubygems'
require 'bundler/setup'
require 'rspec/core/rake_task'
Expand All @@ -9,7 +11,7 @@ $stdout.sync = true
$stderr.sync = true

# Change to the directory of this file.
Dir.chdir(File.expand_path('../', __FILE__))
Dir.chdir(File.expand_path(__dir__))

# This installs the tasks that help with gem creation and
# publishing.
Expand All @@ -26,4 +28,4 @@ RuboCop::RakeTask.new(:rubocop) do |task|
end

# Default task is to run the unit tests
task default: :spec
task :default => :spec
3 changes: 2 additions & 1 deletion bin/floaty
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
$LOAD_PATH.unshift(File.expand_path('../lib', __dir__))

require 'vmfloaty'

Expand Down
88 changes: 40 additions & 48 deletions lib/vmfloaty.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'rubygems'
require 'commander'
Expand Down Expand Up @@ -43,22 +43,22 @@ def run
force = options.force

if args.empty?
STDERR.puts "No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs."
STDERR.puts 'No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs.'
exit 1
end

os_types = Utils.generate_os_hash(args)

max_pool_request = 5
large_pool_requests = os_types.select{|_,v| v > max_pool_request}
if ! large_pool_requests.empty? and ! force
large_pool_requests = os_types.select { |_, v| v > max_pool_request }
if !large_pool_requests.empty? && !force
STDERR.puts "Requesting vms over #{max_pool_request} requires a --force flag."
STDERR.puts "Try again with `floaty get --force`"
STDERR.puts 'Try again with `floaty get --force`'
exit 1
end

if os_types.empty?
STDERR.puts "No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs."
STDERR.puts 'No operating systems provided to obtain. See `floaty get --help` for more information on how to get VMs.'
exit 1
end

Expand Down Expand Up @@ -143,18 +143,18 @@ def run
hostname = args[0]
modify_all = options.all

if hostname.nil? and !modify_all
STDERR.puts "ERROR: Provide a hostname or specify --all."
if hostname.nil? && !modify_all
STDERR.puts 'ERROR: Provide a hostname or specify --all.'
exit 1
end
running_vms = modify_all ? service.list_active(verbose) : hostname.split(",")
running_vms = modify_all ? service.list_active(verbose) : hostname.split(',')

tags = options.tags ? JSON.parse(options.tags) : nil
modify_hash = {
lifetime: options.lifetime,
disk: options.disk,
tags: tags,
reason: options.reason
:lifetime => options.lifetime,
:disk => options.disk,
:tags => tags,
:reason => options.reason,
}
modify_hash.delete_if { |_, value| value.nil? }

Expand All @@ -171,11 +171,11 @@ def run
end
if ok
if modify_all
puts "Successfully modified all VMs."
puts 'Successfully modified all VMs.'
else
puts "Successfully modified VM #{hostname}."
end
puts "Use `floaty list --active` to see the results."
puts 'Use `floaty list --active` to see the results.'
end
end
end
Expand Down Expand Up @@ -205,15 +205,13 @@ def run
if delete_all
running_vms = service.list_active(verbose)
if running_vms.empty?
STDERR.puts "You have no running VMs."
STDERR.puts 'You have no running VMs.'
else
Utils.pretty_print_hosts(verbose, service, running_vms)
# Confirm deletion
puts
confirmed = true
unless force
confirmed = agree('Delete all these VMs? [y/N]')
end
confirmed = agree('Delete all these VMs? [y/N]') unless force
if confirmed
response = service.delete(verbose, running_vms)
response.each do |hostname, result|
Expand All @@ -236,7 +234,7 @@ def run
end
end
else
STDERR.puts "You did not provide any hosts to delete"
STDERR.puts 'You did not provide any hosts to delete'
exit 1
end

Expand Down Expand Up @@ -302,9 +300,7 @@ def run
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)
Expand Down Expand Up @@ -372,25 +368,23 @@ def run

begin
case action
when 'get'
token = service.get_new_token(verbose)
puts token
when 'delete'
result = service.delete_token(verbose, options.token)
puts result
when 'status'
token_value = options.token
if token_value.nil?
token_value = args[1]
end
status = service.token_status(verbose, token_value)
puts status
when nil
STDERR.puts 'No action provided'
exit 1
else
STDERR.puts "Unknown action: #{action}"
exit 1
when 'get'
token = service.get_new_token(verbose)
puts token
when 'delete'
result = service.delete_token(verbose, options.token)
puts result
when 'status'
token_value = options.token
token_value = args[1] if token_value.nil?
status = service.token_status(verbose, token_value)
puts status
when nil
STDERR.puts 'No action provided'
exit 1
else
STDERR.puts "Unknown action: #{action}"
exit 1
end
rescue TokenError => e
STDERR.puts e
Expand All @@ -417,15 +411,13 @@ def run
use_token = !options.notoken

if args.empty?
STDERR.puts "No operating systems provided to obtain. See `floaty ssh --help` for more information on how to get VMs."
STDERR.puts 'No operating systems provided to obtain. See `floaty ssh --help` for more information on how to get VMs.'
exit 1
end

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
Expand All @@ -435,13 +427,13 @@ def run
command :completion do |c|
c.syntax = 'floaty completion [options]'
c.summary = 'Outputs path to completion script'
c.description = Utils.strip_heredoc(<<-EOF)
c.description = Utils.strip_heredoc(<<-DESCRIPTION)
Outputs path to a completion script for the specified shell (or 'bash' if not specified). This makes it easy to add the completion script to your profile:

source $(floaty completion --shell bash)

This subcommand will exit non-zero with an error message if no completion script is available for the requested shell.
EOF
DESCRIPTION
c.example 'Gets path to bash tab completion script', 'floaty completion --shell bash'
c.option '--shell STRING', String, 'Shell to request completion script for'
c.action do |_, options|
Expand Down
36 changes: 14 additions & 22 deletions lib/vmfloaty/auth.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'faraday'
require 'json'
require 'vmfloaty/http'
Expand All @@ -7,46 +9,36 @@ class Auth
def self.get_token(verbose, url, user, password)
conn = Http.get_conn_with_auth(verbose, url, user, password)

resp = conn.post "token"
resp = conn.post 'token'

res_body = JSON.parse(resp.body)
if res_body["ok"]
return res_body["token"]
else
raise TokenError, "HTTP #{resp.status}: There was a problem requesting a token:\n#{res_body}"
end
return res_body['token'] if res_body['ok']

raise TokenError, "HTTP #{resp.status}: There was a problem requesting a token:\n#{res_body}"
end

def self.delete_token(verbose, url, user, password, token)
if token.nil?
raise TokenError, 'You did not provide a token'
end
raise TokenError, 'You did not provide a token' if token.nil?

conn = Http.get_conn_with_auth(verbose, url, user, password)

response = conn.delete "token/#{token}"
res_body = JSON.parse(response.body)
if res_body["ok"]
return res_body
else
raise TokenError, "HTTP #{response.status}: There was a problem deleting a token:\n#{res_body}"
end
return res_body if res_body['ok']

raise TokenError, "HTTP #{response.status}: There was a problem deleting a token:\n#{res_body}"
end

def self.token_status(verbose, url, token)
if token.nil?
raise TokenError, 'You did not provide a token'
end
raise TokenError, 'You did not provide a token' if token.nil?

conn = Http.get_conn(verbose, url)

response = conn.get "token/#{token}"
res_body = JSON.parse(response.body)

if res_body["ok"]
return res_body
else
raise TokenError, "HTTP #{response.status}: There was a problem getting the status of a token:\n#{res_body}"
end
return res_body if res_body['ok']

raise TokenError, "HTTP #{response.status}: There was a problem getting the status of a token:\n#{res_body}"
end
end
5 changes: 3 additions & 2 deletions lib/vmfloaty/conf.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# frozen_string_literal: true

require 'yaml'

class Conf

def self.read_config
conf = {}
begin
conf = YAML.load_file("#{Dir.home}/.vmfloaty.yml")
rescue
rescue StandardError
STDERR.puts "WARNING: There was no config file at #{Dir.home}/.vmfloaty.yml"
end
conf
Expand Down
Loading