Class: CMDB::Commands::Shell
- Inherits:
-
Object
- Object
- CMDB::Commands::Shell
- Defined in:
- lib/cmdb/commands/shell.rb
Constant Summary collapse
- ALT_SEPARATOR =
Character that acts as a separator between key components. The standard notation uses ‘.` but Shell allow allows `/` for a more filesystem-like user experience.
'/'.freeze
- NAVIGATION =
Directory navigation shortcuts (‘.’ and ‘..’)
/^#{Regexp.escape(CMDB::SEPARATOR + CMDB::SEPARATOR)}?$/.freeze
Instance Attribute Summary collapse
-
#_ ⇒ Object
Esult of the most recent command.
- #cmdb ⇒ CMDB::Interface readonly
- #dsl ⇒ CMDB::Shell::DSL readonly
-
#pwd ⇒ Array
The “present working directory” (i.e. key prefix) for shell commands.
Class Method Summary collapse
Instance Method Summary collapse
-
#expand_path(subpath) ⇒ String
Given a key name/prefix relative to ‘pwd`, return the absolute name/prefix.
-
#initialize(interface) ⇒ Shell
constructor
A new instance of Shell.
-
#run ⇒ Object
Run the shim.
Constructor Details
#initialize(interface) ⇒ Shell
Returns a new instance of Shell.
42 43 44 45 46 47 48 |
# File 'lib/cmdb/commands/shell.rb', line 42 def initialize(interface) @cmdb = interface @pwd = [] text = CMDB::Shell::Text.new(!$stdout.tty? || ENV['TERM'].nil?) @out = CMDB::Shell::Printer.new($stdout, $stderr, text) @in = CMDB::Shell::Prompter.new(text) end |
Instance Attribute Details
#_ ⇒ Object
Returns esult of the most recent command.
39 40 41 |
# File 'lib/cmdb/commands/shell.rb', line 39 def _ @_ end |
#cmdb ⇒ CMDB::Interface (readonly)
30 31 32 |
# File 'lib/cmdb/commands/shell.rb', line 30 def cmdb @cmdb end |
#dsl ⇒ CMDB::Shell::DSL (readonly)
33 34 35 |
# File 'lib/cmdb/commands/shell.rb', line 33 def dsl @dsl end |
#pwd ⇒ Array
Returns the “present working directory” (i.e. key prefix) for shell commands.
36 37 38 |
# File 'lib/cmdb/commands/shell.rb', line 36 def pwd @pwd end |
Class Method Details
.create(interface) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/cmdb/commands/shell.rb', line 13 def self.create(interface) require 'cmdb/shell' = Trollop. do <<-EOS The 'shell' command enters a Unix-like shell where you can interact with CMDB sources and inspect the value of keys. Usage: cmdb shell EOS end new(interface) end |
Instance Method Details
#expand_path(subpath) ⇒ String
Given a key name/prefix relative to ‘pwd`, return the absolute name/prefix. If `pwd` is empty, just return `subpath`. The subpath can use either dotted or slash notation, and directory navigation symbols (`.` and `..`) are applied as expected if the subpath uses slash notation.
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 90 91 92 93 94 |
# File 'lib/cmdb/commands/shell.rb', line 65 def (subpath) if subpath[0] == ALT_SEPARATOR result = [] else result = pwd.dup end if subpath.include?(ALT_SEPARATOR) || subpath =~ NAVIGATION # filesystem-like subpath # apply Unix-style directory navigation shortcuts # ge rid of successive //// and other oddities pieces = subpath.split(ALT_SEPARATOR).select { |p| !p.nil? && !p.empty? } pieces.each do |piece| case piece when '..' then result.pop when '.' then nil else result.push(piece) end end CMDB.join(result) else # standard dotted notation # get rid of successive .... and other oddities pieces = CMDB.split(subpath).select { |p| !p.nil? && !p.empty? } result += pieces end CMDB.join(result) end |