lib/rbfs/command.rb in rbfs-0.0.10 vs lib/rbfs/command.rb in rbfs-0.0.14

- old
+ new

@@ -1,20 +1,25 @@ +require 'hosts_file' require 'rbfs/args' require 'rbfs/config' -require "rbfs/host_parser" require "rbfs/rsync" require "rbfs/futures" require "rbfs/logger" module Rbfs class Command attr_accessor :config - def initialize - @config = parse_config - logger.critical "No hosts file specified" unless config[:hosts] - logger.critical "Root path not specified" unless config[:root] + def initialize(config = nil) + if config + @config = config + else + @config = parse_config unless config + end + @config[:logger] = Logger.new(@config) + logger.critical "No hosts file specified" unless @config[:hosts] + logger.critical "Root path not specified" unless @config[:root] end def parse_config config = {} cmdline_args = Rbfs::Args.new.parse @@ -22,38 +27,41 @@ config_args = Rbfs::Config.new(cmdline_args[:config]).parse config = config_args.merge(cmdline_args) else config = cmdline_args end - config[:logger] = Logger.new(config) + unless config[:remote_root] + config[:remote_root] = config[:root] + end config end def logger @config[:logger] end def sync success = true - results = sync_hosts - results.each do |host, result| - if result[:exitcode] != 0 - logger.error "#{host}: #{result[:exitcode].to_i}" - else - logger.info "#{host}: #{result[:exitcode].to_i}" + + sync_hosts.each do |host, result| + logger.info "#{host.name}: #{result.error}" + result.changes.each do |change| + logger.puts " | #{change.filename} (#{change.summary})" end - result[:output].split("\n").each do |line| - logger.puts " | #{line}" - end - success = false if result[:exitcode] != 0 + success = false unless result.success? end + success end def sync_hosts - config[:root] = File.join(config[:root], config[:subpath]) if config[:subpath] + if config[:subpath] + config[:root] = File.join(config[:root], config[:subpath]) + config[:remote_root] = File.join(config[:remote_root], config[:subpath]) + end + logger.info "Syncing #{config[:root]}..." - hosts = Rbfs::HostParser.new(File.open(config[:hosts])) + hosts = HostsFile.load(config[:hosts]) hosts.collect do |host| [host, sync_host(host)] end end