Class: Kodiak::ConfigReader

Inherits:
Object
  • Object
show all
Defined in:
lib/kodiak/config_reader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ConfigReader

Returns a new instance of ConfigReader.



8
9
10
11
12
13
14
15
# File 'lib/kodiak/config_reader.rb', line 8

def initialize(options)
	@options = options
	@environment = @options[:environment]			
	@input = get_config
	@files = []
	check_and_set_globals
	find_and_add_files
end

Instance Attribute Details

#environmentObject

Returns the value of attribute environment.



6
7
8
# File 'lib/kodiak/config_reader.rb', line 6

def environment
  @environment
end

#filesObject

Returns the value of attribute files.



6
7
8
# File 'lib/kodiak/config_reader.rb', line 6

def files
  @files
end

#inputObject

Returns the value of attribute input.



6
7
8
# File 'lib/kodiak/config_reader.rb', line 6

def input
  @input
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/kodiak/config_reader.rb', line 6

def options
  @options
end

#userObject

Returns the value of attribute user.



6
7
8
# File 'lib/kodiak/config_reader.rb', line 6

def user
  @user
end

Instance Method Details

#check_and_set_globalsObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/kodiak/config_reader.rb', line 86

def check_and_set_globals
	if File.exists? "#{ENV['HOME']}/#{Kodiak::GLOBAL_CONFIG}"
		begin
			user = YAML.load(File.open("#{ENV['HOME']}/#{Kodiak::GLOBAL_CONFIG}"))
			if user.class == Hash && ! user.empty?
				Kodiak.user = user
				@user = user						
			else					  
          Kodiak::Notification.new "Kodiak has not been globally configured or the configuration is broken\n", "failure"
				puts "To configure, use:"
				puts 'kodiak configure --user.name "Firstname Lastname" --user.email "[email protected]"'
				exit
			end
		rescue ArgumentError => e
       puts "Could not parse YAML: #{e.message}\n"
		  exit
		end
	else
      Kodiak::Notification.new "Kodiak has not been globally configured or the configuration is broken\n", "failure"
		puts "To configure, use:"
		puts 'kodiak configure --user.name "Firstname Lastname" --user.email "[email protected]"'
		exit
	end
end

#find_and_add_filesObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kodiak/config_reader.rb', line 18

def find_and_add_files
	if @input[@environment]
		@input[@environment]['files'].each do |source, destination|
			Dir.glob(source).each do |file|
				input = { :source => file, :destination => destination }				
				@files.push input
			end
		end
	else
		puts "Environment '#{environment}' is not defined in kodiak.yaml"
		puts "Environments defined:"
		@input.each do |name, environment|
			puts "- #{name}"
		end
		exit
	end
end

#ftpObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kodiak/config_reader.rb', line 42

def ftp
	environment = @input[@environment]
	
	# supply a default path of root
	if ! environment['path']
		environment['path'] = ""
	end

	credentials = {}
	items = ['server','username','password','path']
	
	#check that they provided all the creds we need
	items.each do |item|
		if ! environment[item]
			puts "Missing FTP credential: #{item}"
			exit
		else
			credentials[item] = environment[item]
		end
	end
	return credentials
end

#ftp?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/kodiak/config_reader.rb', line 37

def ftp?
	@input[@environment]['ftp'] || false			
end

#get_configObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/kodiak/config_reader.rb', line 65

def get_config
	if File.exists?(Kodiak::CONFIG_FILENAME)
		begin
			config = YAML.load(File.open(Kodiak::CONFIG_FILENAME))
			if config.class == Hash && ! config.empty?
		  	return config
			else					  
          Kodiak::Notification.new "Kodiak configuration exists but has not been defined yet. Configure it in #{Kodiak::CONFIG_FILENAME}\n", "failure"
				system("open #{Kodiak::CONFIG_FILENAME}")
				exit(0)
			end
		rescue ArgumentError => e
        Kodiak::Notification.new "Could not parse YAML: #{e.message}\n", "failure"
		  exit
		end
	else
      Kodiak::Notification.new "Could not find a Kodiak configuration file in this directory. Use 'kodiak generate' to create one.\n", "failure"
		exit
	end
end