Class: ConfigCommand

Inherits:
CmdParse::Command
  • Object
show all
Defined in:
lib/spiderfw/cmd/commands/config.rb

Instance Method Summary collapse

Constructor Details

#initializeConfigCommand

Returns a new instance of ConfigCommand.



4
5
6
7
8
9
10
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
59
60
61
62
63
64
65
66
67
68
# File 'lib/spiderfw/cmd/commands/config.rb', line 4

def initialize
    super( 'config', true, true )
    @short_desc = _("Manage configuration")
    
    list = CmdParse::Command.new( 'list', false )
    list.short_desc = _("List configuration options")
    list.options = CmdParse::OptionParserWrapper.new do |opt|
    end
    
    list.set_execution_block do |args|
        require 'spiderfw'
        Spider.config.options.sort.each{ |o| puts o }
    end
    
    self.add_command(list)
    
    info = CmdParse::Command.new('info', false)
    info.short_desc = _("Get information about a configuration option")
    info.set_execution_block do |args|
        require 'spiderfw'
        option = Spider.config.option(args.first)
        if option && option[:params]
            print args[0]
            print ":\n"+option[:description]+"\n" if option[:description] && !option[:description].empty?
            puts
            puts "#{(_('Type')+':').ljust(10)} #{option[:params][:type]}" if option[:params][:type]
            default_str = nil
            if default = option[:params][:default]
                default_str = default.is_a?(Proc) ? _('Dynamic') : default
            end
            puts "#{(_('Default')+':').ljust(10)} #{default_str}" if default_str
            puts "#{(_('Choices')+':').ljust(10)} #{option[:params][:choices].join(', ')}" if option[:params][:choices]
        else
            puts _("Configuration option not found")
        end
    end
    
    self.add_command(info)
    
    get = CmdParse::Command.new('get', false)
    get.short_desc = _("Get the current value of a configuration option")
    get.set_execution_block do |args|
        require 'spiderfw'
        puts Spider.conf.get(args.first)
    end
    
    self.add_command(get)
    
    set = CmdParse::Command.new('set', false)
    set.short_desc = _("Set the value of a configuration option")
    set.set_execution_block do |args|
        require 'spiderfw/spider'
        require 'lib/spiderfw/config/configuration_editor'
        editor = Spider::ConfigurationEditor.new
        Spider.init_base
        Spider.config.loaded_files.each do |f|
            editor.load(f)
        end
        editor.set(*args)
        editor.save
    end
    
    self.add_command(set)
    
end