Skip to content

Commit 4b89557

Browse files
author
Peter Benjamin
committed
Refactor restacker_config.rb
1 parent be8f52e commit 4b89557

1 file changed

Lines changed: 35 additions & 6 deletions

File tree

source/lib/restacker_config.rb

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,52 @@
11
class RestackerConfig
22
def self.load_config(plane)
3+
plane = get_plane if plane.nil?
4+
config = find_config
5+
# puts "Inside 'self.load_config'"
6+
# puts "Config: #{config}"
7+
# puts "Plane: #{plane}"
8+
if config[plane].nil?
9+
puts "Plane not found (#{plane}). Please see #{CONFIG_FILE}."
10+
exit
11+
end
12+
config[plane]
13+
end
14+
15+
def self.get_plane(options)
16+
if options[:location]
17+
plane = options[:location]
18+
else
19+
plane = find_default_plane()
20+
end
21+
plane.to_sym
22+
end
23+
24+
private
25+
def self.find_config
26+
Dir.mkdir(CONFIG_DIR) unless Dir.exist?(CONFIG_DIR)
327
begin
4-
Dir.mkdir(CONFIG_DIR) unless Dir.exist?(CONFIG_DIR)
528
if File.exist?(CONFIG_FILE)
629
config = YAML.load_file(CONFIG_FILE)
730
else
831
config = YAML.load_file(SAMPLE_FILE)
932
File.open(CONFIG_FILE, 'w') { |f| f.write config.to_yaml }
1033
end
1134
rescue Psych::SyntaxError
12-
raise "Improperly formatted YAML file: #{CONFIG_FILE}, please ensure it is properly formatted"
35+
raise "Improperly formatted YAML file: #{CONFIG_FILE}."
1336
rescue => e
1437
puts e.message
1538
end
39+
config
40+
end
1641

17-
if config[plane].nil?
18-
puts "Plane not found (#{plane}), please see #{CONFIG_FILE}"
19-
exit
42+
def self.find_default_plane
43+
# puts "Inside 'self.find_default_plane'"
44+
config = find_config()
45+
# puts "config: #{config}"
46+
if config[:default][:label]
47+
config[:default][:label].to_sym
48+
else
49+
raise "Location was not provided and no default location was found in #{CONFIG_FILE}."
2050
end
21-
config[plane]
2251
end
2352
end

0 commit comments

Comments
 (0)