Class: SakaiInfo::CLI::Shell

Inherits:
Object
  • Object
show all
Defined in:
lib/sakai-info/cli/shell.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeShell

Returns a new instance of Shell.



19
20
21
22
23
# File 'lib/sakai-info/cli/shell.rb', line 19

def initialize
  @context = nil
  @context_history = []
  @running = true
end

Class Method Details

.process(args, flags) ⇒ Object



15
16
17
# File 'lib/sakai-info/cli/shell.rb', line 15

def self.process(args, flags)
  Shell.new.run
end

Instance Method Details

#_shell_command_count(argv) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/sakai-info/cli/shell.rb', line 70

def _shell_command_count(argv)
  case argv[0].downcase
  when /^users?$/
    puts User.count
  when /^sites?$/
    puts Site.count
  else
    STDERR.puts("ERROR: unrecognized object type")
  end
end

#_shell_command_quit(argv) ⇒ Object Also known as: _shell_command_exit



64
65
66
# File 'lib/sakai-info/cli/shell.rb', line 64

def _shell_command_quit(argv)
  @running = false
end

#_shell_command_user(argv) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/sakai-info/cli/shell.rb', line 81

def _shell_command_user(argv)
  user = nil
  begin
    user = User.find(argv[0])
  rescue ObjectNotFoundException => e
    STDERR.puts "ERROR: could not find user '#{argv[0]}'"
    STDERR.puts "       #{e}"
    return
  end

  return if user.nil?

  puts user.to_yaml(:shell)
end

#context_promptObject



25
26
27
28
29
30
31
32
33
# File 'lib/sakai-info/cli/shell.rb', line 25

def context_prompt
  if @context.nil?
    "[-]"
  else
    # TODO: implement ShellContext object
    #@context.brief_name
    "[!]"
  end
end

#promptObject



35
36
37
38
# File 'lib/sakai-info/cli/shell.rb', line 35

def prompt
  print "sin#{context_prompt}> ";STDOUT.flush
  gets
end

#runObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sakai-info/cli/shell.rb', line 40

def run
  while @running do
    input = prompt

    # a ctrl-D sends nil
    if input.nil?
      puts "exit"
      break
    end

    argv = input.chomp.split(/ +/)
    command = argv.shift.downcase
    method_name = ("_shell_command_#{command}").to_sym

    if Shell.method_defined? method_name
      self.method(method_name).call(argv)
    else
      STDERR.puts "ERROR: unknown command '#{command}'"
    end
  end

  # TODO: any additional cleanup here
end