Skip to content

Commit 2364a26

Browse files
committed
v0.1.5
2 parents 71e931a + 1a3e15b commit 2364a26

26 files changed

+469
-229
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
language: ruby
22
rvm:
33
- 1.9.3
4+
- 2.0.0
45
- 2.1.2
56
deploy:
67
provider: rubygems

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
Version 0.1.5
2+
---
3+
* Add configuration file ([Vlad - f928415](https://github.com/vghn/vscripts/commit/f928415a19c4f300566721fcffd14877497de6f1))
4+
* Add testing for Ruby 2.0.0 ([Vlad - ba19d81](https://github.com/vghn/vscripts/commit/ba19d817194fe1395ce5d2b3884552004e5e07ef))
5+
* Fix command line options ([Vlad - cc6e8ad](https://github.com/vghn/vscripts/commit/cc6e8ad6ee77218cfe4cb8ae80af57a291d36ddb))
6+
* Fix test for Ruby 2.0 ([Vlad - 2d32474](https://github.com/vghn/vscripts/commit/2d32474afcd2f1122d0d9deebc4e5c0bae700ff5))
7+
* Fix smells ([Vlad - 23283a9](https://github.com/vghn/vscripts/commit/23283a941d356e185364d83832d88b15919c2bf7))
8+
* Minor code improvements ([Vlad - b9091e5](https://github.com/vghn/vscripts/commit/b9091e5fbd6a50679c214d4d7db9539965d727e6))
9+
110
Version 0.1.4
211
---
312
* Switch back to previous branch on deployment ([Vlad - 244ac9a](https://github.com/vghn/vscripts/commit/244ac9a6d810abd63a625c88bc0a2efac19a9993))

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ Automation daemon.
1616
### Gem installation
1717
`gem install vscripts`
1818

19+
## Configuration
20+
21+
VScripts looks for a configuration file in the following locations(in order):
22+
- a file specified in the `--config` command line option
23+
- `.vscripts.yml` in the $HOME directory
24+
- `/etc/vscripts/config.yml`
1925

2026
## Usage
2127

@@ -26,8 +32,9 @@ vscripts GLOBAL-OPTIONS COMMAND OPTIONS
2632

2733
### Global Options
2834
```
29-
-h|--help: Displays VScripts help.
30-
-v|--version: Displays the version number.
35+
--config, -c <s>: Specify configuration file
36+
--help, -h : Displays VScripts help
37+
--version, -v : Displays the version number
3138
```
3239

3340

bin/vscripts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env ruby
22

33
require 'vscripts'
4-
VScripts.run(ARGV)
4+
VScripts.run

lib/vscripts.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
require 'vscripts/commands'
22
require 'vscripts/command_line'
3+
require 'vscripts/config'
34

45
# Main VScripts module
56
module VScripts
7+
# Reads the command line arguments
8+
def self.cli
9+
@cli ||= CommandLine.new # Parses command line
10+
end
11+
12+
# Reads the configuration files
13+
def self.config
14+
@config ||= VScripts::Config.new(cli.global.config) # Parses configuration
15+
end
16+
617
# Reads the arguments and runs the given command
7-
def self.run(argv)
8-
cli = CommandLine.new(argv)
9-
command = VScripts::Commands.const_get(cli.command).new(cli.arguments)
10-
command.execute
18+
def self.run
19+
VScripts::Commands.const_get(cli.command).new(cli.command_options).execute
1120
end
1221
end # module VScripts

lib/vscripts/aws/ec2.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,24 @@ def create_tag(resource, key, value)
3838
ec2.tags.create(resource, key, value)
3939
end
4040

41+
# @return [Hash] a correct Hash object with all tags and keys
42+
def all_tags_hash
43+
hash = {}
44+
all_tags.each do |key, value|
45+
hash = hash.merge(Hash[key, value])
46+
end
47+
hash
48+
end
49+
4150
# Exclude tags
4251
# @param list [Array] the list of tag keys to be excluded
4352
# @return [Hash] the filtered tags
4453
def tags_without(list = [])
45-
all_tags.each_with_object({}) do |tag, hash|
46-
key, value = tag[0], tag[1]
47-
hash[key] = value unless list.include? key
48-
hash
54+
hash = all_tags_hash
55+
list.each do |key|
56+
hash.delete(key)
4957
end
58+
hash
5059
end
5160

5261
# Looks for the value of the 'Name' tag for the given instance

lib/vscripts/command_line.rb

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,63 +5,67 @@
55
module VScripts
66
# Parses the command line arguments
77
class CommandLine
8-
# @return [Array] all command line arguments
9-
attr_accessor :arguments
108
# @return [Hash] the global command line arguments.
119
attr_reader :global
1210
# @return [String] the command name
1311
attr_reader :command
12+
# @return [Array] the command specific arguments.
13+
attr_reader :command_options
1414

15-
# Builds command line arguments
16-
def initialize(argv = [])
17-
@arguments ||= argv
18-
@global ||= parse_cli_options
19-
@command ||= verify_command
15+
# @param args [Array] the command line arguments
16+
# @return [Hash] the command line options
17+
def initialize(args = ARGV)
18+
parse_global(args)
2019
end
2120

2221
# Specifies command line options
2322
# This method smells of :reek:NestedIterators but ignores them
2423
# This method smells of :reek:TooManyStatements but ignores them
2524
def parser # rubocop:disable MethodLength
2625
available = Commands.list.map { |cmd| cmd.to_s.downcase }
27-
@parser ||= Trollop::Parser.new do
26+
Trollop::Parser.new do
2827
version VScripts::VERSION::COPYRIGHT
2928
banner <<-EOS
3029
VScripts automation daemon.
3130
3231
Available commands:
3332
#{available}
3433
35-
Usage:
34+
USAGE:
3635
vscripts GLOBAL-OPTIONS COMMAND OPTIONS
3736
3837
For help on an individual command:
3938
vscripts COMMAND --help
4039
41-
Global Options:
40+
GLOBAL OPTIONS:
4241
EOS
42+
opt :config, 'Specify configuration file',
43+
type: :string, short: '-c'
4344
stop_on available
45+
stop_on_unknown
4446
end
4547
end
4648

47-
# @return [Hash] the command line arguments
48-
def parse_cli_options
49+
# @param args [Array] the command line arguments
50+
# @return [Hash] the command line options
51+
def parse_global(args)
4952
Trollop.with_standard_exception_handling parser do
50-
fail Trollop::HelpNeeded if arguments.empty?
51-
parser.parse arguments
53+
@global = parser.parse args
54+
fail Trollop::HelpNeeded if args.empty? || !parse_command(args)
5255
end
56+
@global
5357
end
5458

5559
# Ensures command is available
5660
# @return [String] the command name
57-
def verify_command
58-
command_cli = arguments.shift
59-
command_cls = command_cli.capitalize.to_sym
60-
if Commands.list.include?(command_cls)
61-
return command_cls
62-
else
63-
abort "Error: unknown subcommand '#{command_cli}'\nTry --help."
64-
end
61+
# @return [Array] the command specific arguments
62+
def parse_command(args)
63+
command = args.shift
64+
return unless command
65+
command_class = command.capitalize.to_sym
66+
abort "Error: unknown subcommand '#{command}'\nTry --help." \
67+
unless Commands.list.include?(command_class)
68+
@command, @command_options = [command_class, args]
6569
end
6670
end # class CommandLine
6771
end # module VScripts

lib/vscripts/config.rb

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
require 'yaml'
2+
3+
module VScripts
4+
# VScripts Configuration
5+
class Config
6+
# User's configuration file
7+
DOT_FILE = "#{File.expand_path('~')}/.vscripts.yml"
8+
# Global configuration file
9+
SYSTEM_CONFIG_FILE = '/etc/vscripts/config.yml'
10+
# Global defaults
11+
GLOBAL_DEFAULTS = {}
12+
13+
# @return [Hash] all configuration options
14+
attr_reader :get
15+
16+
# Loads class
17+
# @param cfg_file [String] the path to the configuration file
18+
# @return [Hash] the configuration options
19+
def initialize(config_file = nil)
20+
@file = config_file
21+
@get = GLOBAL_DEFAULTS.merge(options)
22+
end
23+
24+
# Parses the configuration files in order
25+
# @return [Hash] the first configuration hash found
26+
def options
27+
parse(@file) ||
28+
parse(DOT_FILE) ||
29+
parse(SYSTEM_CONFIG_FILE) ||
30+
{}
31+
end
32+
33+
# Parses the configuration
34+
# @param file [String] the path to the configuration file
35+
# @return [Hash] the configuration hash
36+
def parse(file)
37+
YAML.load(read(file)) if check_config(file)
38+
end
39+
40+
# @param (see #parse)
41+
# @return [String] the contents of the file
42+
def read(file)
43+
File.read(file)
44+
end
45+
46+
# @param (see #parse)
47+
# @return [Boolean] true if the file exists
48+
def check_config(file)
49+
file && File.exist?(file)
50+
end
51+
end # class Config
52+
end # module VScripts

lib/vscripts/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module VScripts
22
# This module holds the version information.
33
module VERSION
44
# @return [String] the version number
5-
STRING = '0.1.4'
5+
STRING = '0.1.5'
66

77
# @return [String] the semantic version number
88
MAJOR, MINOR, PATCH = STRING.split('.').map(&:to_i)

spec/aws_spec_helper.rb

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)