Skip to content

Commit

Permalink
Reuse exisiting code
Browse files Browse the repository at this point in the history
  • Loading branch information
sargon committed Jun 19, 2024
1 parent 09ac9c9 commit 81c7105
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 38 deletions.
12 changes: 5 additions & 7 deletions lib/oxidized/source/csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def setup

def load(_node_want = nil)
nodes = []
open_file.each_line do |line|
open_file(@cfg).each_line do |line|
next if line =~ /^\s*#/

data = line.chomp.split(@cfg.delimiter, -1)
Expand All @@ -46,12 +46,10 @@ def load(_node_want = nil)
nodes
end

private

def open_file
file = File.expand_path(@cfg.file)
if @cfg.gpg?
crypto = GPGME::Crypto.new password: @cfg.gpg_password
def self.open_file(cfg)
file = File.expand_path(cfg.file)
if cfg.gpg?
crypto = GPGME::Crypto.new password: cfg.gpg_password
crypto.decrypt(File.open(file)).to_s
else
File.open(file)
Expand Down
6 changes: 3 additions & 3 deletions lib/oxidized/source/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ def load(node_want = nil)
nodes
end

private

def string_navigate(object, wants)
def self.string_navigate(object, wants)
wants = wants.split(".").map do |want|
head, match, _tail = want.partition(/\[\d+\]/)
match.empty? ? head : [head, match[1..-2].to_i]
Expand All @@ -62,6 +60,8 @@ def string_navigate(object, wants)
object
end

private

def pagination(data, node_want)
node_data = []
raise Oxidized::OxidizedError, "if using pagination, 'pagination_key_name' setting must be set" unless @cfg.pagination_key_name?
Expand Down
34 changes: 6 additions & 28 deletions lib/oxidized/source/jsonfile.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Oxidized
require "csv"
require "oxidized/source/http"
require "oxidized/source/csv"
class JSONFile < Source

def initialize
Expand All @@ -23,52 +24,29 @@ def setup

def load(node_want = nil)
nodes = []
data = JSON.parse(open_file.read)
data = string_navigate(data, @cfg.hosts_location) if @cfg.hosts_location?
data = JSON.parse(CSV.open_file(@cfg).read)
data = HTTP.string_navigate(data, @cfg.hosts_location) if @cfg.hosts_location?
data.each do |node|
next if node.empty?

# map node parameters
keys = {}
@cfg.map.each do |key, want_position|
keys[key.to_sym] = node_var_interpolate string_navigate(node, want_position)
keys[key.to_sym] = node_var_interpolate HTTP.string_navigate(node, want_position)
end
keys[:model] = map_model keys[:model] if keys.has_key? :model
keys[:group] = map_group keys[:group] if keys.has_key? :group

# map node specific vars
vars = {}
@cfg.vars_map.each do |key, want_position|
vars[key.to_sym] = node_var_interpolate string_navigate(node, want_position)
vars[key.to_sym] = node_var_interpolate HTTP.string_navigate(node, want_position)
end
keys[:vars] = vars unless vars.empty?

nodes << keys
end
nodes
end

private

def string_navigate(object, wants)
wants = wants.split(".").map do |want|
head, match, _tail = want.partition(/\[\d+\]/)
match.empty? ? head : [head, match[1..-2].to_i]
end
wants.flatten.each do |want|
object = object[want] if object.respond_to? :each
end
object
end

def open_file
file = File.expand_path(@cfg.file)
if @cfg.gpg?
crypto = GPGME::Crypto.new password: @cfg.gpg_password
crypto.decrypt(File.open(file)).to_s
else
File.open(file)
end
end
end
end

0 comments on commit 81c7105

Please sign in to comment.