chdir to the destdir before running rsync
[ikiwiki] / IkiWiki / Plugin / rsync.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::rsync;
3
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
7
8 sub import {
9         hook(type => "getsetup", id => "rsync", call => \&getsetup);
10         hook(type => "checkconfig", id => "rsync", call => \&checkconfig);
11         hook(type => "postrefresh", id => "rsync", call => \&postrefresh);
12 }
13
14 sub getsetup () {
15         return
16                 plugin => {
17                         safe => 0,
18                         rebuild => 0,
19                 },
20                 rsync_command => {
21                         type => "string",
22                         example => "rsync -qa --delete . user\@host:/path/to/docroot/",
23                         description => "command to run to sync updated pages",
24                         safe => 0,
25                         rebuild => 0,
26                 },
27 }
28
29 sub checkconfig {
30         if (! exists $config{rsync_command} ||
31             ! defined $config{rsync_command}) {
32                 error("Must specify rsync_command");
33         }
34 }
35
36 sub postrefresh () {
37         chdir($config{destdir}) || error("chdir: $!");
38         system $config{rsync_command};
39         if ($? == -1) {
40                 error("failed to execute rsync_command: $!");
41         } elsif ($? != 0) {
42                 error(sprintf("rsync_command exited %d", $? >> 8));
43         }
44 }
45
46 1