Class: Rubyang::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyang/cli.rb,
lib/rubyang/cli/parser.rb

Defined Under Namespace

Classes: Parser

Instance Method Summary collapse

Constructor Details

#initializeCli

Returns a new instance of Cli.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rubyang/cli.rb', line 11

def initialize
  @sock_file = "/tmp/rubyang/server/Example.sock"
  @db = DRbObject.new_with_uri( "drbunix:#{@sock_file}" )

  @config_tree = @db.configure

  @fo = File.open("#{File.dirname(__FILE__)}/cli/log.txt", 'w')
  @fo.sync = true

  #Readline.basic_word_break_characters = ""
  #Readline.completer_word_break_characters = ""
  Readline.completion_append_character = " "
  Readline.completion_proc = proc { |buf|
    @fo.puts "line_buffer: #{Readline.line_buffer}"
    begin
      tokens = Rubyang::Cli::Parser.parse( Readline.line_buffer )
    rescue
      next
    end
    command_type = tokens.shift
    case command_type
    when /^set$/
      if tokens.size == 0
        all_candidates = ['set']
        candidates = all_candidates
      else
        value = tokens.last.to_s
        all_candidates = get_candidates( @config_tree, tokens )
        candidates = all_candidates.grep(/^#{Regexp.escape(value.to_s)}/)
      end
    when /^show$/
      format = tokens[0].to_s
      all_candidates = ['xml', 'json']
      candidates = all_candidates.grep(/^#{Regexp.escape(format.to_s)}/)
      candidates
    else
      all_candidates = ['set', 'show']
      candidates = all_candidates.grep(/^#{Regexp.escape(command_type.to_s)}/)
      candidates
    end
    @fo.puts "tokens = #{tokens.inspect}"
    @fo.puts "all_candidates = #{all_candidates.inspect}"
    @fo.puts "candidates = #{candidates.inspect}"
    @fo.puts
    @fo.puts
    candidates
  }
end

Instance Method Details

#get_candidates(config_tree, tokens) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rubyang/cli.rb', line 76

def get_candidates config_tree, tokens
  @fo.puts config_tree.class
  @fo.puts tokens.inspect
  token = tokens[0].to_s
  case config_tree
  when Rubyang::Database::DataTree::Leaf
    if tokens.size == 1
      [config_tree.value]
    else
      []
    end
  else
    if tokens.size > 1 and config_tree.schema.children.map{ |c| c.arg }.include? token
      child_tree = config_tree.edit token
      get_candidates child_tree, tokens[1..-1]
    else
      config_tree.schema.children.map{ |c| c.arg }
    end
  end
end

#runObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rubyang/cli.rb', line 97

def run
  while buf = Readline.readline("> ", true)
    tokens = Rubyang::Cli::Parser.parse( buf )
    command_type = tokens.shift
    case command_type
    when /^set$/
      begin
        set @config_tree, tokens
      rescue => e
        puts "Error: #{e}"
      end
    when /^show$/
      show_type = tokens.shift
      case show_type
      when /^xml$/
        puts @config_tree.to_xml( pretty: true )
      when /^json$/
        puts @config_tree.to_json( pretty: true )
      end
    when /^commit$/
      begin
        @config_tree.commit
      rescue => e
        puts "Error: #{e}"
      end
    end
  end
end

#set(config_tree, tokens) ⇒ Object



60
61
62
# File 'lib/rubyang/cli.rb', line 60

def set config_tree, tokens
  set_recursive config_tree, tokens
end

#set_recursive(config_tree, tokens) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rubyang/cli.rb', line 64

def set_recursive config_tree, tokens
  return config_tree if tokens.size == 0
  token = tokens[0]
  case tokens[1..-1].size
  when 0
    config_tree.set token
  else
    child_tree = config_tree.edit token
    set_recursive child_tree, tokens[1..-1]
  end
end