Class: PVN::CachableCommand
Constant Summary collapse
- TMP_DIR =
ENV['PVN_TMP_DIR'] || '/tmp/pvncache'
- CACHE_DIR =
Pathname.new TMP_DIR
Instance Attribute Summary collapse
-
#lines ⇒ Object
Returns the value of attribute lines.
Instance Method Summary collapse
- #get_cache_file(cmd) ⇒ Object
-
#initialize(args = Hash.new) ⇒ CachableCommand
constructor
A new instance of CachableCommand.
- #run(args) ⇒ Object
- #run_cached_command(cmd) ⇒ Object
- #sysexec(cmd) ⇒ Object
- #use_cache? ⇒ Boolean
Constructor Details
#initialize(args = Hash.new) ⇒ CachableCommand
Returns a new instance of CachableCommand.
13 14 15 16 17 18 19 20 |
# File 'lib/system/cachecmd.rb', line 13 def initialize args = Hash.new debug "args: #{args.inspect}" @use_cache = args[:use_cache].nil? ? true : args[:use_cache] info "@use_cache: #{@use_cache}".blue super end |
Instance Attribute Details
#lines ⇒ Object
Returns the value of attribute lines.
11 12 13 |
# File 'lib/system/cachecmd.rb', line 11 def lines @lines end |
Instance Method Details
#get_cache_file(cmd) ⇒ Object
44 45 46 47 |
# File 'lib/system/cachecmd.rb', line 44 def get_cache_file cmd pwd = Pathname.pwd.to_s.sub(%r{^/}, '') CACHE_DIR + pwd + cmd.gsub(' ', '') end |
#run(args) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/system/cachecmd.rb', line 32 def run args if use_cache? run_cached_command args else super end end |
#run_cached_command(cmd) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/system/cachecmd.rb', line 49 def run_cached_command cmd stack "cmd: #{cmd}".cyan cfile = get_cache_file cmd if cfile.exist? debug "reading from cache file: #{cfile}".cyan @output = cfile.readlines else @output = ::IO::popen(cmd).readlines cfile.parent.mkpath stack "saving output to cache file: #{cfile}".cyan File.put_via_temp_file cfile do output end end end |
#sysexec(cmd) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/system/cachecmd.rb', line 22 def sysexec cmd info "cmd: #{cmd}".on_red if use_cache? info "cmd: #{cmd}".on_green run_cached_command cmd else @output = ::IO::popen(cmd).readlines end end |
#use_cache? ⇒ Boolean
40 41 42 |
# File 'lib/system/cachecmd.rb', line 40 def use_cache? @use_cache end |