whiteout 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/lib/whiteout/version.rb +1 -1
  2. data/lib/whiteout.rb +43 -23
  3. metadata +4 -4
@@ -1,3 +1,3 @@
1
1
  module Whiteout
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/whiteout.rb CHANGED
@@ -1,49 +1,69 @@
1
1
  require "whiteout/version"
2
2
  require "optparse"
3
3
 
4
- # TODO handle directories more appropriately
5
- # TODO add man page
6
-
7
4
  module Whiteout
8
5
  def self.execute(*args)
6
+ recurse = false
7
+
9
8
  opts = OptionParser.new do |opts|
10
9
  opts.banner = "Usage: whiteout file1 [...]"
11
10
 
12
- opts.on('-h', '--help', 'Display this screen' ) do
11
+ opts.on('-h', '--help', 'Display this screen') do
13
12
  puts opts
14
13
  exit
15
14
  end
15
+
16
+ opts.on('-r', '--recursive', 'Clean all files under each directory, recursively') do
17
+ recurse = true
18
+ end
16
19
  end
17
20
 
18
21
  begin
19
- opts.parse!
20
-
21
- if !$stdin.tty? || args[0] == '-'
22
- puts self.clean($stdin.read)
23
- else
24
- if args.empty?
25
- puts opts
26
- exit
27
- end
22
+ opts.parse!(args)
23
+ rescue OptionParser::InvalidOption => e
24
+ abort "#{e}\n#{opts}"
25
+ end
28
26
 
29
- args.each do |file|
30
- abort "Can't find #{file}" unless File.exists?(file)
27
+ if !$stdin.tty? || args[0] == '-'
28
+ puts self.clean($stdin.read)
29
+ else
30
+ abort opts.to_s if args.empty?
31
31
 
32
- contents = File.read(file)
32
+ self.input_list(args, recurse).each do |file|
33
+ contents = File.read(file)
33
34
 
34
- # TODO consider writing to a temporary file and moving into place
35
- File.open(file, 'w') do |f|
36
- f.write(self.clean(contents))
37
- end
35
+ # TODO consider writing to a temporary file and moving into place
36
+ File.open(file, 'w') do |f|
37
+ f.write(self.clean(contents))
38
38
  end
39
39
  end
40
- rescue OptionParser::InvalidOption => e
41
- warn e
42
- puts opts
43
40
  end
44
41
  end
45
42
 
46
43
  def self.clean(str)
47
44
  str.gsub(/[ \t]+$/, '')
48
45
  end
46
+
47
+ def self.input_list(file_args, recurse)
48
+ to_process = file_args
49
+ ret = []
50
+
51
+ to_process.each do |file|
52
+ abort "Can't find #{file}" unless File.exists?(file)
53
+
54
+ if File.directory?(file)
55
+ if recurse
56
+ Dir[file + "/**/*"].each do |subfile|
57
+ to_process << subfile
58
+ end
59
+ else
60
+ abort "#{file} is a directory; not processing without -r option"
61
+ end
62
+ else
63
+ ret << file
64
+ end
65
+ end
66
+
67
+ ret
68
+ end
49
69
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: whiteout
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-13 00:00:00.000000000Z
12
+ date: 2012-02-14 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70201719750680 !ruby/object:Gem::Requirement
16
+ requirement: &70307420752320 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70201719750680
24
+ version_requirements: *70307420752320
25
25
  description: ! ' Removes trailing whitespace from standard input or from named files.
26
26
 
27
27
  '