antsy 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.md +25 -0
- data/Rakefile +8 -0
- data/antsy.gemspec +25 -0
- data/lib/antsy/version.rb +3 -0
- data/lib/antsy.rb +34 -0
- data/spec/antsy_spec.rb +29 -0
- data/spec/spec_helper.rb +45 -0
- metadata +69 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.gem
|
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# antsy
|
2
|
+
|
3
|
+
A gem to make writing ansible modules in ruby more pleasant.
|
4
|
+
|
5
|
+
# installation
|
6
|
+
|
7
|
+
gem install antsy
|
8
|
+
|
9
|
+
# usage
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
require 'antsy'
|
13
|
+
args = Antsy.args
|
14
|
+
|
15
|
+
Antsy.fail! "must provide foo argument" unless args[:foo]
|
16
|
+
|
17
|
+
# do stuff with args
|
18
|
+
# ...
|
19
|
+
|
20
|
+
if something was updated
|
21
|
+
Antsy.changed!
|
22
|
+
else
|
23
|
+
Antsy.no_change!
|
24
|
+
end
|
25
|
+
```
|
data/Rakefile
ADDED
data/antsy.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib/', __FILE__)
|
3
|
+
$:.unshift lib unless $:.include?(lib)
|
4
|
+
require 'antsy/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = 'antsy'
|
8
|
+
s.version = Antsy::VERSION
|
9
|
+
s.platform = Gem::Platform::RUBY
|
10
|
+
s.authors = [ 'Tim Miller' ]
|
11
|
+
s.email = [ '' ]
|
12
|
+
s.homepage = 'https://github.com/echohead/ansible-module'
|
13
|
+
s.summary = %q{utility functions for ansible modules}
|
14
|
+
s.description = %q{utility functions for ansible modules}
|
15
|
+
|
16
|
+
s.required_ruby_version = ">= 1.8.7"
|
17
|
+
s.required_rubygems_version = ">= 1"
|
18
|
+
|
19
|
+
s.files = `git ls-files`.split("\n")
|
20
|
+
s.test_files = `git ls-files -- {spec}/*`.split("\n")
|
21
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
+
s.require_paths = ['lib']
|
23
|
+
|
24
|
+
s.add_runtime_dependency 'json', '>= 0'
|
25
|
+
end
|
data/lib/antsy.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'json'
|
3
|
+
require 'shellwords'
|
4
|
+
|
5
|
+
module Antsy
|
6
|
+
|
7
|
+
# parse the specified file, whose contents look like this:
|
8
|
+
# arg1=foo arg2=bar
|
9
|
+
# and return a hash like this:
|
10
|
+
# {:arg1 => 'foo', :arg2 => 'bar'}
|
11
|
+
def self.args()
|
12
|
+
Hash[
|
13
|
+
Shellwords::shellwords(File.read(ARGV.first)).map { |kv| kv.split('=') }
|
14
|
+
].inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.fail!(msg)
|
18
|
+
puts ({ 'failed' => true, 'msg' => msg }.to_json)
|
19
|
+
exit 1
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.changed!(data={})
|
23
|
+
data[:changed] = true
|
24
|
+
puts data.to_json
|
25
|
+
exit 0
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.no_change!(data={})
|
29
|
+
data[:changed] = false
|
30
|
+
puts data.to_json
|
31
|
+
exit 0
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
data/spec/antsy_spec.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'tempfile'
|
3
|
+
|
4
|
+
describe Antsy do
|
5
|
+
|
6
|
+
it 'should parse equals-seperated args from a file' do
|
7
|
+
f = Tempfile.new('antsy')
|
8
|
+
f.write 'foo=bar bam=baz'
|
9
|
+
f.flush
|
10
|
+
ARGV[0] = f.path
|
11
|
+
Antsy.args.should == {foo: 'bar', bam: 'baz'}
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should emit failure json and exit failure' do
|
15
|
+
capture_stdout { Antsy.fail! 'foo' }.chomp.should == {failed: true, msg: 'foo'}.to_json
|
16
|
+
lambda { Antsy.fail! 'foo' }.should exit_with_code 1
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should emit change json and exit success' do
|
20
|
+
capture_stdout { Antsy.changed! }.chomp.should == {changed: true}.to_json
|
21
|
+
lambda { Antsy.changed! }.should exit_with_code 0
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should emit no-change json and exit success' do
|
26
|
+
capture_stdout { Antsy.no_change! }.chomp.should == {changed: false}.to_json
|
27
|
+
lambda { Antsy.no_change! }.should exit_with_code 0
|
28
|
+
end
|
29
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'antsy'
|
2
|
+
|
3
|
+
def capture_stdout
|
4
|
+
out = StringIO.new
|
5
|
+
$stdout = out
|
6
|
+
begin
|
7
|
+
yield
|
8
|
+
rescue SystemExit
|
9
|
+
;
|
10
|
+
end
|
11
|
+
out.string
|
12
|
+
ensure
|
13
|
+
$stdout = STDOUT
|
14
|
+
end
|
15
|
+
|
16
|
+
def suppress_stdout
|
17
|
+
begin
|
18
|
+
$stdout = StringIO.new
|
19
|
+
yield
|
20
|
+
ensure
|
21
|
+
$stdout = STDOUT
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
RSpec::Matchers.define :exit_with_code do |exp_code|
|
26
|
+
actual = nil
|
27
|
+
match do |block|
|
28
|
+
begin
|
29
|
+
suppress_stdout { block.call }
|
30
|
+
rescue SystemExit => e
|
31
|
+
actual = e.status
|
32
|
+
end
|
33
|
+
actual and actual == exp_code
|
34
|
+
end
|
35
|
+
failure_message_for_should do |block|
|
36
|
+
"expected block to call exit(#{exp_code}) but exit" +
|
37
|
+
(actual.nil? ? " not called" : "(#{actual}) was called")
|
38
|
+
end
|
39
|
+
failure_message_for_should_not do |block|
|
40
|
+
"expected block not to call exit(#{exp_code})"
|
41
|
+
end
|
42
|
+
description do
|
43
|
+
"expect block to call exit(#{exp_code})"
|
44
|
+
end
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: antsy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.6
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tim Miller
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: json
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: utility functions for ansible modules
|
31
|
+
email:
|
32
|
+
- ''
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- README.md
|
39
|
+
- Rakefile
|
40
|
+
- antsy.gemspec
|
41
|
+
- lib/antsy.rb
|
42
|
+
- lib/antsy/version.rb
|
43
|
+
- spec/antsy_spec.rb
|
44
|
+
- spec/spec_helper.rb
|
45
|
+
homepage: https://github.com/echohead/ansible-module
|
46
|
+
licenses: []
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 1.8.7
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '1'
|
63
|
+
requirements: []
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.8.23
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: utility functions for ansible modules
|
69
|
+
test_files: []
|