From f53924cfbd800137fe6181c40a51bee72bef5a5d Mon Sep 17 00:00:00 2001 From: "Glenn Y. Rolland" Date: Mon, 14 Dec 2009 23:56:06 +0000 Subject: [PATCH] sshfs-mapper: Updates for the ruby version. git-svn-id: https://websvn.glenux.net/svn/Upoc/sshfs-mapper/trunk@1462 eaee96b3-f302-0410-b096-c6cfd47f7835 --- config.rb | 69 ++++++++++++++++++++++++++----------------------- map.rb | 30 ++++++++++++++++++++- sshfs-mapper.rb | 8 +++--- 3 files changed, 70 insertions(+), 37 deletions(-) diff --git a/config.rb b/config.rb index b5dfdb9..81fede1 100644 --- a/config.rb +++ b/config.rb @@ -1,5 +1,5 @@ #!/usr/bin/ruby -# vim: set ts=2 sw=2: +# vim: set ts=4 sw=4: require 'optparse' require 'ostruct' @@ -8,7 +8,8 @@ require 'find' module SshfsMapper class Config - attr_reader :options + attr_reader :maps_active + attr_reader :maps def initialize() user = if ENV['USER'] then @@ -17,7 +18,7 @@ module SshfsMapper raise "Environment variable 'USER' is missing!" end - home_dir = if ENV['HOME'] then + home_dir = if ENV['HOME'] then ENV['HOME'] else "/home/" + user @@ -29,33 +30,33 @@ module SshfsMapper 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 - } ) + @config_dir = xdg_dir + '/sshfs-mapper' + @maps = [] + @initialize_enable = false + @umount_enable = false + @target = nil + @verbose_enable = false end - def parseFile(&blk) - puts "Parsing config #{@options.config_dir}/config" + def parseFile( &blk ) + puts "Parsing config #{@config_dir}/config" maps = [] - Find.find( @options.config_dir ) do |path| + Find.find( @config_dir ) do |path| if File.file?( path ) if File.basename( path ) =~ /.map$/ puts "* #{File.basename( path )}" - map = Map.new( path ) - map.parse() - if blk then - yield map - else - maps.push( map ) + begin + map = Map.new( path ) + map.parse() + if blk then + yield map + else + maps.push( map ) + end + rescue + # error while parsing map end - else - Find.prune # Don't look any further into this way end #total_size += FileTest.size(path) end @@ -71,39 +72,43 @@ module SshfsMapper opts.separator "" opts.separator "Specific options:" - 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 + 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 end opts.on('-u', '--umount', 'Umount') do |umount| - @options.umount_enable = umount + @umount_enable = umount end opts.on('-i', '--initialize', 'Populate default configuration and example map' ) do |init| - @options.initialize_enable = init + @initialize_enable = init end opts.on('-v', '--[no-]verbose', 'Run verbosely' ) do |verbose| - @options.verbose_enable = verbose + @verbose_enable = verbose end end begin opts.parse!( args ) rescue OptionParser::ParseError => e + puts opts.to_s + puts "" puts e.message exit 1 end - @options end def to_s s = [] - s << "config_file = #{@options.config_file}" - s << "verbose_enable = #{@options.verbose_enable}" + s << "config_file = #{@config_file}" + s << "verbose_enable = #{@verbose_enable}" s.join("\n") end end diff --git a/map.rb b/map.rb index cd9e015..717ef32 100644 --- a/map.rb +++ b/map.rb @@ -1,15 +1,43 @@ module SshfsMapper class Map + attr_reader :path, :host, :port, :user, :map + def initialize( map_path ) @path = map_path @host = nil @port = 22 @user = nil + @map = {} end def parse() puts "Parsing map #{@path}" - # + File.open( @path ) do |f| + f.each do |line| + case line + when /^MAP\s*=\s*(\S+)\s*(\S+)\s*$/ then + @map[$1] = $2 + when /^REMOTE_HOST\s*=\s*(\S+)\s*$/ then + @host = $1 + when /^REMOTE_PORT\s*=\s*(\S+)\s*$/ then + @port = $1 + when /^REMOTE_USER\s*=\s*(\S+)\s*$/ then + @user = $1 + when /^\s*$/ then + # skip + else + puts "unexpectd line '#{line}'" + end + end + end + end + + def is_alive? + #FIXME: test liveness + end + + def is_connected? + #FIXME test if connected / mounted end def connect() diff --git a/sshfs-mapper.rb b/sshfs-mapper.rb index baa603e..4643189 100755 --- a/sshfs-mapper.rb +++ b/sshfs-mapper.rb @@ -7,20 +7,20 @@ require 'map' module SshfsMapper class SshfsMapper def initialize() - @maps = nil + @active_maps = nil puts "-- sshfs-mapper --" conf = Config.new conf.parseCmd ARGV - @maps = conf.parseFile + @active_maps = conf.parseFile puts conf end def run() - if @maps.nil? then + if @active_maps.nil? then return end - @maps.each do |map| + @active_maps.each do |map| map.connect() end puts "--run"