Class: CMDB::Shell::DSL

Inherits:
BasicObject
Defined in:
lib/cmdb/shell/dsl.rb

Overview

Host for CMDB command methods. Every public method of this class is a CMDB command and its parameters represent the arguments to the command. If a command is successful, it always updates the _ attribute with the output (return value) of the command.

Instance Method Summary collapse

Constructor Details

#initialize(shell, out) ⇒ DSL

Returns a new instance of DSL.



7
8
9
10
11
# File 'lib/cmdb/shell/dsl.rb', line 7

def initialize(shell, out)
  @shell = shell
  @cmdb = @shell.cmdb
  @out = out
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



17
18
19
# File 'lib/cmdb/shell/dsl.rb', line 17

def method_missing(meth, *args)
  ::Kernel.raise ::CMDB::BadCommand.new(meth)
end

Instance Method Details

#cd(path) ⇒ Object Also known as: chdir



66
67
68
69
70
# File 'lib/cmdb/shell/dsl.rb', line 66

def cd(path)
  pwd = @shell.expand_path(path)
  @shell.pwd = CMDB.split(pwd)
  pwd.to_sym
end

#classObject



13
14
15
# File 'lib/cmdb/shell/dsl.rb', line 13

def class
  DSL
end

#get(key) ⇒ Object Also known as: cat



44
45
46
47
48
# File 'lib/cmdb/shell/dsl.rb', line 44

def get(key)
  key = @shell.expand_path(key)

  @cmdb.get(key)
end

#helpObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cmdb/shell/dsl.rb', line 26

def help
  @out.info 'Commands:'
  @out.info '  cd <key>          - append/remove search prefix'
  @out.info '  cd /              - reset search prefix'
  @out.info '  get <key>         - print value of key'
  @out.info '  ls                - show keys and values'
  @out.info '  set <key> <value> - print value of key'
  @out.info '  quit              - exit the shell'
  @out.info 'Key notation:'
  @out.info '  a.b.c             - relative to search prefix'
  @out.info '  ../b/c            - relative to parent of search prefix'
  @out.info '  /a                - relative to root (i.e. all sources)'
  @out.info 'Shortcuts:'
  @out.info '  <key>             - for get'
  @out.info '  <key>=<value>     - for set'
  @out.info '  cat,rm,unset,...  - as expected'
end

#ls(path = '') ⇒ Object



21
22
23
24
# File 'lib/cmdb/shell/dsl.rb', line 21

def ls(path='')
  prefix = @shell.expand_path(path)
  @cmdb.search prefix
end

#pryObject



73
74
75
# File 'lib/cmdb/shell/dsl.rb', line 73

def pry

end

#quitObject Also known as: exit



77
78
79
# File 'lib/cmdb/shell/dsl.rb', line 77

def quit
  ::Kernel.raise ::Interrupt
end

#set(key, value) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/cmdb/shell/dsl.rb', line 51

def set(key, value)
  key = @shell.expand_path key

  if @cmdb.set(key, value)
    @cmdb.get(key)
  else
    ::Kernel.raise ::CMDB::BadCommand.new('set', 'No source is capable of accepting writes')
  end
end

#unset(key) ⇒ Object Also known as: rm



61
62
63
# File 'lib/cmdb/shell/dsl.rb', line 61

def unset(key)
  @cmdb.set(key, nil)
end