summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorChris Schlaeger <chris@linux.com>2014-08-19 22:17:38 +0200
committerChris Schlaeger <chris@linux.com>2014-08-19 22:17:38 +0200
commit03484e8eb8f4b74806b2043af6d83a230b5ceb97 (patch)
tree21ed87c6a144ed103194db8f03a22703a6b1c1bf /lib
parent420d3bc52169969196f9c6d9eda58f40b9e47893 (diff)
downloadpostrunner-03484e8eb8f4b74806b2043af6d83a230b5ceb97.zip
Adding support for a runtime config file.
Diffstat (limited to 'lib')
-rw-r--r--lib/postrunner/ActivitiesDB.rb5
-rw-r--r--lib/postrunner/Main.rb20
-rw-r--r--lib/postrunner/RuntimeConfig.rb73
3 files changed, 92 insertions, 6 deletions
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 [ <fit file> | <ref> ... ]
dump <fit file> | <ref>
Dump the content of the FIT file.
-import <fit file> | <directory>
- Import the provided FIT file(s) into the postrunner database.
+import [ <fit file> | <directory> ]
+ 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 <ref>
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 <cs@taskjuggler.org>
+#
+# 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
+