Skip to content

Commit

Permalink
Merge branch 'feature/config' into develop
Browse files Browse the repository at this point in the history
* feature/config:
  Minor cosmetics.
  Updated README and default config file.
  Added default configuration file.
  Added configuration file management.
  Added default YAML config file location, and option to use an alternate config file.
  • Loading branch information
Guillaume-Jean Herbiet committed Sep 29, 2010
2 parents 2d89092 + cfdea8e commit 50da235
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 4 deletions.
21 changes: 19 additions & 2 deletions README.textile
Expand Up @@ -16,11 +16,28 @@ Then for every file *YOU* have decided, *dropsync* will do the following:

h2. Usage

TO BE COMPLETED
<pre>
dropsync [-h|--help] [--version] [--verbose|-v] [--debug|-d]
[--config-file|-f <path to config file>]

--help, -h : Print this help, then exit
--version : Print the script version, then exit
--verbose, -v : Enable verbose mode
--debug, -d : Enable debug mode

--config-file, -f : Alternate location for the configuration file
(Default: $HOME/.dropsyncrc.yaml)
</pre>

h2. Configuration file

TO BE COMPLETED
*dropsync* uses a YAML-formatted configuration file. It is mainly used to specify the list of files you would like to sync with your dropsync folder.

The default location for this configuration file is @$HOME/.dropsyncrc.yaml@, where @$HOME@ is your home folder. This can be overriden using the @--config-file@ or @-f@ option when calling the @dropsync@ script.

You can also use it to change some parameters (like the location of your Dropbox folder, or the name of the dropsync folder) to non-default values.

An example of configuration is given in the @dropsyncrc.yaml@ file.

h2. Planned (i.e. someday) features

Expand Down
70 changes: 68 additions & 2 deletions dropsync
Expand Up @@ -32,6 +32,8 @@ use warnings;
#
use Getopt::Long; # To easily retrieve arguments from command-line
use Term::ANSIColor qw(:constants); # Colored output for the terminal
use YAML; # To parse the config file
use Data::Dumper; # Dump data structues (mainly used for debug)

#-----------------------------------------------------------------------------

Expand Down Expand Up @@ -64,6 +66,11 @@ my $DEBUG = 0; # Debug mode
#
# Script specific variables
#
my $config_file = $ENV{'HOME'}."/.dropsyncrc.yaml"; # Default location of config file
my $config; # Hashref of the config options

my $dropbox_location = $ENV{'HOME'}."/Dropbox"; # DropBox folder location
my $dropsync_folder = "dropsync"; # dropsync folder name

#-----------------------------------------------------------------------------

Expand All @@ -75,7 +82,8 @@ $OPTIONS_VALID = GetOptions(
'help|h' => sub { USAGE(); },
'version' => sub { VERSION_MESSAGE(); },
'verbose|v+' => \$VERBOSE,
'debug|d' => sub { $VERBOSE = 1; $DEBUG = 1; }
'debug|d' => sub { $VERBOSE = 1; $DEBUG = 1; },
'config-file|f=s' => \$config_file,
);

unless ($OPTIONS_VALID) {
Expand All @@ -86,12 +94,16 @@ unless ($OPTIONS_VALID) {
sub USAGE {
my $exitval = defined($_) ? $_ : 0;
print <<EOF;
$COMMAND [-h|--help] [--version]
$COMMAND [-h|--help] [--version] [--verbose|-v] [--debug|-d]
[--config-file|-f <path to config file>]
--help, -h : Print this help, then exit
--version : Print the script version, then exit
--verbose, -v : Enable verbose mode
--debug, -d : Enable debug mode
--config-file, -f : Alternate location for the configuration file
(Default: \$HOME/.dropsyncrc.yaml)
EOF
exit $exitval;
}
Expand All @@ -112,11 +124,65 @@ EOF
# Core code
#

#
# Read configuration from the specified file
$config = read_config_file($config_file);
#

#
# dropsync folder location management
#
my $src_folder = setup_src_folder();

#
# Files backup/update
# TODO: implement actual backup/update of files
#
ohai "Syncing selected files." if $VERBOSE;
for my $file (@{$config->{'files'}}) {
say $file if $DEBUG;
}

#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Additional functions
#
sub read_config_file {
my $file = shift;

# Try to open the configuration file or die
unless (-f $file) {
fail "Configuration file $file does not exist!"; exit 2;
} elsif ($VERBOSE) { ohai "Reading configuration from $file."; }

# Read the configuration to a hash
# NOTE: config_array and config_string should be empty
my ($config_hash, $config_array, $config_string) = YAML::LoadFile($file);
ohai "Read configuration:\n", Dumper($config_hash, $config_array, $config_string) if $DEBUG;
oops "Some unnamed variables were found while reading config file $_. They will be ignored."
if (defined $config_array || defined $config_string);
return $config_hash;
}

sub setup_src_folder {
# Update the location of the Dropbox location
# and the dropsync folder if required
$dropbox_location = $config->{'dropbox_location'}
if (exists $config->{'dropbox_location'} && !$config->{'dropbox_location'} eq "");
$dropsync_folder = $config->{'dropsync_folder'}
if (exists $config->{'dropsync_folder'} && !$config->{'dropsync_folder'} eq "");

# Create the dropsync folder if required
my $src = $dropbox_location."/".$dropsync_folder;
unless (-d $src) {
mkdir $src;
ohai "Creating non-existing dropsync folder at $src."
if $VERBOSE;
}
say "Using ", BOLD, "$src", RESET, " as source folder." if $VERBOSE;
return $src;
}

#-----------------------------------------------------------------------------

8 changes: 8 additions & 0 deletions dropsyncrc.yaml
@@ -0,0 +1,8 @@
---
dropbox_location: "/Users/me/Dropbox"
dropsync_folder: "dropsync"
files:
- "File 1"
- "File 2"
- "File 3"
---

0 comments on commit 50da235

Please sign in to comment.