Skip to content

Commit

Permalink
Resolve conflicts while loading http classes (input, output, source)
Browse files Browse the repository at this point in the history
- manager.rb: specify in which namespace a file must be loaded
- Adds a namespace Oxidized::Output for outputs
- More unit tests to test the changes to manager.rb
- Renamed Oxidized::OxidizedFile to Oxidized::Output::File. To access
ruby's File object within the Oxidized::Output::File object, you need
::File within the Oxidized::Output::File object.

Other modifications:
- output/http.rb can not have worked, as it used a not initialized
constant CFGS. Fixed this.

Not modified:
- No Namespace Oxidized::Input was created for inputs. Maybe this should
be also done some day, but input/cli.rb seemed to be too complicated for
this commit.
  • Loading branch information
robertcheramy committed Nov 4, 2024
1 parent 73c8861 commit b3f3f95
Show file tree
Hide file tree
Showing 10 changed files with 569 additions and 491 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
- edgecos.rb: hide temperature and fan speed (@dhooper6430)
- cnos: show information before config, remove secrets only when told to do so (@robje)
- Updated slackdiff.rb to use new files.getUploadURLExternal slack file upload API instead of deprecated files.upload (@varesa)
- Updated source files to reference a Source module to avoid namespace duplication
- Updated source/output files to reference a Source/Outputb module to avoid namespace duplication (@laf, @robertcheramy)

### Fixed
- fixed error for ibos when remove_secret is set (@dminuoso)
Expand Down
37 changes: 17 additions & 20 deletions lib/oxidized/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@ module Oxidized
require 'oxidized/source/source'
class Manager
class << self
def load(dir, file)
def load(dir, file, namespace)
require File.join dir, file + '.rb'
klass = nil
# Search the object in the different namespaces
# - Most objects are in the namespace Oxidized
# - Source objects have their own namespace (Oxidized::Source)
# - Models have no namespace (Object)
[Oxidized, Oxidized::Source, Object].each do |mod|
klass = mod.constants.find { |const| const.to_s.casecmp(file).zero? }
klass ||= mod.constants.find { |const| const.to_s.downcase == 'oxidized' + file.downcase }
klass = mod.const_get klass if klass
break if klass
end

# Search the object to load in namespace
klass = namespace.constants.find { |const| const.to_s.casecmp(file).zero? }

return false unless klass

klass = namespace.const_get klass

i = klass.new
i.setup if i.respond_to? :setup
{ file => klass }
Expand All @@ -37,32 +34,32 @@ def initialize
end

def add_input(name)
loader @input, Config::INPUT_DIR, "input", name
loader @input, Config::INPUT_DIR, "input", name, Oxidized
end

def add_output(name)
loader @output, Config::OUTPUT_DIR, "output", name
loader @output, Config::OUTPUT_DIR, "output", name, Oxidized::Output
end

def add_source(name)
loader @source, Config::SOURCE_DIR, "source", name
loader @source, Config::SOURCE_DIR, "source", name, Oxidized::Source
end

def add_model(name)
loader @model, Config::MODEL_DIR, "model", name
loader @model, Config::MODEL_DIR, "model", name, Object
end

def add_hook(name)
loader @hook, Config::HOOK_DIR, "hook", name
loader @hook, Config::HOOK_DIR, "hook", name, Object
end

private

# if local version of file exists, load it, else load global - return falsy value if nothing loaded
def loader(hash, global_dir, local_dir, name)
def loader(hash, global_dir, local_dir, name, namespace)
dir = File.join(Config::ROOT, local_dir)
map = Manager.load(dir, name) if File.exist? File.join(dir, name + ".rb")
map ||= Manager.load(global_dir, name)
map = Manager.load(dir, name, namespace) if File.exist? File.join(dir, name + ".rb")
map ||= Manager.load(global_dir, name, namespace)
hash.merge!(map) if map
end
end
Expand Down
87 changes: 45 additions & 42 deletions lib/oxidized/output/file.rb
Original file line number Diff line number Diff line change
@@ -1,55 +1,58 @@
module Oxidized
class OxidizedFile < Output
require 'fileutils'
module Output
# ruby's File class must be accessed with ::File to avoid name conflicts
class File < Output
require 'fileutils'

attr_reader :commitref
attr_reader :commitref

def initialize
super
@cfg = Oxidized.config.output.file
end
def initialize
super
@cfg = Oxidized.config.output.file
end

def setup
return unless @cfg.empty?
def setup
return unless @cfg.empty?

Oxidized.asetus.user.output.file.directory = File.join(Config::ROOT, 'configs')
Oxidized.asetus.save :user
raise NoConfig, "no output file config, edit #{Oxidized::Config.configfile}"
end
Oxidized.asetus.user.output.file.directory = ::File.join(Config::ROOT, 'configs')
Oxidized.asetus.save :user
raise NoConfig, "no output file config, edit #{Oxidized::Config.configfile}"
end

def store(node, outputs, opt = {})
file = File.expand_path @cfg.directory
file = File.join File.dirname(file), opt[:group] if opt[:group]
FileUtils.mkdir_p file
file = File.join file, node
File.write(file, outputs.to_cfg)
@commitref = file
end
def store(node, outputs, opt = {})
file = ::File.expand_path @cfg.directory
file = ::File.join ::File.dirname(file), opt[:group] if opt[:group]
FileUtils.mkdir_p file
file = ::File.join file, node
::File.write(file, outputs.to_cfg)
@commitref = file
end

def fetch(node, group)
cfg_dir = File.expand_path @cfg.directory
node_name = node.name

if group # group is explicitly defined by user
cfg_dir = File.join File.dirname(cfg_dir), group
File.read File.join(cfg_dir, node_name)
elsif File.exist? File.join(cfg_dir, node_name) # node configuration file is stored on base directory
File.read File.join(cfg_dir, node_name)
else
path = Dir.glob(File.join(File.dirname(cfg_dir), '**', node_name)).first # fetch node in all groups
File.read path
def fetch(node, group)
cfg_dir = ::File.expand_path @cfg.directory
node_name = node.name

if group # group is explicitly defined by user
cfg_dir = ::File.join ::File.dirname(cfg_dir), group
::File.read ::File.join(cfg_dir, node_name)
elsif ::File.exist? ::File.join(cfg_dir, node_name) # node configuration file is stored on base directory
::File.read ::File.join(cfg_dir, node_name)
else
path = Dir.glob(::File.join(::File.dirname(cfg_dir), '**', node_name)).first # fetch node in all groups
::File.read path
end
rescue Errno::ENOENT
nil
end
rescue Errno::ENOENT
nil
end

def version(_node, _group)
# not supported
[]
end
def version(_node, _group)
# not supported
[]
end

def get_version(_node, _group, _oid)
'not supported'
def get_version(_node, _group, _oid)
'not supported'
end
end
end
end
Loading

0 comments on commit b3f3f95

Please sign in to comment.