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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
# File 'lib/fos.rb', line 10
def archive
options = {}
opt_parser = OptionParser.new do |opt|
opt.banner = "Fos is a folder archive tool for Linux or Mac."
opt.separator ""
opt.separator "Archives ~/Desktop and ~/Downloads by default"
opt.separator ""
opt.separator "Usage: fos [OPTIONS]"
opt.separator ""
opt.separator "Options"
opt.on("-p","--path PATH","Input path(s), BE CAREFUL WITH THIS") do |path|
options[:path] = path
end
opt.on("-n","--name NAME","Output name, default is archive") do |name|
options[:name] = name
end
opt.on("-z","--zip","Output as zip file, default is false") do |zip|
options[:zip] = zip
end
opt.on("-h","--help","help") do |help|
puts opt_parser
options[:help] = help
end
opt.on("-v","--version","version") do |version|
puts Fos::VERSION
options[:version] = version
end
end
begin opt_parser.parse! ARGV
rescue OptionParser::InvalidOption => e
puts e
puts "Try fos -h"
exit 1
end
user_path = File.expand_path('~')
if !File.directory?("#{user_path}/.fos")
`mkdir #{user_path}/.fos`
`touch #{user_path}/.fos/config.yml`
open("#{user_path}/.fos/config.yml", 'w') { |f|
f.puts "name: archive"
f.puts "paths: [#{user_path}/Desktop, #{user_path}/Downloads]"
}
end
hash = YAML.load(File.read("#{user_path}/.fos/config.yml"))
path = options[:path] || nil
archive_name = options[:name] || hash['name']
zip = options[:zip] || false
version = options[:version] || false
help = options[:help] || false
folders = hash['paths']
today = Time.now.strftime("%B %e %Y").gsub(' ', '_').gsub(/:.*/, '')
case ARGV[0]
when "shit"
puts "I can clean that up for you"
else
do_archive = true
end
if help == true || version == true
do_archive = false
end
if !path.nil?
folders = path.split(',')
folders.each do |f|
f.sub! "~", "#{user_path}"
end
end
if do_archive == true
folders.each do |current_path|
current_files = Dir.entries(current_path)
if !File.directory?("#{current_path}/#{archive_name}")
`mkdir #{current_path}/#{archive_name}`
puts "[ " + "Creating".yellow + ": "+"#{current_path}/#{archive_name}"+" ]"
end
if !File.directory?("#{current_path}/#{archive_name}/#{today}")
`mkdir #{current_path}/#{archive_name}/#{today}`
puts "[ " + "Creating".yellow + ": "+"#{current_path}/#{archive_name}/#{today}"+" ]"
end
current_files.delete_if{|x| (x =~ /^\./) == 0 }
current_files.delete_if{|x| x == "#{archive_name}"}
current_files.delete_if{|x| (x =~ /Macintosh/) == 0}
current_files = current_files.map{|x| x.gsub(" ", '\ ').gsub("(", '\(').gsub(")", '\)')}
current_files.each do |filename|
`mv #{current_path}/#{filename} #{current_path}/#{archive_name}/#{today}`
puts "[ " + "Archiving".yellow + ": "+"#{current_path}/#{filename}"+" ]"
end
if zip == true
puts "[ " + "Zipping".yellow + ": #{current_path} ]"
`zip -r #{current_path}/#{archive_name}.zip #{current_path}/#{archive_name}`
tmp_archive_name = archive_name + ".zip"
else
tmp_archive_name = archive_name
end
puts "[ " + "Archived".colorize(:green) + ": "+"#{current_path} "+"into"+" #{current_path}/#{tmp_archive_name}"+" ]"
end
end
end
|