Class: PVN::CommandLine

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/system/cmdline.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(use_cache, args = Array.new) ⇒ CommandLine

Returns a new instance of CommandLine.



11
12
13
14
# File 'lib/system/cmdline.rb', line 11

def initialize use_cache, args = Array.new
  @args = args.dup
  @use_cache = use_cache
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



8
9
10
# File 'lib/system/cmdline.rb', line 8

def output
  @output
end

#use_cacheObject

Returns the value of attribute use_cache.



9
10
11
# File 'lib/system/cmdline.rb', line 9

def use_cache
  @use_cache
end

Instance Method Details

#<<(arg) ⇒ Object



24
25
26
# File 'lib/system/cmdline.rb', line 24

def << arg
  @args << arg
end

#cache_dirObject



20
21
22
# File 'lib/system/cmdline.rb', line 20

def cache_dir
  nil
end

#executeObject



28
29
30
31
32
33
34
# File 'lib/system/cmdline.rb', line 28

def execute
  if use_cache?
    run_cached_command
  else
    @output = ::IO.popen(to_command).readlines
  end
end

#get_cache_fileObject



48
49
50
51
52
# File 'lib/system/cmdline.rb', line 48

def get_cache_file
  pwd = Pathname.pwd.split_path.join('')
  info "pwd: #{pwd}"
  Pathname.new(cache_dir) + pwd + @args.join('-')
end

#run(args) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/system/cmdline.rb', line 40

def run args
  if use_cache?
    run_cached_command
  else
    super
  end      
end

#run_cached_commandObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/system/cmdline.rb', line 54

def run_cached_command
  stack "@args: #{@args}".cyan
  cachefile = get_cache_file
  if cachefile.exist?
    debug "reading from cache file: #{cachefile}".cyan
    @output = cachefile.readlines
  else
    @output = IO.popen(to_command).readlines
    cachefile.parent.mkpath
    debug "saving output to cache file: #{cachefile}".on_cyan
    File.put_via_temp_file cachefile do
      @output
    end
  end
end

#to_commandObject



36
37
38
# File 'lib/system/cmdline.rb', line 36

def to_command
  @args.join ' '
end

#use_cache?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/system/cmdline.rb', line 16

def use_cache?
  @use_cache
end