From 155e7a317b6ee5325caa1d7aa5d8b2a2495ebe67 Mon Sep 17 00:00:00 2001 From: Guillaume-Jean Herbiet Date: Tue, 28 Sep 2010 13:04:42 +0200 Subject: [PATCH 1/5] Added default YAML config file location, and option to use an alternate config file. --- dropsync | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/dropsync b/dropsync index 556ebd8..ebf0bd1 100755 --- a/dropsync +++ b/dropsync @@ -32,6 +32,7 @@ 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 #----------------------------------------------------------------------------- @@ -64,6 +65,8 @@ my $DEBUG = 0; # Debug mode # # Script specific variables # +my $config_file = $ENV{'HOME'}."/.dropsyncrc.yaml"; # Default location of config file +my %config; # Configuration read from file #----------------------------------------------------------------------------- @@ -75,7 +78,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) { @@ -86,12 +90,16 @@ unless ($OPTIONS_VALID) { sub USAGE { my $exitval = defined($_) ? $_ : 0; print <] --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; } From e559d651d82f0211fc719e744ba5cefa0bcdd707 Mon Sep 17 00:00:00 2001 From: Guillaume-Jean Herbiet Date: Tue, 28 Sep 2010 19:23:26 +0200 Subject: [PATCH 2/5] Added configuration file management. --- dropsync | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/dropsync b/dropsync index ebf0bd1..7102b9c 100755 --- a/dropsync +++ b/dropsync @@ -33,6 +33,7 @@ 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) #----------------------------------------------------------------------------- @@ -66,7 +67,10 @@ my $DEBUG = 0; # Debug mode # Script specific variables # my $config_file = $ENV{'HOME'}."/.dropsyncrc.yaml"; # Default location of config file -my %config; # Configuration read from file +my $config; + +my $dropbox_location = $ENV{'HOME'}."/Dropbox"; +my $dropsync_folder = "dropsync"; #----------------------------------------------------------------------------- @@ -120,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; +} #----------------------------------------------------------------------------- From e94b74c2a72da0904097d7db2a935712cd3996db Mon Sep 17 00:00:00 2001 From: Guillaume-Jean Herbiet Date: Tue, 28 Sep 2010 19:23:52 +0200 Subject: [PATCH 3/5] Added default configuration file. --- dropsyncrc.yaml | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 dropsyncrc.yaml diff --git a/dropsyncrc.yaml b/dropsyncrc.yaml new file mode 100644 index 0000000..31c5fba --- /dev/null +++ b/dropsyncrc.yaml @@ -0,0 +1,8 @@ +--- +dropbox_location: "" +dropsync_folder: "dropsync" +files: + - "File 1" + - "File 2" + - "File 3" +--- From 56cc241c4099178c02a642faaad61fa06952ed84 Mon Sep 17 00:00:00 2001 From: Guillaume-Jean Herbiet Date: Tue, 28 Sep 2010 19:31:33 +0200 Subject: [PATCH 4/5] Updated README and default config file. --- README.textile | 21 +++++++++++++++++++-- dropsyncrc.yaml | 2 +- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/README.textile b/README.textile index 223724f..2757b94 100644 --- a/README.textile +++ b/README.textile @@ -16,11 +16,28 @@ Then for every file *YOU* have decided, *dropsync* will do the following: h2. Usage -TO BE COMPLETED +
+dropsync [-h|--help] [--version] [--verbose|-v] [--debug|-d]
+    [--config-file|-f ]
+
+    --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)
+
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 diff --git a/dropsyncrc.yaml b/dropsyncrc.yaml index 31c5fba..6397e02 100644 --- a/dropsyncrc.yaml +++ b/dropsyncrc.yaml @@ -1,5 +1,5 @@ --- -dropbox_location: "" +dropbox_location: "/Users/me/Dropbox" dropsync_folder: "dropsync" files: - "File 1" From cfdea8ec3477d952a7db7e5a458860178dcfaf96 Mon Sep 17 00:00:00 2001 From: Guillaume-Jean Herbiet Date: Wed, 29 Sep 2010 09:14:52 +0200 Subject: [PATCH 5/5] Minor cosmetics. --- dropsync | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dropsync b/dropsync index 7102b9c..febb867 100755 --- a/dropsync +++ b/dropsync @@ -67,10 +67,10 @@ my $DEBUG = 0; # Debug mode # Script specific variables # my $config_file = $ENV{'HOME'}."/.dropsyncrc.yaml"; # Default location of config file -my $config; +my $config; # Hashref of the config options -my $dropbox_location = $ENV{'HOME'}."/Dropbox"; -my $dropsync_folder = "dropsync"; +my $dropbox_location = $ENV{'HOME'}."/Dropbox"; # DropBox folder location +my $dropsync_folder = "dropsync"; # dropsync folder name #----------------------------------------------------------------------------- @@ -159,7 +159,7 @@ sub read_config_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); + 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;