Skip to content

Commit

Permalink
#321 #314 #302 should implement project specific config support feature
Browse files Browse the repository at this point in the history
  • Loading branch information
phuonglm committed Jun 15, 2017
1 parent b0c3d46 commit 23c46cc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
28 changes: 25 additions & 3 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,36 @@ override_hash = nil
# Check and override if exist any match JSON object from vagrant_config_override.json
if File.exist? (File.dirname(__FILE__) + '/vagrant_config_override.json')
override_file = File.read(File.dirname(__FILE__) + '/vagrant_config_override.json')

parsing_file = File.dirname(__FILE__) + '/vagrant_config_override.json'
begin
override_hash = JSON.parse(override_file)

data_hash = overrides(data_hash, override_hash)

if data_hash.key?('config_paths')
data_hash['config_paths'].each do |default_config_file_path, value|
overide_config_file_path = default_config_file_path.gsub(/default\.json$/, "overide.json")
if File.exist? (default_config_file_path)
default_config_file = File.read(default_config_file_path)
parsing_file = default_config_file_path
project_config_hash = JSON.parse(default_config_file)
end

if File.exist? (overide_config_file_path)
override_config_file = File.read(overide_config_file_path)
parsing_file = overide_config_file_path
overide_config_hash = JSON.parse(override_config_file)
project_config_hash = overrides(project_config_hash, overide_config_hash)
end
if !project_config_hash.nil?
overrides(data_hash, project_config_hash)
end
end
end
rescue Exception => msg
puts red(msg)
puts red('from vagrant_config_override.json')
ans = prompt yellow("some errors have occured and 'vagrant_config_override.json' file will not be used, do you want to continue? [y/N]: ")
puts red('from ' + parsing_file )
ans = prompt yellow("some errors have occured and '" + parsing_file + "' file will not be used, do you want to continue? [y/N]: ")
if ans.downcase != 'y'
exit!
end
Expand Down
24 changes: 20 additions & 4 deletions lib/utility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,33 @@
def overrides(obj1, obj2)
obj2.each do |key, value|
# replace
replaced_key = key.sub(/_r_/, '')
if key.start_with?('_r_') and obj1.has_key?(replaced_key) and value.class.name == 'Array'
obj1[replaced_key] = []
key = replaced_key
replaced_key = key.sub(/_[ra]_/, '')
if value.class.name == 'Array'
if (key.start_with?('_r_') || key.start_with?('_a_')) && obj1.has_key?(key)
obj1[replaced_key] = obj1[key].clone
obj1.delete(key)
end

if (key.start_with?('_r_') || key.start_with?('_a_')) && !obj1.has_key?(replaced_key)
obj1[replaced_key] = []
key = replaced_key
else
if key.start_with?('_r_')
obj1[replaced_key] = []
key = replaced_key
elsif key.start_with?('_a_')
value = obj1[replaced_key].concat(obj2[key])
key = replaced_key
end
end
end

if obj1.has_key?(key)
if value.class.name == 'Hash'
obj1[key] = overrides(obj1[key], obj2[key])
elsif value.class.name == 'Array'
obj1_value = obj1[key].clone

if value[0].class.name != 'Hash'
obj1_value = value
else
Expand Down
11 changes: 11 additions & 0 deletions vagrant_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,20 @@
"action": "add" // one of add, remove. Default: add.
// See more: https://github.com/customink-webops/magic_shell/blob/master/resources/alias.rb
}, {
"_id": "1",
"name": "ctop", // `$ ctop` for top-like interface for container metrics, see: https://github.com/bcicen/ctop
"command": "docker run -ti --name ctop --rm -v /var/run/docker.sock:/var/run/docker.sock quay.io/vektorlab/ctop:latest",
"action": "add"
},{
"_id": "2",
"name": "http", // `$ http` to make http requests, how to use: https://github.com/teracyhq/httpie-jwt-auth
"command": "docker container run -it --rm --net=host teracy/httpie-jwt-auth:latest-alpine",
"action": "add"
}, {
"_id": "3",
"name": "https", // shortcut for making https requests
"command": "http --default-scheme=https",
"action": "add"
}],
"env_vars": [{
"_id": "0",
Expand Down

0 comments on commit 23c46cc

Please sign in to comment.