diff options
Diffstat (limited to 'lib/oxidized/source')
-rw-r--r-- | lib/oxidized/source/csv.rb | 11 | ||||
-rw-r--r-- | lib/oxidized/source/http.rb | 31 |
2 files changed, 34 insertions, 8 deletions
diff --git a/lib/oxidized/source/csv.rb b/lib/oxidized/source/csv.rb index d498e0b..c1e310a 100644 --- a/lib/oxidized/source/csv.rb +++ b/lib/oxidized/source/csv.rb @@ -11,14 +11,23 @@ class CSV < Source Oxidized.asetus.user.source.csv.delimiter = /:/ Oxidized.asetus.user.source.csv.map.name = 0 Oxidized.asetus.user.source.csv.map.model = 1 + Oxidized.asetus.user.source.csv.gpg = false Oxidized.asetus.save :user raise NoConfig, 'no source csv config, edit ~/.config/oxidized/config' end + require 'gpgme' if @cfg.gpg? end def load nodes = [] - open(File.expand_path @cfg.file).each_line do |line| + file = File.expand_path(@cfg.file) + file = if @cfg.gpg? + crypto = GPGME::Crypto.new password: @cfg.gpg_password + crypto.decrypt(file).to_s + else + open(file) + end + file.each_line do |line| next if line.match(/^\s*#/) data = line.chomp.split(@cfg.delimiter, -1) next if data.empty? diff --git a/lib/oxidized/source/http.rb b/lib/oxidized/source/http.rb index 4fd388b..6c12f29 100644 --- a/lib/oxidized/source/http.rb +++ b/lib/oxidized/source/http.rb @@ -29,25 +29,27 @@ class HTTP < Source end request = Net::HTTP::Get.new(uri.request_uri, headers) - if (@cfg.user && @cfg.pass) + if (@cfg.user? && @cfg.pass?) request.basic_auth(@cfg.user,@cfg.pass) end response = http.request(request) data = JSON.parse(response.body) - data.each do |line| - next if line.empty? + data.each do |node| + next if node.empty? # map node parameters keys = {} - @cfg.map.each do |key, position| - keys[key.to_sym] = node_var_interpolate line[position] + @cfg.map.each do |key, want_position| + want_positions = want_position.split('.') + keys[key.to_sym] = node_var_interpolate node.dig(*want_positions) end keys[:model] = map_model keys[:model] if keys.key? :model # map node specific vars vars = {} - @cfg.vars_map.each do |key, position| - vars[key.to_sym] = node_var_interpolate line[position] + @cfg.vars_map.each do |key, want_position| + want_positions = want_position.split('.') + vars[key.to_sym] = node_var_interpolate node.dig(*want_positions) end keys[:vars] = vars unless vars.empty? @@ -58,3 +60,18 @@ class HTTP < Source end end + +if RUBY_VERSION < '2.3' + class Hash + def dig(key, *rest) + value = self[key] + if value.nil? || rest.empty? + value + elsif value.respond_to?(:dig) + value.dig(*rest) + else # foo.bar.baz (bar exist but is not hash) + return nil + end + end + end +end |