2009-04-03 16:27:49 +00:00
|
|
|
#!/usr/bin/ruby
|
2009-12-14 23:56:06 +00:00
|
|
|
# vim: set ts=4 sw=4:
|
2009-04-03 16:27:49 +00:00
|
|
|
|
|
|
|
require 'optparse'
|
|
|
|
require 'ostruct'
|
|
|
|
require 'pp'
|
|
|
|
require 'find'
|
|
|
|
|
2011-03-07 23:51:29 +00:00
|
|
|
require 'rubygems'
|
|
|
|
require 'rdebug/base'
|
|
|
|
|
2009-05-22 21:01:23 +00:00
|
|
|
module SshfsMapper
|
|
|
|
class Config
|
2009-12-14 23:56:06 +00:00
|
|
|
attr_reader :maps_active
|
|
|
|
attr_reader :maps
|
2009-05-22 21:01:23 +00:00
|
|
|
|
2011-03-07 23:25:47 +00:00
|
|
|
def initialize
|
2009-05-22 21:01:23 +00:00
|
|
|
user = if ENV['USER'] then
|
|
|
|
ENV['USER']
|
2009-04-03 16:27:49 +00:00
|
|
|
else
|
2009-05-22 21:01:23 +00:00
|
|
|
raise "Environment variable 'USER' is missing!"
|
2009-04-03 16:27:49 +00:00
|
|
|
end
|
|
|
|
|
2009-12-14 23:56:06 +00:00
|
|
|
home_dir = if ENV['HOME'] then
|
2009-05-22 21:01:23 +00:00
|
|
|
ENV['HOME']
|
|
|
|
else
|
|
|
|
"/home/" + user
|
|
|
|
end
|
|
|
|
|
|
|
|
xdg_dir = if ENV['XDG_CONFIG_HOME'] then
|
|
|
|
ENV['XDG_CONFIG_HOME']
|
|
|
|
else
|
|
|
|
home_dir + '/.config'
|
|
|
|
end
|
|
|
|
|
2009-12-14 23:56:06 +00:00
|
|
|
@config_dir = xdg_dir + '/sshfs-mapper'
|
2010-01-24 21:00:47 +00:00
|
|
|
@config_file = nil
|
2009-12-14 23:56:06 +00:00
|
|
|
@maps = []
|
|
|
|
@initialize_enable = false
|
|
|
|
@umount_enable = false
|
|
|
|
@target = nil
|
|
|
|
@verbose_enable = false
|
2011-03-07 23:51:29 +00:00
|
|
|
@debug = false
|
2009-05-22 21:01:23 +00:00
|
|
|
end
|
2009-04-03 16:27:49 +00:00
|
|
|
|
2011-03-07 23:25:47 +00:00
|
|
|
def parse_file &blk
|
2011-03-07 23:51:29 +00:00
|
|
|
rdebug "Config: #{@config_dir}/config"
|
2009-05-22 21:01:23 +00:00
|
|
|
|
|
|
|
maps = []
|
2009-12-14 23:56:06 +00:00
|
|
|
Find.find( @config_dir ) do |path|
|
2011-03-07 23:25:47 +00:00
|
|
|
if File.file? path
|
2009-05-22 21:01:23 +00:00
|
|
|
if File.basename( path ) =~ /.map$/
|
2009-12-14 23:56:06 +00:00
|
|
|
begin
|
2011-03-07 23:25:47 +00:00
|
|
|
map = Map.new path
|
2011-03-07 23:51:29 +00:00
|
|
|
yield map if block_given?
|
|
|
|
maps.push map
|
2009-12-14 23:56:06 +00:00
|
|
|
rescue
|
|
|
|
# error while parsing map
|
2009-09-19 23:51:18 +00:00
|
|
|
end
|
2009-05-22 21:01:23 +00:00
|
|
|
end
|
|
|
|
#total_size += FileTest.size(path)
|
2009-04-03 16:27:49 +00:00
|
|
|
end
|
|
|
|
end
|
2009-05-22 21:01:23 +00:00
|
|
|
return maps
|
2009-04-03 16:27:49 +00:00
|
|
|
end
|
|
|
|
|
2011-03-07 23:25:47 +00:00
|
|
|
def parse_cmd_line args
|
2009-05-22 21:01:23 +00:00
|
|
|
opts = OptionParser.new do |opts|
|
2009-04-03 16:27:49 +00:00
|
|
|
|
2009-05-22 21:01:23 +00:00
|
|
|
opts.banner = "Usage: #{$0} [options]"
|
2009-04-03 16:27:49 +00:00
|
|
|
|
2009-05-22 21:01:23 +00:00
|
|
|
opts.separator ""
|
|
|
|
opts.separator "Specific options:"
|
2009-04-03 16:27:49 +00:00
|
|
|
|
2009-12-14 23:56:06 +00:00
|
|
|
opts.on('-a', '--all', 'Mount all targets (disables -s)') do |all|
|
|
|
|
@all_enable = all
|
|
|
|
end
|
|
|
|
|
|
|
|
#FIXME: use target list there
|
|
|
|
opts.on('-s', '--select TARGET', 'Mount only specified target') do |target|
|
|
|
|
@targets << target
|
2009-05-22 21:01:23 +00:00
|
|
|
end
|
2009-04-03 16:27:49 +00:00
|
|
|
|
2009-05-22 21:01:23 +00:00
|
|
|
opts.on('-u', '--umount', 'Umount') do |umount|
|
2009-12-14 23:56:06 +00:00
|
|
|
@umount_enable = umount
|
2009-05-22 21:01:23 +00:00
|
|
|
end
|
2009-04-03 16:27:49 +00:00
|
|
|
|
2009-05-22 21:01:23 +00:00
|
|
|
opts.on('-i', '--initialize',
|
|
|
|
'Populate default configuration and example map' ) do |init|
|
2009-12-14 23:56:06 +00:00
|
|
|
@initialize_enable = init
|
2009-05-22 21:01:23 +00:00
|
|
|
end
|
2009-04-03 16:27:49 +00:00
|
|
|
|
2009-05-22 21:01:23 +00:00
|
|
|
opts.on('-v', '--[no-]verbose', 'Run verbosely' ) do |verbose|
|
2009-12-14 23:56:06 +00:00
|
|
|
@verbose_enable = verbose
|
2009-05-22 21:01:23 +00:00
|
|
|
end
|
|
|
|
end
|
2009-04-03 16:27:49 +00:00
|
|
|
|
2009-05-22 21:01:23 +00:00
|
|
|
begin
|
2011-03-07 23:51:29 +00:00
|
|
|
opts.parse! args
|
2009-05-22 21:01:23 +00:00
|
|
|
rescue OptionParser::ParseError => e
|
2009-12-14 23:56:06 +00:00
|
|
|
puts opts.to_s
|
|
|
|
puts ""
|
2009-05-22 21:01:23 +00:00
|
|
|
puts e.message
|
|
|
|
exit 1
|
2009-04-03 16:27:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-22 21:01:23 +00:00
|
|
|
def to_s
|
|
|
|
s = []
|
2009-12-14 23:56:06 +00:00
|
|
|
s << "config_file = #{@config_file}"
|
|
|
|
s << "verbose_enable = #{@verbose_enable}"
|
2011-03-07 23:51:29 +00:00
|
|
|
s.join "\n"
|
2009-04-03 16:27:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|