Class: CMDB::Commands::Shell

Inherits:
Object
  • Object
show all
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
/^#{Regexp.escape(CMDB::SEPARATOR + CMDB::SEPARATOR)}?$/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interface) ⇒ Shell

Returns a new instance of Shell.

Parameters:



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.

Returns:

  • (Object)

    esult of the most recent command



39
40
41
# File 'lib/cmdb/commands/shell.rb', line 39

def _
  @_
end

#cmdbCMDB::Interface (readonly)

Returns:



30
31
32
# File 'lib/cmdb/commands/shell.rb', line 30

def cmdb
  @cmdb
end

#dslCMDB::Shell::DSL (readonly)

Returns:



33
34
35
# File 'lib/cmdb/commands/shell.rb', line 33

def dsl
  @dsl
end

#pwdArray

Returns the “present working directory” (i.e. key prefix) for shell commands.

Returns:

  • (Array)

    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'

  options = Trollop.options do
    banner <<-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.

Returns:

  • (String)

    absolute key in dotted 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 expand_path(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

#runObject

Run the shim.

Raises:

  • (SystemExit)

    if something goes wrong



53
54
55
56
# File 'lib/cmdb/commands/shell.rb', line 53

def run
  @dsl = CMDB::Shell::DSL.new(self, @out)
  repl
end