bin/diru in diru-0.1.0 vs bin/diru in diru-0.1.1

- old
+ new

@@ -143,18 +143,44 @@ return found[0] end end - # Find file entry from directory hierarchy upwards, exit if not found. - def Diru.must_find_upper_file( file, dir = Dir.pwd ) - begin - Diru.find_upper_file( file, dir ) - rescue RuntimeError - STDERR.puts "Could not find file \"#{file}\"!" - exit( false ) + # Find file entry from directory hierarchy upwards. + def Diru.find_an_upper_file( files, dir = Dir.pwd ) + found = nil + + # Process all directory levels. + while true + + # Process directory level. + while true + # Process all files per directory level. + idx = 0 + files.each do |file| + match = Dir.glob( "#{dir}/#{file}" ) + unless match.empty? + found = match[0] + break + end + end + + break if found + + end + + break if found + + if dir == "/" + raise RuntimeError, "Could not find file(s): \"#{files.join("\", \"")}\"!" + end + + dir = File.dirname( dir ) + end + + found end def Diru.opts_filename if ENV['DIRU_OPTS_FORMAT'] @@ -168,38 +194,24 @@ # Diru configuration file. class DiruConf - def DiruConf.deploy( filename = nil ) - dc_file = DiruConf.find( filename ) - DiruConf.use( dc_file ) - end - - def DiruConf.find( filename = nil ) - opts_file = nil - if Opt['options'].given - opts_file = Opt['options'].value - elsif filename - opts_file = filename - elsif ENV['DIRU_OPTS'] - opts_file = ENV['DIRU_OPTS'] - else - opts_file = "#{ENV['HOME']}/#{Diru.opts_filename}" + # Convert YAML config to hash with String keys at highest level. + def DiruConf.stringify_yaml_keys( entry ) + ret = {} + entry.each do |k,v| + if k.is_a? String + key = k + else + key = k.to_s + end + ret[ key ] = v end - - opts_file + ret end - def DiruConf.use( filename ) - case File.extname( filename ) - when ".json"; DiruConfJson.new( filename ) - when ".yml"; DiruConfYaml.new( filename ) - else raise RuntimeError, "Wrong Diru configuration file format (i.e. not json or yaml)!" - end - end - def initialize( filename ) @filename = filename end end @@ -208,11 +220,11 @@ # Diru configuration file in JSON. class DiruConfJson < DiruConf def load if File.exist?( @filename ) - JSON.parse( File.read( @filename ), :symbolize_names => true ) + JSON.parse( File.read( @filename ) ) else STDERR.puts "Diru Error: Broken JSON, please fix: \"#{@filename}\"..." nil end end @@ -223,11 +235,11 @@ # Diru configuration file in YAML. class DiruConfYaml < DiruConf def load if File.exist?( @filename ) - YAML.load( File.read( @filename ) ) + DiruConf.stringify_yaml_keys( YAML.load( File.read( @filename ) ) ) else STDERR.puts "Diru Error: Broken YAML, please fix: \"#{@filename}\"..." nil end end @@ -419,16 +431,16 @@ def update_conf if @opts_file return unless load_conf( @opts_file ) - if @conf[:dsync] && @conf[:dsync] >= 0 - @dsync = @conf[:dsync] + if @conf[ "dsync" ] && @conf[ "dsync" ] >= 0 + @dsync = @conf[ "dsync" ] end - if @conf[:sync] && @conf[:sync] >= 1 - @sync = @conf[:sync] + if @conf[ "sync" ] && @conf[ "sync" ] >= 1 + @sync = @conf[ "sync" ] @dsync = @sync unless @dsync end if @dsync == 0 kill_th_data @@ -436,18 +448,18 @@ unless @th_data start_th_data end end - if @conf[:hist] && ( @conf[:hist] >= 2 ) - @histlimit = @conf[:hist] + if @conf[ "hist" ] && ( @conf[ "hist" ] >= 2 ) + @histlimit = @conf[ "hist" ] else @histlimit = 20 end - if @conf[ :favs ] - @fav = @conf[ :favs ] + if @conf[ "favs" ] + @fav = @conf[ "favs" ] end end end @@ -844,17 +856,17 @@ end if Opt['template'].given puts %q{--- -:hist: 20 -:sync: 10 -:dsync: 0 -:favs: +hist: 20 +sync: 10 +dsync: 0 +favs: f: dir_0/dir_0_4/dir_0_4_0 g: dir_1/dir_1_2 -:peers: +peers: - - "(.*/dir_0/.*)/dir_0_2_0" - "\\1" - - "(.*/dir_0/.*)/dir_0_1_0" - "\\1/dir_0_1_1" } @@ -967,37 +979,61 @@ # Setup client server. hub = DRbObject.new( nil, "druby://localhost:#{hport}" ) root = nil - opts_file = nil + # Define project root, if it is explicitly defined. if ENV['DIRU_ROOT'] root = ENV['DIRU_ROOT'] else # First search for .diru_root_dir file. begin root = File.dirname( Diru.find_upper_file( '.diru_root_dir' ) ) rescue root = nil end + end - unless root + opts_json = ".diru.json" + opts_yaml = ".diru.yml" - # Next search for .diru.[json|yml] file. - begin - file = Diru.find_upper_file( Diru.opts_filename ) - root = File.dirname( file ) - opts_file = file - rescue - Diru.error "Could not find user directory root!" - end + opts_filename = nil + opts_file = nil + + if root + if Opt['options'].given + opts_filename = Opt['options'].value + elsif ENV['DIRU_OPTS'] + opts_filename = ENV['DIRU_OPTS'] + elsif File.exists? "#{ENV['HOME']}/#{opts_json}" + opts_filename = "#{ENV['HOME']}/#{opts_json}" + elsif File.exists? "#{ENV['HOME']}/#{opts_yaml}" + opts_filename = "#{ENV['HOME']}/#{opts_yaml}" + else + opts_filename = nil end + else + # No explicit root, hence derive from options file (if any). + begin + file = Diru.find_an_upper_file( [ opts_json, opts_yaml ] ) + root = File.dirname( file ) + opts_filename = file + rescue + Diru.error "Could not find user directory root!" + end end - # opts_file = Diru.get_opts_file( opts_file ) - opts_file = DiruConf.deploy( opts_file ) + if opts_filename + case File.extname( opts_filename ) + when ".json"; opts_file = DiruConfJson.new( opts_filename ) + when ".yml"; opts_file = DiruConfYaml.new( opts_filename ) + else raise RuntimeError, "Wrong Diru configuration file format (i.e. not json or yaml)!" + end + else + raise RuntimeError, "Missing configuration file!" + end if Opt['server'].value.any? user = Opt['server'].value[0] else user = ENV['USER'] @@ -1010,11 +1046,10 @@ end if s_port == 0 Diru.error "Could not start server!" else - # File.write( port_file, s_port.to_s ) if port_file puts "Using server port: #{s_port}..." end exit( true ) end @@ -1316,10 +1351,10 @@ # pair, and switch to the pair dir. # def peer # cur = Dir.pwd cur = @pwd - peers = @conf[ :peers ] + peers = @conf[ "peers" ] peers.each do |pair| re = Regexp.new( pair[0] ) if ( re.match( cur ) ) return cur.sub( re, pair[1] ) end