Skip to content

Instantly share code, notes, and snippets.

@KamilLelonek
Created January 31, 2015 13:43
Show Gist options
  • Save KamilLelonek/059d64dbc81bf5946dcb to your computer and use it in GitHub Desktop.
Save KamilLelonek/059d64dbc81bf5946dcb to your computer and use it in GitHub Desktop.
#!/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
@farnoy
Copy link

farnoy commented Feb 5, 2015

You could use ARGF instead of File.open(ARGV[0]), it's simpler and allows reading in from STDIN.

@rabajaj0509
Copy link

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