2009-04-03 16:27:49 +00:00
|
|
|
#!/usr/bin/ruby
|
|
|
|
|
|
|
|
require 'optparse'
|
|
|
|
require 'ostruct'
|
|
|
|
require 'pp'
|
|
|
|
require 'find'
|
|
|
|
|
2009-05-22 21:01:23 +00:00
|
|
|
module SshfsMapper
|
|
|
|
class Config
|
|
|
|
attr_reader :options
|
|
|
|
|
|
|
|
def initialize()
|
|
|
|
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-05-22 21:01:23 +00:00
|
|
|
home_dir = if ENV['HOME'] then
|
|
|
|
ENV['HOME']
|
|
|
|
else
|
|
|
|
"/home/" + user
|
|
|
|
end
|
|
|
|
|
|
|
|
xdg_dir = if ENV['XDG_CONFIG_HOME'] then
|
|
|
|
ENV['XDG_CONFIG_HOME']
|
|
|
|
else
|
|
|
|
home_dir + '/.config'
|
|
|
|
end
|
|
|
|
|
|
|
|
@options = OpenStruct.new( {
|
|
|
|
:config_dir => xdg_dir + '/sshfs-mapper',
|
|
|
|
:map_list => [],
|
|
|
|
:initialize_enable => false,
|
|
|
|
:umount_enable => false,
|
|
|
|
:target => nil,
|
|
|
|
:verbose_enable => false
|
|
|
|
} )
|
|
|
|
end
|
2009-04-03 16:27:49 +00:00
|
|
|
|
2009-05-22 21:01:23 +00:00
|
|
|
def parseFile(&blk)
|
|
|
|
puts "Parsing #{@options.config_dir}/config"
|
|
|
|
puts "Parsing maps..."
|
|
|
|
|
|
|
|
maps = []
|
|
|
|
Find.find( @options.config_dir ) do |path|
|
|
|
|
if File.file?( path )
|
|
|
|
if File.basename( path ) =~ /.map$/
|
|
|
|
puts "* #{File.basename( path )}"
|
|
|
|
maps.push( path )
|
|
|
|
if blk then yield path end
|
|
|
|
else
|
|
|
|
Find.prune # Don't look any further into this way
|
|
|
|
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
|
|
|
|
|
2009-05-22 21:01:23 +00:00
|
|
|
def parseCmd( args )
|
|
|
|
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-05-22 21:01:23 +00:00
|
|
|
opts.on('-t', '--target TARGET', 'Mount only specified target') do |target|
|
|
|
|
@options.resize_enable = true
|
|
|
|
@options.resize_width = resizeX.to_i
|
|
|
|
@options.resize_height = resizeY.to_i
|
|
|
|
end
|
2009-04-03 16:27:49 +00:00
|
|
|
|
2009-05-22 21:01:23 +00:00
|
|
|
opts.on('-u', '--umount', 'Umount') do |umount|
|
|
|
|
@options.umount_enable = umount
|
|
|
|
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|
|
|
|
|
@options.initialize_enable = init
|
|
|
|
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|
|
|
|
|
@options.verbose_enable = verbose
|
|
|
|
end
|
|
|
|
end
|
2009-04-03 16:27:49 +00:00
|
|
|
|
2009-05-22 21:01:23 +00:00
|
|
|
begin
|
|
|
|
opts.parse!( args )
|
|
|
|
rescue OptionParser::ParseError => e
|
|
|
|
puts e.message
|
|
|
|
exit 1
|
2009-04-03 16:27:49 +00:00
|
|
|
end
|
2009-05-22 21:01:23 +00:00
|
|
|
@options
|
2009-04-03 16:27:49 +00:00
|
|
|
end
|
|
|
|
|
2009-05-22 21:01:23 +00:00
|
|
|
def to_s
|
|
|
|
s = []
|
|
|
|
s << "config_file = #{@options.config_file}"
|
|
|
|
s << "verbose_enable = #{@options.verbose_enable}"
|
|
|
|
s.join("\n")
|
2009-04-03 16:27:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|