From 7aa9299971a2cacad29ab86e82cc780354d659e0 Mon Sep 17 00:00:00 2001 From: Saku Ytti Date: Thu, 7 Aug 2014 14:33:18 +0300 Subject: Keep config as Oxidized::Config instead of hash The model will look like this: cmd 'show ip cef' do |out| out.type = 'poop' out end cmd 'show process cpu' do |out| out.type = 'poop' out end cmd 'show memory statistics' do |out| out.type = 'poop' out end I think it's cleaner. --- lib/oxidized/model/model.rb | 17 ++++++++--------- lib/oxidized/model/outputs.rb | 12 +++--------- lib/oxidized/output/git.rb | 6 ++---- lib/oxidized/string.rb | 16 ++++++++++++++++ 4 files changed, 29 insertions(+), 22 deletions(-) (limited to 'lib/oxidized') diff --git a/lib/oxidized/model/model.rb b/lib/oxidized/model/model.rb index af86f1d..3dd3f98 100644 --- a/lib/oxidized/model/model.rb +++ b/lib/oxidized/model/model.rb @@ -79,16 +79,15 @@ module Oxidized def cmd string, &block out = @input.cmd string return false unless out - out = Oxidized::String.new out self.class.cmds[:all].each do |all_block| - out = instance_exec out, string, &all_block + out = instance_exec Oxidized::String.new(out), string, &all_block end if vars :remove_secret self.class.cmds[:secret].each do |all_block| - out = instance_exec out, string, &all_block + out = instance_exec Oxidized::String.new(out), string, &all_block end end - out = instance_exec out, &block if block + out = instance_exec Oxidized::String.new(out), &block if block process_cmd_output out, string end @@ -152,12 +151,12 @@ module Oxidized private - def process_cmd_output cmd, name - if Hash === cmd - cmd[:name] = name - return cmd + def process_cmd_output output, name + if output.class != Oxidized::String + output = Oxidized::String.new output end - {:output=>cmd, :type=>'cfg', :name=>name} + output.cmd = name + output end end diff --git a/lib/oxidized/model/outputs.rb b/lib/oxidized/model/outputs.rb index 5ef9bc4..406f886 100644 --- a/lib/oxidized/model/outputs.rb +++ b/lib/oxidized/model/outputs.rb @@ -7,13 +7,7 @@ module Oxidized 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 + type(want_type).map { |out| out }.join end def << output @@ -25,11 +19,11 @@ module Oxidized end def type type - @outputs.select { |h| h[:type]==type } + @outputs.select { |out| out.type==type } end def types - @outputs.map { |h| h[:type] }.uniq + @outputs.map { |out| out.type }.uniq - ['cfg'] end private diff --git a/lib/oxidized/output/git.rb b/lib/oxidized/output/git.rb index 59b6bd4..f87ab66 100644 --- a/lib/oxidized/output/git.rb +++ b/lib/oxidized/output/git.rb @@ -21,7 +21,6 @@ class Git < Output end def store file, outputs, opt={} - data = outputs.to_cfg @msg = opt[:msg] @user = (opt[:user] or @cfg.user) @email = (opt[:email] or @cfg.email) @@ -29,11 +28,10 @@ class Git < Output 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] + type_file = file + '--' + output.name + update type_repo, type_file, output end end diff --git a/lib/oxidized/string.rb b/lib/oxidized/string.rb index 35dc5af..b17f896 100644 --- a/lib/oxidized/string.rb +++ b/lib/oxidized/string.rb @@ -1,13 +1,29 @@ module Oxidized # Used in models, contains convenience methods class String < String + attr_accessor :type, :cmd + # @return [Oxidized::String] copy of self with last line removed def cut_tail Oxidized::String.new each_line.to_a[0..-2].join end + # @return [Oxidized::String] copy of self with first line removed def cut_head Oxidized::String.new each_line.to_a[1..-1].join end + + # @return clean/filename version of cmd + def name + cmd.strip.gsub(/\s+/, '_') + end + + private + + def initialize arg + super + @type = 'cfg' + @cmd = 'n/a' + end end end -- cgit v1.2.1