Class: Rest::Terminal
- Inherits:
-
Object
show all
- Extended by:
- Commands, CommandsInfo, Persistent, PersistentRc
- Defined in:
- lib/rest/terminal.rb,
lib/rest/terminal/commands.rb,
lib/rest/terminal/persistent.rb,
lib/rest/terminal/commands_info.rb,
lib/rest/terminal/persistent_rc.rb
Defined Under Namespace
Modules: Commands, CommandsInfo, Persistent, PersistentRc
Class Method Summary
collapse
Methods included from Commands
is_runable?
Class Method Details
.commands ⇒ Object
84
85
86
87
88
|
# File 'lib/rest/terminal.rb', line 84
def commands
Rest::Terminal::Commands.private_instance_methods.collect do |x|
"#{x[1,99]}"
end
end
|
.current_path ⇒ Object
101
102
103
|
# File 'lib/rest/terminal.rb', line 101
def current_path
"services#{@pwd}"
end
|
.execute ⇒ Object
90
91
92
93
94
95
|
# File 'lib/rest/terminal.rb', line 90
def execute
puts "="*65
send("_#{@cmd}")
puts @_status.green
save_hist
end
|
.full_path(spc) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/rest/terminal.rb', line 109
def full_path(spc)
x = "#{@cpath} "
l = x.count('/')+1
dots = spc[/\.+/]
if dots &&
set = l-dots.length
set = 1 if set < 1
path_up = x.split(/\//)[0,set].join('/')
spc = spc.gsub(/\.+/,path_up)
end
spc = "#{spc}/" if spc[-1]!='/'
space_path(spc)
end
|
.initialize ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/rest/terminal.rb', line 14
def initialize
@skey = ''
@repl = ''
@type = 'earth'
@serv = 'localhost'
@service = ''
@services = { }
@response = { }
@spaces = [ ]
@hist = [ ]
@prm = [ ]
@pwd = '/'
@cmd = ''
@cpath = ''
@_status = ''
end
|
.invalid_command ⇒ Object
79
80
81
82
|
# File 'lib/rest/terminal.rb', line 79
def invalid_command
puts "#{('='*65)}\n#{'invalid command!'.red}\n#{('-'*65).yellow}"
puts "valid commands are:\n#{commands.sort.collect{|x|x.green}.join(', ')}"
end
|
.is_cmd_in_commands? ⇒ Boolean
sometime command can include the child service Ex: rest vars@child old code for this action use @pwd as current service path new code for this action introduce @cpath as current path
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/rest/terminal.rb', line 68
def is_cmd_in_commands?
puts ARGV.inspect.red
cmdp = ARGV[0].to_s.split('@')
parm = ARGV[1,99]
@cmd = cmdp[0]
@cpath = cmdp[1]
@prm = parm ? parm : [ ] commands.index(@cmd) ? @cmd : nil
end
|
.load_state ⇒ Object
55
56
57
58
59
60
61
62
|
# File 'lib/rest/terminal.rb', line 55
def load_state
if File.exists?('./.rest-terminal/persistent_rc.rb')
require './.rest-terminal/persistent_rc'
load_vars
@cpath = @capth ? "#{@pwd}#{@cpath}/" : @pwd
load_services
end
end
|
.multi_exec(cmd) ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/rest/terminal.rb', line 123
def multi_exec(cmd)
@prm = ['.'] if @prm==[]
@prm.each do |prm|
prm = @cpath if prm=='.'
pth = full_path(prm)
puts "Service: #{pth}".yellow
svc = @services[pth]
svc.load_service
@_status = svc.send(cmd,prm)
end
end
|
.prompt ⇒ Object
97
98
99
|
# File 'lib/rest/terminal.rb', line 97
def prompt
"#{@type}@#{@serv} >".yellow
end
|
.run ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/rest/terminal.rb', line 31
def run
initialize
if is_cmd_in_commands?
if @cmd == "init" || is_runable?
load_state
execute
save_vars
end
elsif ARGV == [ ]
require 'rest/terminal/commands_info'
puts ("="*65).yellow
commands.sort.each do |x|
help_line = send("#{x}_line")
puts " #{x.ljust(10)}#{help_line}"
end
puts ("-"*65).yellow
puts "for detail explanation on each commands"
puts "type: #{'rest help xxx'.green} #xxx: command to know"
else
invalid_command
end
end
|
.space_path(spc) ⇒ Object
105
106
107
|
# File 'lib/rest/terminal.rb', line 105
def space_path(spc)
(spc[0] == '/') ? spc : "#{@cpath}#{spc}"
end
|