Class: KateGet

Inherits:
Object
  • Object
show all
Defined in:
lib/kate-get.rb

Overview

Main class for Kate-Get.

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ KateGet

Reads in a YAML config file or hash.

Parameters:

  • config (String, Hash)

    YAML config file or hash of config options



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/kate-get.rb', line 8

def initialize config
	@config = \
		if config.is_a? String
			require 'yaml'
			file = File.expand_path config
			YAML.load File.read file if File.exists? file
		elsif config.is_a? Hash
			config
		end

	fail "Failed to load config." unless @config.is_a? Hash

	#update

end

Instance Method Details

#add_file(file) ⇒ Object

Addes a file to the array of files to be copied.

Parameters:

  • file (String)

    path to file



26
27
28
29
# File 'lib/kate-get.rb', line 26

def add_file file
	@files = [] unless @files
	@files << file
end

#getObject

Uses rsync to download files based on config options.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/kate-get.rb', line 32

def get

	@files.each do |f|

		# Look for the first source that matches the file.
		site, name = nil
		@config[:sources].each do |n, s|
			name, site = [n, s] if f[/^#{s[:base].sub "##source_name##", n}/ ]
			break if site
		end

		# Skip the file if there is no matching source.
		next if site.nil?

		# Load global connection settings and merge with source specific ones.
		remote = @config[:global]
		remote.merge! site[:remote] unless site[:remote].nil?

		# Prepare the elements for the rsync command.
		root_dir = site[:root].sub "##source_name##", name
		base = site[:base].sub "##source_name##", name

		# Skip the file if it is not in the root directory of the source.
		next unless f[root_dir]

		f.sub! base, ""

		dir = "#{File.expand_path site[:local]}#{File.dirname f}"
		FileUtils.mkdir_p dir unless File.directory? dir

		rsync = "rsync -a -e 'ssh -p #{remote[:port]}' #{remote[:user]}@#{remote[:host]}:#{root_dir}/#{f} #{site[:local]}/#{f}"

		# Get the file using rsync.
		system rsync
	end
end