diff options
Diffstat (limited to 'lib/leap_cli/config')
-rw-r--r-- | lib/leap_cli/config/environment.rb | 30 | ||||
-rw-r--r-- | lib/leap_cli/config/manager.rb | 15 | ||||
-rw-r--r-- | lib/leap_cli/config/node.rb | 4 | ||||
-rw-r--r-- | lib/leap_cli/config/object_list.rb | 4 |
4 files changed, 45 insertions, 8 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) diff --git a/lib/leap_cli/config/manager.rb b/lib/leap_cli/config/manager.rb index d69a5808..a9f1a85f 100644 --- a/lib/leap_cli/config/manager.rb +++ b/lib/leap_cli/config/manager.rb @@ -342,14 +342,25 @@ module LeapCli if node.vagrant? return self.env("local") else - environment = self.env(default_environment) + environment = nil if node['tags'] node['tags'].to_a.each do |tag| if self.environment_names.include?(tag) - environment = self.env(tag) + if environment.nil? + environment = self.env(tag) + else + LeapCli::Util.bail! do + LeapCli.log( + :error, + "The node '%s' is invalid, because it cannot have two environments ('%s' and '%s')." % + [node.name, environment.name, tag] + ) + end + end end end end + environment ||= self.env(default_environment) return environment end end diff --git a/lib/leap_cli/config/node.rb b/lib/leap_cli/config/node.rb index 23abdee3..a7c5c1e4 100644 --- a/lib/leap_cli/config/node.rb +++ b/lib/leap_cli/config/node.rb @@ -169,8 +169,8 @@ module LeapCli; module Config # # modifies the config file nodes/NAME.json for this node. # - def update_json(new_values) - self.env.update_node_json(node, new_values) + def update_json(new_values, options=nil) + self.env.update_node_json(node, new_values, options) end # diff --git a/lib/leap_cli/config/object_list.rb b/lib/leap_cli/config/object_list.rb index 80f89d92..815864e4 100644 --- a/lib/leap_cli/config/object_list.rb +++ b/lib/leap_cli/config/object_list.rb @@ -49,6 +49,10 @@ module LeapCli end end + def first + ObjectList.new(self.values.first) + end + def exclude(node) list = self.dup list.delete(node.name) |