Class: Zazu
- Inherits:
-
Object
- Object
- Zazu
- Defined in:
- lib/zazu.rb
Overview
Fetch tools and run them
Example:
zazu = Zazu.new 'my-script'
zazu.fetch url: 'https://example.com/my_script.sh'
zazu.run ['--environment', 'prod']
Defined Under Namespace
Classes: DownloadError, Error, RunError
Constant Summary collapse
- VERSION =
'0.0.3'
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#fetch(url: nil, age: 60*60, &block) ⇒ Object
Download the tool, to the temp directory.
-
#initialize(name, logger: Logger.new(STDERR), level: Logger::INFO) ⇒ Zazu
constructor
Creates the Zazu instance.
-
#run(args = [], show: //, hide: /^$/, &block) ⇒ Object
Run the downloaded tool with arguments Raises a RunError if the command exits with nonzero.
Constructor Details
#initialize(name, logger: Logger.new(STDERR), level: Logger::INFO) ⇒ Zazu
Creates the Zazu instance. Create as many as you like.
-
:name:- The name of the command -
:logger:- Default to STDERR, can be replaced with another, e.g. IO::NIL -
:level:- Log level for the logger
33 34 35 36 37 38 |
# File 'lib/zazu.rb', line 33 def initialize(name, logger: Logger.new(STDERR), level: Logger::INFO) @name = name @path = File.join Dir.tmpdir, name + '-download' @logger = logger @logger.level = level end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
26 27 28 |
# File 'lib/zazu.rb', line 26 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
26 27 28 |
# File 'lib/zazu.rb', line 26 def path @path end |
Instance Method Details
#fetch(url: nil, age: 60*60, &block) ⇒ Object
Download the tool, to the temp directory. Raises a DownloadError on HTTP error.
-
:url:- The URL to the tool–Optional, can specity the block instead -
:age:- Seconds old the downloaded copy can be before downloading again–Default is 3600 -
:block:- Receives the OS (:linux, :mac, :windows) and the machine arch (32 or 64)–Should return the URL
45 46 47 48 49 50 51 52 |
# File 'lib/zazu.rb', line 45 def fetch(url: nil, age: 60*60, &block) return false if File.exists?(path) && Time.now - File.stat(path).mtime < age url ||= block.call os, arch @logger.info "Downloading from #{url}".cyan download_file url end |
#run(args = [], show: //, hide: /^$/, &block) ⇒ Object
Run the downloaded tool with arguments Raises a RunError if the command exits with nonzero.
-
:args:- An array of args for the command -
:show:- A regexp of output (STDOUT and STDERR) to include -
:hide:- A regexp of output (STDOUT and STDERR) to exclude -
:block:- If given, receies each output line, otherwise output is logged
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/zazu.rb', line 61 def run(args = [], show: //, hide: /^$/, &block) command = [path] + args @logger.debug "Running command #{command}".yellow Open3.popen3 *command do |i,o,e,t| i.close threads = [o,e].map do |x| Thread.new do x.each_line do |line| line.chomp! next if line !~ show || line =~ hide if block block.call line else @logger.info line.cyan end end end end threads.each &:join t.join raise RunError.new "#{path} exited with code #{t.value}" unless t.value == 0 end true end |