bin/diru in diru-0.0.8 vs bin/diru in diru-0.1.0

- old
+ new

@@ -27,12 +27,10 @@ # rest of the path should include the words "build", and # "src". If path with this spec is found, the shell directory gets # updated. # # Perform: -# shell> dr @ doc -# or # shell> dr i # # for some documentation. # # Diru setup: @@ -60,10 +58,11 @@ require 'como' include Como require 'drb' +require 'json' require 'yaml' require 'fileutils' # require 'byebug' @@ -96,27 +95,14 @@ module Diru # Default port. DIRU_HUB_PORT = ENV['DIRU_HUB_PORT'] || 41114 - # Load yaml configuration file (if exists). - def Diru.load_conf( conf_file ) - conf = nil - if File.exist?( conf_file ) - begin - conf = YAML.load( File.read( conf_file ) ) - rescue - STDERR.puts "Diru Error: Broken YAML, please fix: \"#{conf_file}\"..." - end - end - conf - end - # See: Diru.load_conf def load_conf( conf_file ) - @conf = Diru.load_conf( conf_file ) + @conf = conf_file.load end # Error message display. def Diru.error( msg ) @@ -167,28 +153,91 @@ STDERR.puts "Could not find file \"#{file}\"!" exit( false ) end end - def Diru.get_opts_file( file = nil ) + + def Diru.opts_filename + if ENV['DIRU_OPTS_FORMAT'] + ".diru.#{ENV['DIRU_OPTS_FORMAT']}" + else + ".diru.yml" + end + end + +end + + +# 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 file - opts_file = file + elsif filename + opts_file = filename elsif ENV['DIRU_OPTS'] opts_file = ENV['DIRU_OPTS'] else - opts_file = "#{ENV['HOME']}/.diru.yml" + opts_file = "#{ENV['HOME']}/#{Diru.opts_filename}" end opts_file 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 +# Diru configuration file in JSON. +class DiruConfJson < DiruConf + + def load + if File.exist?( @filename ) + JSON.parse( File.read( @filename ), :symbolize_names => true ) + else + STDERR.puts "Diru Error: Broken JSON, please fix: \"#{@filename}\"..." + nil + end + end + +end + + +# Diru configuration file in YAML. +class DiruConfYaml < DiruConf + + def load + if File.exist?( @filename ) + YAML.load( File.read( @filename ) ) + else + STDERR.puts "Diru Error: Broken YAML, please fix: \"#{@filename}\"..." + nil + end + end + +end + + + # Diru Server State. class Search include Diru @@ -590,14 +639,15 @@ # Start data thread. def start_th_data unless @dsync == 0 @th_data = Thread.new do + sleep 1 loop do # puts "data update..." - sleep @dsync update_data_sync + sleep @dsync end end end end @@ -610,13 +660,14 @@ # Start opts thread. def start_th_opts @th_opts = Thread.new do + sleep 1 loop do - sleep @sync update_opts_sync + sleep @sync end end end @@ -929,21 +980,23 @@ rescue root = nil end unless root - # Next search for .diru.yml file. + + # Next search for .diru.[json|yml] file. begin - yml_file = Diru.find_upper_file( '.diru.yml' ) - root = File.dirname( yml_file ) - opts_file = yml_file + 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 end end - opts_file = Diru.get_opts_file( opts_file ) + # opts_file = Diru.get_opts_file( opts_file ) + opts_file = DiruConf.deploy( opts_file ) if Opt['server'].value.any? user = Opt['server'].value[0] else user = ENV['USER']