summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/oxidized/model/model.rb28
-rw-r--r--lib/oxidized/model/outputs.rb43
-rw-r--r--lib/oxidized/output/git.rb43
-rw-r--r--lib/oxidized/output/output.rb5
4 files changed, 97 insertions, 22 deletions
diff --git a/lib/oxidized/model/model.rb b/lib/oxidized/model/model.rb
index c3d8760..af86f1d 100644
--- a/lib/oxidized/model/model.rb
+++ b/lib/oxidized/model/model.rb
@@ -1,3 +1,5 @@
+require_relative 'outputs'
+
module Oxidized
class Model
include Oxidized::Config::Vars
@@ -87,7 +89,7 @@ module Oxidized
end
end
out = instance_exec out, &block if block
- out
+ process_cmd_output out, string
end
def output
@@ -124,20 +126,20 @@ module Oxidized
end
def get
- data, pre = '', ''
+ outputs = Outputs.new
procs = self.class.procs
+ procs[:pre].each do |pre_proc|
+ outputs << instance_eval(&pre_proc)
+ end
self.class.cmds[:cmd].each do |command, block|
out = cmd command, &block
return false unless out
- data << out.to_s
- end
- procs[:pre].each do |pre_proc|
- pre << instance_eval(&pre_proc).to_s
+ outputs << out
end
procs[:post].each do |post_proc|
- data << instance_eval(&post_proc).to_s
+ outputs << instance_eval(&post_proc)
end
- pre + data
+ outputs
end
def comment _comment
@@ -148,5 +150,15 @@ module Oxidized
data
end
+ private
+
+ def process_cmd_output cmd, name
+ if Hash === cmd
+ cmd[:name] = name
+ return cmd
+ end
+ {:output=>cmd, :type=>'cfg', :name=>name}
+ end
+
end
end
diff --git a/lib/oxidized/model/outputs.rb b/lib/oxidized/model/outputs.rb
new file mode 100644
index 0000000..5ef9bc4
--- /dev/null
+++ b/lib/oxidized/model/outputs.rb
@@ -0,0 +1,43 @@
+module Oxidized
+ class Model
+ class Outputs
+
+ def to_cfg
+ type_to_str('cfg')
+ end
+
+ def type_to_str want_type
+ type(want_type).map { |h| h[:output] }.join
+ end
+
+ def each_type &block
+ types.each do |want_type|
+ yield [want_type, type(want_type)]
+ end
+ end
+
+ def << output
+ @outputs << output
+ end
+
+ def all
+ @outputs
+ end
+
+ def type type
+ @outputs.select { |h| h[:type]==type }
+ end
+
+ def types
+ @outputs.map { |h| h[:type] }.uniq
+ end
+
+ private
+
+ def initialize
+ @outputs = []
+ end
+
+ end
+ end
+end
diff --git a/lib/oxidized/output/git.rb b/lib/oxidized/output/git.rb
index 18c2cc1..59b6bd4 100644
--- a/lib/oxidized/output/git.rb
+++ b/lib/oxidized/output/git.rb
@@ -20,23 +20,27 @@ class Git < Output
end
end
- def store file, data, opt={}
- msg = opt[:msg]
- user = (opt[:user] or @cfg.user)
- email = (opt[:email] or @cfg.email)
- repo = @cfg.repo
- if opt[:group]
- repo = File.join File.dirname(repo), opt[:group] + '.git'
- end
- begin
- repo = Rugged::Repository.new repo
- update_repo repo, file, data, msg, user, email
- rescue Rugged::OSError, Rugged::RepositoryError
- Rugged::Repository.init_at repo, :bare
- retry
+ def store file, outputs, opt={}
+ data = outputs.to_cfg
+ @msg = opt[:msg]
+ @user = (opt[:user] or @cfg.user)
+ @email = (opt[:email] or @cfg.email)
+ @opt = opt
+ repo = @cfg.repo
+
+ outputs.types.each do |type|
+ next if type == 'cfg'
+ type_repo = File.join File.dirname(repo), type + '.git'
+ outputs.type(type).each do |output|
+ type_file = file + '--' + output[:name].strip.gsub(/\s+/, '_')
+ update type_repo, type_file, output[:output]
+ end
end
+
+ update repo, file, outputs.to_cfg
end
+
def fetch node, group
begin
repo = @cfg.repo
@@ -54,6 +58,17 @@ class Git < Output
private
+ def update repo, file, data
+ if @opt[:group]
+ repo = File.join File.dirname(repo), @opt[:group] + '.git'
+ end
+ repo = Rugged::Repository.new repo
+ update_repo repo, file, data, @msg, @user, @email
+ rescue Rugged::OSError, Rugged::RepositoryError
+ Rugged::Repository.init_at repo, :bare
+ retry
+ end
+
def update_repo repo, file, data, msg, user, email
oid = repo.write data, :blob
index = repo.index
diff --git a/lib/oxidized/output/output.rb b/lib/oxidized/output/output.rb
index eaf149a..1355d03 100644
--- a/lib/oxidized/output/output.rb
+++ b/lib/oxidized/output/output.rb
@@ -1,5 +1,10 @@
module Oxidized
class Output
class NoConfig < OxidizedError; end
+
+ def cfg_to_str cfg
+ cfg.select{ |h| h[:type]=='cfg' }.map{ |h| h[:data] }.join
+ end
+
end
end