Class: CollinsShell::Console::Filesystem

Inherits:
Object
  • Object
show all
Includes:
CommandHelpers
Defined in:
lib/collins_shell/console/filesystem.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommandHelpers

#asset_context?, #asset_exists?, #call_collins, #cli_options, #collins_client, #find_assets, #find_one_asset, #get_all_tags, #get_asset, #get_tag_values, #resolve_asset_tag, #shell_handle, #tag_from_stack, #virtual_tags

Methods included from Cache

#cache_get_or_else, #clear_cache

Constructor Details

#initialize(optns = {}) ⇒ Filesystem

Returns a new instance of Filesystem.



9
10
11
12
13
14
# File 'lib/collins_shell/console/filesystem.rb', line 9

def initialize optns = {}
  options = Collins::Util.symbolize_hash(optns)
  @console_asset = options.fetch(:console_asset, nil)
  @options = options
  @stack = options.fetch(:stack, [])
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (protected)



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/collins_shell/console/filesystem.rb', line 91

def method_missing meth, *args, &block
  if asset? then
    a = @console_asset
    if a.respond_to?(meth) then
      a.send(meth, *args, &block)
    else
      Pry.output.puts("Command not found: #{meth}")
      cmds = available_commands.join(', ')
      Pry.output.puts("Available commands: #{cmds}")
    end
  elsif pry_commands.include?(meth) then
    arg = args.unshift(meth).join(' ')
    runner = CollinsShell::Console
    runner.run_pry_command(arg, :context => self, :binding_stack => [Pry.binding_for(self)])
  else
    Pry.output.puts("command not found: #{meth}")
  end
end

Instance Attribute Details

#console_assetObject

Using console_asset, don’t want to steal asset



7
8
9
# File 'lib/collins_shell/console/filesystem.rb', line 7

def console_asset
  @console_asset
end

#stackObject

Using console_asset, don’t want to steal asset



7
8
9
# File 'lib/collins_shell/console/filesystem.rb', line 7

def stack
  @stack
end

Instance Method Details

#asset?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/collins_shell/console/filesystem.rb', line 20

def asset?
  !console_asset.nil?
end

#asset_methods(exclude_non_scalar = false) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/collins_shell/console/filesystem.rb', line 52

def asset_methods exclude_non_scalar = false
  non_scalar = [:addresses, :backend_address, :backend_addresses, :backend_ip_addresses,
    :backend_netmasks, :cpus, :disks, :extras, :get_attribute, :mac_addresses, :memory, :nics,
    :power, :public_address, :public_addresses, :public_ip_addresses]
  asset_objects = {:ipmi => Collins::Ipmi, :state => Collins::AssetState}
  ex_methods = exclude_methods
  ex_methods += non_scalar if exclude_non_scalar
  Collins::Asset.public_instance_methods(false).map(&:to_sym).reject do |meth|
    ex_methods.include?(meth)
  end.map do |meth|
    if asset_objects.key?(meth) then
      asset_objects[meth].public_instance_methods(false).map(&:to_sym).reject do |meh|
        ex_methods.include?(meh) || meh.to_s =~ /=$/
      end.map do |meh|
        "#{meth}.#{meh}"
      end
    else
      [meth.to_s]
    end
  end.flatten.sort
end

#available_commandsObject



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/collins_shell/console/filesystem.rb', line 74

def available_commands
  includes = ['clear_cache'] + pry_commands
  if asset? then
    excludes = exclude_methods
    cmds1 = CollinsShell::Console::Asset.public_instance_methods(false).reject{|i| excludes.include?(i.to_sym)}
    cmds2 = asset_methods.map{|m| "asset.#{m}"}.sort
    cmds1 + cmds2 + includes
  else
    includes
  end
end

#exclude_methodsObject



48
49
50
# File 'lib/collins_shell/console/filesystem.rb', line 48

def exclude_methods
  ['respond_to?','to_s','cput', 'id'].map{|s|s.to_sym}
end

#pathObject



24
25
26
# File 'lib/collins_shell/console/filesystem.rb', line 24

def path
  '/' + @stack.join('/')
end

#popObject



28
29
30
31
32
33
34
35
# File 'lib/collins_shell/console/filesystem.rb', line 28

def pop
  stk = @stack.dup
  stk.pop
  stk = [] if stk.nil?
  asset = resolve_location(stk) if stk.size > 0
  opts = @options.merge(:stack => stk, :console_asset => asset)
  CollinsShell::Console::Filesystem.new(opts)
end

#push(ctxt) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/collins_shell/console/filesystem.rb', line 37

def push ctxt
  stk = @stack.dup
  stk << ctxt
  asset = resolve_location stk
  if asset? and asset then
    raise StandardError.new("Can not nest assets in assets")
  end
  opts = @options.merge(:stack => stk, :console_asset => asset)
  CollinsShell::Console::Filesystem.new(opts)
end

#root?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/collins_shell/console/filesystem.rb', line 16

def root?
  @stack.size == 0
end

#to_sObject



86
87
88
# File 'lib/collins_shell/console/filesystem.rb', line 86

def to_s
  "#{self.class}(path = '#{path}', asset? = '#{asset?}')"
end