package GitHubBackup;
-use base qw(Class::Accessor::Fast);
use strict;
use warnings;
use utf8;
use Carp qw(croak);
-
-__PACKAGE__->mk_accessors( qw(
- directory
-));
+use File::Spec;
# both hash and hashref are acceptable
sub new {
my $class = shift;
-
my $args = (ref $_[0] eq 'HASH') ? $_[0] : {@_};
- if (! $args->{directory}) {
- $args->{directory} = ".";
- }
- return $class->SUPER::new($args);
+ return bless $args, $class;
}
sub account {
return $self->{repository};
}
+sub directory {
+ my $self = shift;
+ my $args = shift;
+
+ if (defined $args) {
+ $self->{directory} = File::Spec->rel2abs($args);
+ }
+
+ return $self->{directory};
+}
+
sub repos {
my $self = shift;
return $self->{repos} if ($self->{repos});
package GitHubBackup::Repository;
-use base qw(Class::Accessor::Fast);
use strict;
use warnings;
use Carp qw(croak);
use Git::Repository;
use File::chdir;
-
+use File::Spec;
+use File::Path qw(mkpath);
sub new {
my $class = shift;
$args->{clone_url} = $result->{clone_url};
}
- return $class->SUPER::new($args);
+ return bless $args, $class;
}
sub directory {
my $self = shift;
- return $self->{directory}->();
+ my $path = $self->{full_name};
+ if (my $base = $self->{directory}->()) {
+ $path = File::Spec->catfile($base, $path);
+ }
+
+ return $path;
}
sub clone_git {
my $self = shift;
- my $dir = $self->directory .'/'. $self->{full_name};
+ my $dir = $self->directory;
if (-d "$dir") {
local $CWD = $dir;
print "fetch ", $dir, "\n";
}
print "clone ", $dir, "\n";
+ mkpath $dir;
Git::Repository->run(clone => '--mirror' => $self->{clone_url} => $dir);
return $self;
}