From 03484e8eb8f4b74806b2043af6d83a230b5ceb97 Mon Sep 17 00:00:00 2001 From: Chris Schlaeger Date: Tue, 19 Aug 2014 22:17:38 +0200 Subject: Adding support for a runtime config file. --- lib/postrunner/ActivitiesDB.rb | 5 +-- lib/postrunner/Main.rb | 20 ++++++++--- lib/postrunner/RuntimeConfig.rb | 73 +++++++++++++++++++++++++++++++++++++++++ spec/PostRunner_spec.rb | 6 ++-- 4 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 lib/postrunner/RuntimeConfig.rb diff --git a/lib/postrunner/ActivitiesDB.rb b/lib/postrunner/ActivitiesDB.rb index 008fb09..6aba195 100644 --- a/lib/postrunner/ActivitiesDB.rb +++ b/lib/postrunner/ActivitiesDB.rb @@ -22,10 +22,11 @@ module PostRunner class ActivitiesDB - attr_reader :db_dir, :fit_dir, :html_dir, :activities + attr_reader :db_dir, :cfg, :fit_dir, :html_dir, :activities - def initialize(db_dir) + def initialize(db_dir, cfg) @db_dir = db_dir + @cfg = cfg @fit_dir = File.join(@db_dir, 'fit') @html_dir = File.join(@db_dir, 'html') @archive_file = File.join(@db_dir, 'archive.yml') diff --git a/lib/postrunner/Main.rb b/lib/postrunner/Main.rb index 8ae59bf..89fd7d6 100644 --- a/lib/postrunner/Main.rb +++ b/lib/postrunner/Main.rb @@ -15,6 +15,7 @@ require 'logger' require 'fit4ruby' require 'postrunner/version' +require 'postrunner/RuntimeConfig' require 'postrunner/ActivitiesDB' module PostRunner @@ -36,6 +37,7 @@ module PostRunner return if (args = parse_options(args)).nil? + @cfg = RuntimeConfig.new(@db_dir) execute_command(args) end @@ -117,8 +119,10 @@ check [ | ... ] dump | Dump the content of the FIT file. -import | - Import the provided FIT file(s) into the postrunner database. +import [ | ] + Import the provided FIT file(s) into the postrunner database. If no + file or directory is provided, the directory that was used for the + previous import is being used. delete Delete the activity from the archive. @@ -146,7 +150,7 @@ EOT end def execute_command(args) - @activities = ActivitiesDB.new(@db_dir) + @activities = ActivitiesDB.new(@db_dir, @cfg) case (cmd = args.shift) when 'check' @@ -204,10 +208,15 @@ EOT Log.fatal "Activity references must start with ':': #{a_ref}" end end - end def process_files(files_or_dirs, command) + # If we have no file or directory for the import command, we get the + # most recently used dir from the runtime config. + if files_or_dirs.empty? && command == :import + files_or_dirs = [ @cfg.get_option(:import_dir) ] + end + if files_or_dirs.empty? Log.fatal("You must provide at least one .FIT file name") end @@ -221,6 +230,9 @@ EOT process_file(fod, command) end end + if command == :import + @cfg.set_option(:import_dir, File.dirname(files_or_dirs[-1])) + end end def process_file(file, command) diff --git a/lib/postrunner/RuntimeConfig.rb b/lib/postrunner/RuntimeConfig.rb new file mode 100644 index 0000000..0cc3e05 --- /dev/null +++ b/lib/postrunner/RuntimeConfig.rb @@ -0,0 +1,73 @@ +#!/usr/bin/env ruby -w +# encoding: UTF-8 +# +# = ActivitiesDB.rb -- PostRunner - Manage the data from your Garmin sport devices. +# +# Copyright (c) 2014 by Chris Schlaeger +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of version 2 of the GNU General Public License as +# published by the Free Software Foundation. +# + +require 'yaml' +require 'fit4ruby' + +module PostRunner + + # Simple class to manage runtime configuration options which are persisted + # in a YAML file. + class RuntimeConfig + + # Create a new RC object. + # @param dir [String] the directory to hold the config.yml file. + def initialize(dir) + @options = { + :unit_system => :metric, + :import_dir => nil + } + @config_file = File.join(dir, 'config.yml') + + load_options if File.exist?(@config_file) + end + + # Get a config option value. + # @param name [Symbol] the name of the config option. + # @return [Object] the value of the config option. + def get_option(name) + @options[name] + end + + # Set a config option and update the RC file. + # @param name [Symbol] The name of the config option. + # @param value [Object] The value of the config option. + def set_option(name, value) + @options[name] = value + save_options + end + + private + + def load_options + begin + opts = YAML::load_file(@config_file) + rescue IOError + Log.error "Cannot load config file '#{@config_file}': #{$!}" + end + # Merge the loaded options into the @options hash. + opts.each { |key, value| @options[key] = value } + end + + def save_options + begin + File.write(@config_file, @options.to_yaml) + Log.info "Runtime config file '#{@config_file}' written" + rescue + Log.error "Cannot write config file '#{@config_file}': #{$!}" + end + end + + end + +end + diff --git a/spec/PostRunner_spec.rb b/spec/PostRunner_spec.rb index d3ab129..bb2d600 100644 --- a/spec/PostRunner_spec.rb +++ b/spec/PostRunner_spec.rb @@ -117,6 +117,8 @@ describe PostRunner::Main do it 'should import a FIT file' do postrunner(%w( import FILE1.FIT )) + rc = YAML::load_file(File.join(@db_dir, 'config.yml')) + rc[:import_dir].should == '.' end it 'should check the imported file' do @@ -131,8 +133,8 @@ describe PostRunner::Main do postrunner(%w( list )).index('FILE1.FIT').should be_a(Fixnum) end - it 'should import another FIT file' do - postrunner(%w( import FILE2.FIT )) + it 'should import the other FIT file' do + postrunner(%w( import )) list = postrunner(%w( list )) list.index('FILE1.FIT').should be_a(Fixnum) list.index('FILE2.FIT').should be_a(Fixnum) -- cgit v1.2.3