-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxtract
executable file
·110 lines (89 loc) · 2.6 KB
/
xtract
1
2
3
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env ruby
require "bundler"
Bundler.setup
require_relative "ndex"
require "pathname"
require "thor"
module Xtract
class << self
extend NdexPlugin
attr_reader :zipfile
attr_reader :source_dir
ndexable :default_source_dir
ndexable :default_match_pattern
def zipfile=(dir)
@zipfile = Pathname.new(dir).expand_path
end
def source_dir=(dir)
@source_dir = Pathname.new(dir).expand_path
end
def new_subfolder_path
"#{Ndex.subfolder_prefix}#{Ndex.max_id + 1}"
end
def make_new_subfolder!
path = new_subfolder_path
puts "Making new subfolder #{path}"
path.tap do |path|
Dir.mkdir(path)
end
end
def extract!
path = make_new_subfolder!
puts "Unzipping #{zipfile} into #{path}"
`unzip "#{zipfile}" -d "#{path}"`
puts "New directory contents:"
Dir.glob("#{path}/*").each do |name|
puts " #{name}"
end
end
def move_matches!(pattern)
path = make_new_subfolder!
puts "Moving files into #{path}"
`mv #{Xtract.source_dir}/#{pattern} #{path}`
puts "New directory contents:"
Dir.glob("#{path}/*").each do |name|
puts " #{name}"
end
end
def candidate_matches(pattern)
Dir.glob("#{Xtract.source_dir}/#{pattern}")
end
end
end
class XtractCLI < Thor
desc "zip PATH", "unzip a zipfile of logs into a new subfolder"
def zip(path)
Xtract.zipfile = path
puts "Subfolder that would be created: #{Xtract.new_subfolder_path}\n\n"
if File.exists?(Xtract.zipfile)
if yes?("Unzip '#{Xtract.zipfile}'? (y/n)")
Xtract.extract!
else
puts "Aborting."
end
else
puts "You entered \"#{Xtract.zipfile}\" as your zipfile for extraction"
puts "This file does not exist, please provide a valid zipfile"
end
end
desc "match [PATTERN] [SOURCE_DIR]", "moves files matching PATTERN at SOURCE_DIR to new subfolder"
def match(pattern=nil, path=nil)
pattern = pattern || Xtract.default_match_pattern
Xtract.source_dir = path || Xtract.default_source_dir
puts "Subfolder that would be created: #{Xtract.new_subfolder_path}\n\n"
candidates = Xtract.candidate_matches(pattern)
if candidates.size == 0
error("No logfiles matching '#{Xtract.source_dir}/#{pattern}'! Aborting.")
else
puts "Files that would be moved:"
candidates.each { |filepath| puts " #{filepath}" }
puts
if yes?("Move these files? (y/n)")
Xtract.move_matches!(pattern)
else
puts "Aborting"
end
end
end
end
XtractCLI.start(ARGV)