Added some basic configuration reading

This commit is contained in:
Gregory Ballantine 2022-08-30 12:57:10 -04:00
parent fbadba375a
commit a8cd5d3d54
5 changed files with 57 additions and 1 deletions

5
Gemfile Normal file
View File

@ -0,0 +1,5 @@
# frozen_string_literal: true
source "https://rubygems.org"
gem "toml", "~> 0.3.0"

15
Gemfile.lock Normal file
View 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

View File

@ -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
View 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
View File

@ -0,0 +1,7 @@
#!/usr/bin/env ruby
require_relative 'config.rb'
c = Config.new('~/.config/zealot.toml')
puts c.get('transcoder.repository')