summaryrefslogtreecommitdiff
path: root/lib/leap_cli/config/environment.rb
diff options
context:
space:
mode:
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)