summaryrefslogtreecommitdiff
path: root/lib/leap_cli/config/environment.rb
diff options
context:
space:
mode:
authorelijah <elijah@riseup.net>2017-01-05 10:47:06 -0800
committerelijah <elijah@riseup.net>2017-01-05 10:47:06 -0800
commit5ba6924cd64322c4759acfa1c12873a871043170 (patch)
treec1d1c6694110596438d9ad6d2a731d914ee01981 /lib/leap_cli/config/environment.rb
parent099eee57bcfcc7dc5ff0db48df266b0f30308bed (diff)
parentc0f489c4226c924fa1d96d12cba7eb5f63ccaf64 (diff)
Merge branch 'feature/node-disable'
Diffstat (limited to 'lib/leap_cli/config/environment.rb')
-rw-r--r--lib/leap_cli/config/environment.rb30
1 files changed, 26 insertions, 4 deletions
diff --git a/lib/leap_cli/config/environment.rb b/lib/leap_cli/config/environment.rb
index ce570839..0410ef5b 100644
--- a/lib/leap_cli/config/environment.rb
+++ b/lib/leap_cli/config/environment.rb
@@ -122,14 +122,25 @@ module LeapCli; module Config
end
#
- # Alters the node's json config file. Unfortunately, doing this will
- # strip out all the comments.
+ # Alters the node's json config file. As a side effect, all comments get
+ # moved to the top of the file.
#
- def update_node_json(node, new_values)
+ # NOTE: This does a shallow merge! In other words, a call like this...
+ #
+ # update_node_json(node, {"webapp" => {"domain" => "example.org"})
+ #
+ # ...is probably not what you want, because it will entirely remove all
+ # existing entries under "webapp".
+ #
+ def update_node_json(node, new_values, options=nil)
node_json_path = Path.named_path([:node_config, node.name])
+ comments = read_comments(node_json_path)
old_data = load_json(node_json_path, Config::Node)
+ options && options[:remove] && options[:remove].each do |key|
+ old_data.delete(key)
+ end
new_data = old_data.merge(new_values)
- new_contents = JSON.sorted_generate(new_data) + "\n"
+ new_contents = [comments, JSON.sorted_generate(new_data), "\n"].join
Util::write_file! node_json_path, new_contents
end
@@ -152,6 +163,17 @@ module LeapCli; module Config
results
end
+ def read_comments(filename)
+ buffer = StringIO.new
+ File.open(filename, "rb", :encoding => 'UTF-8') do |f|
+ while (line = f.gets)
+ next unless line =~ /^\s*\/\//
+ buffer << line
+ end
+ end
+ return buffer.string.force_encoding('utf-8')
+ end
+
def load_json(filename, object_class, options={})
if !File.exist?(filename)
return object_class.new(self)