Added some basic configuration reading
This commit is contained in:
parent
fbadba375a
commit
a8cd5d3d54
5
Gemfile
Normal file
5
Gemfile
Normal file
@ -0,0 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
source "https://rubygems.org"
|
||||
|
||||
gem "toml", "~> 0.3.0"
|
15
Gemfile.lock
Normal file
15
Gemfile.lock
Normal file
@ -0,0 +1,15 @@
|
||||
GEM
|
||||
remote: https://rubygems.org/
|
||||
specs:
|
||||
parslet (2.0.0)
|
||||
toml (0.3.0)
|
||||
parslet (>= 1.8.0, < 3.0.0)
|
||||
|
||||
PLATFORMS
|
||||
x86_64-linux
|
||||
|
||||
DEPENDENCIES
|
||||
toml (~> 0.3.0)
|
||||
|
||||
BUNDLED WITH
|
||||
2.3.19
|
2
LICENSE
2
LICENSE
@ -1,4 +1,4 @@
|
||||
Copyright (c) <year> <owner>
|
||||
Copyright (c) 2022 Bit Goblin
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
|
29
src/config.rb
Normal file
29
src/config.rb
Normal file
@ -0,0 +1,29 @@
|
||||
require 'toml'
|
||||
|
||||
class Config
|
||||
|
||||
# class constructor
|
||||
def initialize(config_path)
|
||||
expanded_path = File.expand_path(config_path)
|
||||
@config = TOML::load_file(expanded_path)
|
||||
|
||||
# just in case the user wants to use a tilde (~) in the repository path...
|
||||
@config['transcoder']['repository'] = File.expand_path(self.get('transcoder.repository'))
|
||||
end
|
||||
|
||||
# returns a configuration value from a dot-seperated string, like 'transcoder.interval'
|
||||
def get(path)
|
||||
value = @config
|
||||
bits = path.split('.')
|
||||
bits.each { |bit|
|
||||
if (value.has_key?(bit))
|
||||
value = value[bit]
|
||||
else
|
||||
abort("Configuration value #{path} does exist.")
|
||||
end
|
||||
}
|
||||
|
||||
return value
|
||||
end
|
||||
|
||||
end
|
7
src/zealot.rb
Executable file
7
src/zealot.rb
Executable file
@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
require_relative 'config.rb'
|
||||
|
||||
c = Config.new('~/.config/zealot.toml')
|
||||
|
||||
puts c.get('transcoder.repository')
|
Loading…
Reference in New Issue
Block a user