Created
January 31, 2015 13:43
-
-
Save KamilLelonek/059d64dbc81bf5946dcb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# WANT_JSON | |
require 'rubygems' | |
require 'json' | |
File.open(ARGV[0]) do |arguments_stream| | |
begin | |
data = JSON.parse(arguments_stream.read) | |
dir_path = data.fetch 'dir_path' | |
exists = File.exists?(dir_path) | |
directory = exists ? File.directory?(dir_path) : false | |
empty = directory ? Dir["#{dir_path}/*"].empty? : nil | |
result = { | |
exists: exists, | |
directory: directory, | |
empty: empty | |
} | |
print JSON.dump(result) | |
rescue JSON::ParserError | |
print JSON.dump({ | |
failed: true, | |
msg: 'failed to parse dir_path argument' | |
}) | |
rescue KeyError | |
print JSON.dump({ | |
failed: true, | |
msg: 'dir_path key not found' | |
}) | |
end | |
end |
I am not clear about, how you verify this? As in how did you use this module in a playbook?
Also, can you run an ad-hoc command using this module?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You could use
ARGF
instead ofFile.open(ARGV[0])
, it's simpler and allows reading in from STDIN.