From a8cd5d3d54bb41610a9e6a919ddef8fcecd2d94b Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Tue, 30 Aug 2022 12:57:10 -0400 Subject: [PATCH] Added some basic configuration reading --- Gemfile | 5 +++++ Gemfile.lock | 15 +++++++++++++++ LICENSE | 2 +- src/config.rb | 29 +++++++++++++++++++++++++++++ src/zealot.rb | 7 +++++++ 5 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 src/config.rb create mode 100755 src/zealot.rb diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..57ef136 --- /dev/null +++ b/Gemfile @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +gem "toml", "~> 0.3.0" diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..577f343 --- /dev/null +++ b/Gemfile.lock @@ -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 diff --git a/LICENSE b/LICENSE index 5f662b3..2b5518f 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) +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: diff --git a/src/config.rb b/src/config.rb new file mode 100644 index 0000000..71b6bab --- /dev/null +++ b/src/config.rb @@ -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 diff --git a/src/zealot.rb b/src/zealot.rb new file mode 100755 index 0000000..4e8673b --- /dev/null +++ b/src/zealot.rb @@ -0,0 +1,7 @@ +#!/usr/bin/env ruby + +require_relative 'config.rb' + +c = Config.new('~/.config/zealot.toml') + +puts c.get('transcoder.repository')