Class: LogTwuncator::ServiceCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/log_twuncator/service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServiceCommand

Returns a new instance of ServiceCommand.



8
9
10
# File 'lib/log_twuncator/service.rb', line 8

def initialize
  @options = { :delay => 5}
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



6
7
8
# File 'lib/log_twuncator/service.rb', line 6

def command
  @command
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/log_twuncator/service.rb', line 6

def options
  @options
end

Instance Method Details

#parse(args) ⇒ Object



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
# File 'lib/log_twuncator/service.rb', line 20

def parse(args)
  begin
    opts = OptionParser.new do |opts|          
      opts.separator ""
      opts.banner = "Usage: log_twuncator_service <command> [options]"
      opts.separator ""
      opts.separator "Specific options:"
      opts.separator ""
      opts.separator "Commands : install, remove, start and stop"
      opts.separator ""
      opts.on("-N", "--name SERVICE_NAME", String, "Name of windows service") do |v|
        @options[:name] = v
      end
      
      if @command == 'install'
        opts.on("-c", "--config FILE", String, "Config file with log file truncation settings") do |v|
          @options[:config] = v
        end
      
        opts.on("-d", "--delay N", Integer, "Check files for truncation every N minutes") do |v|
          @options[:delay] = v
        end
  
        opts.on("-l", "--log FILE", String, "Log file for truncator") do |v|
          @options[:log] = v
        end
      end
      
      opts.on_tail("-h", "--help", "Show this message") do
        puts opts.help
        exit
      end
    end
    opts.parse!(args)        
    validate_options
  rescue OptionParser::ParseError => e
    puts e
    puts opts
  end
end

#run(args) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/log_twuncator/service.rb', line 12

def run(args)
  @command = args.shift unless args[0] == '-h'
  @command = @command.downcase if @command
  
  parse(args)
  @options
end

#validate_optionsObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/log_twuncator/service.rb', line 61

def validate_options
  errors = []
  errors << "Service name is required (switch -N)" unless @options[:name]
  if @command == 'install'
    errors << "Delay is required (switch -d)" unless @options[:delay]
    errors << "Config file is required (switch -c)" unless @options[:config]
    errors << "Log file is required (switch -l)" unless @options[:log]
    errors << "Config file does not exist" if @options[:config] && !FileTest.exists?(@options[:config])
  end    
  
  if errors.size > 0
    puts "Error found."
    puts errors.join("\n")
    exit
  end
end