aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbin/events2md.pl95
1 files changed, 48 insertions, 47 deletions
diff --git a/bin/events2md.pl b/bin/events2md.pl
index b0772d3..dfa1b2b 100755
--- a/bin/events2md.pl
+++ b/bin/events2md.pl
@@ -8,6 +8,15 @@ use Feature::Compat::Try;
use FindBin qw($Bin);
use lib "$Bin/../lib";
+use Getopt::Complete (
+ 'output' => undef,
+ 'skeldir' => 'directories',
+ 'username' => undef,
+ 'password' => undef,
+ 'locale' => undef,
+ 'timezone' => undef,
+ '<>' => undef,
+);
use IO::Interactive::Tiny;
use Log::Any qw($log);
use Log::Any::Adapter;
@@ -29,22 +38,15 @@ else {
use Log::Any::Adapter ( 'Stderr', default_level => 'info' );
}
-# set defaults and parse command-line options
-my ($BASE_URI, $CALENDAR_URI, $SKELDIR, $OUTPUT_FILE, $CALENDAR_LANG,
- $CALENDAR_TIME_ZONE, %GROUPWARE_OPTIONS
-);
-$BASE_URI = $ENV{CAL_DAV_URL_BASE};
-$CALENDAR_URI = $ENV{CAL_DAV_URL_CALENDAR};
-$SKELDIR = $ENV{SKELDIR} || "$Bin/../templates";
-$BASE_URI ||= shift @ARGV
- if @ARGV;
-$CALENDAR_URI ||= shift @ARGV
- if @ARGV;
-$OUTPUT_FILE = shift @ARGV
- if @ARGV;
-$CALENDAR_LANG = $ENV{CAL_LANG} || setlocale(LC_TIME);
-$CALENDAR_TIME_ZONE
- = DateTime::TimeZone->new( name => ( $ENV{CAL_TIME_ZONE} || 'local' ), );
+# collect settings from command-line options and defaults
+my $SKELDIR = $ARGS{skeldir} || $ENV{SKELDIR} || "$Bin/../templates";
+my $BASE_URI = $ARGS{'<>'}[0] || $ENV{CAL_DAV_URL_BASE};
+my $CALENDAR_URI = $ARGS{'<>'}[1] || $ENV{CAL_DAV_URL_CALENDAR};
+my $USERNAME = $ARGS{username} || $ENV{CAL_DAV_USER};
+my $PASSWORD = $ARGS{password} || $ENV{CAL_DAV_PASS};
+my $LOCALE = $ARGS{locale} || $ENV{CAL_LANG};
+my $TIME_ZONE = $ARGS{timezone};
+my $OUTPUT_FILE = $ARGS{output};
# extend DateTime locale with form LONGER
# * omit year and second
@@ -65,7 +67,7 @@ my %at = (
ru => " 'в' ",
zh => "'在'",
);
-my $dt_locale = DateTime::Locale->load($CALENDAR_LANG);
+my $dt_locale = DateTime::Locale->load( $LOCALE || setlocale(LC_TIME) );
my ( $locale, $lang ) = $dt_locale->code =~ /^((\w+)(?:-\w+)?)/;
my $dt = DateTime->now( locale => $dt_locale );
my %dt_locale_data = $dt_locale->locale_data;
@@ -80,9 +82,11 @@ $dt_locale_data{time_format_medium} = $dt->locale->format_for('Hm');
$dt_locale_data{datetime_format_medium}
=~ s/^\{1\}\K,? (?=\{0\}$)/$at{$lang}/
if $at{$lang};
-%GROUPWARE_OPTIONS = (
+my %GROUPWARE_OPTIONS = (
dt_locale => DateTime::Locale::FromData->new( \%dt_locale_data ),
- dt_time_zone => $CALENDAR_TIME_ZONE,
+ dt_time_zone => DateTime::TimeZone->new(
+ name => ( $ARGS{timezone} || 'local' ),
+ ),
);
$log->infof(
'Will use locale %s and time zone %s',
@@ -91,38 +95,36 @@ $log->infof(
);
# resolve calendar URIs
-my ( $base_uri, $calendar_uri, $calendar );
-$base_uri = URI->new($BASE_URI)
- if ($BASE_URI);
-$base_uri
- or $log->fatal('required base URI not provided') && exit 2;
-$base_uri->scheme
- or $base_uri->scheme('file');
-
-if ( $base_uri->scheme eq 'http' or $base_uri->scheme eq 'https' ) {
- $log->infof( 'will use base URI %s', $base_uri );
- $calendar_uri = URI->new( $CALENDAR_URI || $base_uri );
- $calendar_uri and $calendar_uri->authority
+my $calendar;
+$BASE_URI = URI->new($BASE_URI)
+ or $log->fatal('failed to parse required base URI') && exit 2;
+$BASE_URI->scheme
+ or $BASE_URI->scheme('file');
+
+if ( $BASE_URI->scheme eq 'http' or $BASE_URI->scheme eq 'https' ) {
+ $log->infof( 'will use base URI %s', $BASE_URI );
+ $CALENDAR_URI = URI->new( $CALENDAR_URI || $BASE_URI );
+ $CALENDAR_URI and $CALENDAR_URI->authority
or $log->fatal('bad calendar URI: must be an internet URI') && exit 2;
- $base_uri->eq($calendar_uri) and $calendar_uri = undef
- or $log->infof( 'will use calendar URI %s', $calendar_uri );
+ $BASE_URI->eq($CALENDAR_URI) and $CALENDAR_URI = undef
+ or $log->infof( 'will use calendar URI %s', $CALENDAR_URI );
my $session = Object::Groupware::DAV->new(
- user => $ENV{CAL_DAV_USER},
- pass => $ENV{CAL_DAV_PASS},
- uri => $base_uri,
+ user => $USERNAME,
+ pass => $PASSWORD,
+ uri => $BASE_URI,
%GROUPWARE_OPTIONS,
);
- $calendar = $session->get($calendar_uri);
+ $calendar = $session->get($CALENDAR_URI);
}
-elsif ( $base_uri->scheme eq 'file' ) {
- defined $base_uri->file
+elsif ( $BASE_URI->scheme eq 'file' ) {
+ defined $BASE_URI->file
or $log->fatal('bad base URI: cannot open file') && exit 2;
- $log->infof( 'will use base URI %s', $base_uri );
+ $log->infof( 'will use base URI %s', $BASE_URI );
# parse local calendar data
$log->debug('parse local calendar data...');
- my $path = path( $base_uri->file );
+ my $path = path( $BASE_URI->file );
if ( $path->is_file ) {
$calendar = Object::Groupware::Calendar->new(
filename => "$path",
@@ -161,11 +163,10 @@ my $span = DateTime::Span->from_datetimes( start => $start, end => $end );
my @events = $calendar->events($span);
# serialize calendar view
-my $output_path;
if ($OUTPUT_FILE) {
- $output_path = path($OUTPUT_FILE);
- $output_path->parent->mkpath;
- $output_path->remove;
+ $OUTPUT_FILE = path($OUTPUT_FILE);
+ $OUTPUT_FILE->parent->mkpath;
+ $OUTPUT_FILE->remove;
}
my %vars;
@@ -185,8 +186,8 @@ my $template = Text::Xslate->new(
my $content = $template->render( 'list', \%vars );
-if ($output_path) {
- $output_path->append_utf8($content);
+if ($OUTPUT_FILE) {
+ $OUTPUT_FILE->append_utf8($content);
}
else {
print $content;