wol 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/VERSION +1 -1
  2. data/lib/wol/runner.rb +52 -51
  3. metadata +2 -2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 0.3.3
data/lib/wol/runner.rb CHANGED
@@ -27,128 +27,129 @@ module Wol
27
27
  07:09:09:0A:0B:0C example.com
28
28
  0D:0E:0F:00:10:11}
29
29
 
30
+ @options = { :quiet => false,
31
+ :address => "255.255.255.255",
32
+ :port => 9,
33
+ :delay => 0.01,
34
+ :count => 3,
35
+ :macs => [],
36
+ :file => nil,
37
+ :nothing_to_wake => false}
38
+
30
39
  # Parse the give arguments
31
- def self.parse(args)
40
+ def self.parse!(args)
32
41
  args = ["-h"] if args.empty?
33
42
 
34
- options = {}
35
- options[:quiet] = false
36
- options[:address] = "255.255.255.255"
37
- options[:port] = 9
38
- options[:delay] = 0.01
39
- options[:count] = 3
40
- options[:macs] = Array.new
41
- options[:file] = nil
42
-
43
43
  opts = OptionParser.new do |opts|
44
44
  # Set a banner, displayed at the top
45
45
  # of the help screen.
46
- opts.banner = "#{version}\nUsage: wol -i ADDRESS ff:ff:ff:ff:ff:ff"
46
+ opts.banner = "Ruby Wake-On-LAN #{version}\nUsage: wol -i ADDRESS ff:ff:ff:ff:ff:ff"
47
47
 
48
- opts.separator ""
49
- opts.separator "Specific options:"
48
+ opts.separator "\nSpecific options:"
50
49
 
51
50
  opts.on( '-q', '--quiet', 'No console output' ) do
52
- options[:quiet] = true
51
+ @options[:quiet] = true
53
52
  end
54
53
 
55
54
  opts.on( '-i', '--address 255.255.255.255', 'Set the destination address' ) do |address|
56
- options[:address] = address
55
+ @options[:address] = address
57
56
  end
58
57
 
59
58
  opts.on( '-p', '--port 9', Integer, 'Set the destination port' ) do |port|
60
- options[:port] = port
59
+ @options[:port] = port
61
60
  end
62
61
 
63
62
  opts.on( '-d', '--delay 0.01', Float, 'Delay between sending packets in seconds') do |delay|
64
- options[:delay] = delay
63
+ @options[:delay] = delay
65
64
  end
66
65
 
67
66
  opts.on( '-c', '--count 3', Integer, 'Number of packets to send. Default 3') do |count|
68
- options[:count] = count
67
+ @options[:count] = count
69
68
  end
70
69
 
71
70
  opts.on( '-f', '--file FILE', 'TODO: Uses a file as a source of addresses') do |file|
72
- options[:file] = file
71
+ @options[:file] = file
73
72
  end
74
73
 
75
- opts.separator ""
76
- opts.separator "Common options:"
74
+ opts.separator "\nCommon options:"
77
75
 
78
76
  opts.on_tail( '-h', '--help', 'Display this message' ) do
77
+ @options[:nothing_to_wake] = true
79
78
  puts opts
80
- exit
81
79
  end
82
80
 
83
81
  opts.on_tail( '-s', '--sample-file', 'Display a sample file') do
82
+ @options[:nothing_to_wake] = true
84
83
  puts @sample_file
85
- exit
86
84
  end
87
85
 
88
86
  opts.on_tail( '-v', '--version', 'Show version') do
89
- puts version
90
- exit
87
+ @options[:nothing_to_wake] = true
88
+ puts "Ruby Wake-On-LAN #{version}"
91
89
  end
92
90
  end
93
91
 
94
92
  begin
95
93
  opts.parse!(args)
96
94
 
97
- if options[:file].nil?
95
+ if @options[:file].nil?
98
96
  args.each do |arg|
99
- options[:macs] << arg if /^(\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2})?$/.match(arg)
97
+ @options[:macs] << arg if /^(\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2}:\S{1,2})?$/.match(arg)
100
98
  end
101
99
 
102
- options[:macs].uniq!
100
+ @options[:macs].uniq!
103
101
  end
104
102
 
105
- return options
103
+ return @options
106
104
  rescue OptionParser::InvalidOption => e
107
105
  STDERR.puts e.message, "\n", opts
108
- exit(-1)
106
+ return -1
109
107
  end
110
108
  end
111
109
 
112
- # Return the version string.
113
- # TODO: Fix the way the version is defined.
110
+ # Return the version from the VERSION file if it exists
114
111
  def self.version
115
- "Ruby Wake-On-LAN version 0.3.1"
112
+ File.exist?('VERSION') ? File.read('VERSION') : ""
116
113
  end
117
114
 
118
115
  # Send WOL MagicPackets based on the parsed options
119
- def self.wake(options = {})
120
- if options[:file]
121
- hosts = ParseFile.read_and_parse_file(options[:file])
116
+ def self.wake!
117
+ if @options[:file]
118
+ hosts = ParseFile.read_and_parse_file(@options[:file])
122
119
 
123
- for host in hosts
124
- options[:address], options[:macs], options[:port] = host[:address], host[:mac], host[:port]
120
+ hosts.each do |host|
121
+ @options[:address], @options[:macs], @options[:port] = host[:address], host[:mac], host[:port]
125
122
 
126
- message = WakeOnLan.new(options).wake.to_s
123
+ message = WakeOnLan.new(@options).wake.to_s
127
124
  puts message unless options[:quiet]
125
+
128
126
  return 0
129
127
  end
130
- elsif !options[:macs].empty?
131
- options[:macs].each do |mac|
132
- options[:mac] = mac
133
- message = WakeOnLan.new(options).wake.to_s
134
- puts message unless options[:quiet]
135
- return 0
128
+ elsif !@options[:macs].empty?
129
+ @options[:macs].each do |mac|
130
+ @options[:mac] = mac
131
+ message = WakeOnLan.new(@options).wake.to_s
132
+ puts message unless @options[:quiet]
136
133
  end
134
+
135
+ return 0
136
+ elsif @options[:macs].empty? && @options[:file].nil? && !@options[:nothing_to_wake]
137
+ raise Exception, "You must supply a MAC address or a file"
137
138
  else
138
- puts "You have to specify a file or MAC address"
139
+ return -1
139
140
  end
140
141
  end
141
142
 
142
143
  # Parse the command line options, then use them to wake up any given hosts.
143
144
  def self.run!(argv)
144
145
  begin
145
- options = parse(argv)
146
+ parse!(argv)
146
147
 
147
- wake(options)
148
- return 0
148
+ return wake!
149
+ rescue SocketError => e
150
+ puts e.message
151
+ return -1
149
152
  rescue Exception => e
150
- puts "An error occured. Please check your inputs."
151
- puts "If you used a file, please check that it is properly formatted."
152
153
  STDERR.puts e.message
153
154
  return -1
154
155
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wol
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matias Korhonen
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-19 00:00:00 +02:00
12
+ date: 2009-11-21 00:00:00 +02:00
13
13
  default_executable: wol
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency