|
1 | 1 | class RestackerConfig |
2 | 2 | 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) |
3 | 27 | begin |
4 | | - Dir.mkdir(CONFIG_DIR) unless Dir.exist?(CONFIG_DIR) |
5 | 28 | if File.exist?(CONFIG_FILE) |
6 | 29 | config = YAML.load_file(CONFIG_FILE) |
7 | 30 | else |
8 | 31 | config = YAML.load_file(SAMPLE_FILE) |
9 | 32 | File.open(CONFIG_FILE, 'w') { |f| f.write config.to_yaml } |
10 | 33 | end |
11 | 34 | 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}." |
13 | 36 | rescue => e |
14 | 37 | puts e.message |
15 | 38 | end |
| 39 | + config |
| 40 | + end |
16 | 41 |
|
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}." |
20 | 50 | end |
21 | | - config[plane] |
22 | 51 | end |
23 | 52 | end |
0 commit comments