-
Notifications
You must be signed in to change notification settings - Fork 0
/
github-upload
executable file
·61 lines (52 loc) · 1.72 KB
/
github-upload
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/perl
use strict;
use warnings;
use vars qw(%creds);
use File::Basename;
BEGIN {
-e "github-upload.localdefs" and do "github-upload.localdefs"; # you can put your "use lib"s there
}
use Net::GitHub::Upload;
BEGIN {
unless (Net::GitHub::Upload->can("delete")) {
die "Your installation of Net::GitHub::Upload does not support deletion\n".
"of files. Please grab, for now, a supported version from\n".
" https://github.com/yath/net-github-upload-perl\n".
"and check out the 'deletefile' branch.\n";
}
}
# enough for compilation phase
use constant CREDS => "github-upload.creds";
unless (my $ret = do +CREDS) {
die "Unable to parse @{[CREDS]}: $@" if $@;
die "Unable to import @{[CREDS]}: $!" unless defined $ret;
die "Unable to run @{[CREDS]}" unless $ret;
}
my ($filename, $repo) = @ARGV;
die "Usage: $0 <filename> <repository>" unless $filename && $repo;
# The MIME content type may be specified in brackets after the
# file name. If the file name *really* contains square brackets
# one can just append [].
my $mimetype = do {
$filename =~ s/(.*)\[(.*)\]$/$1/ ? $2 : undef
};
# set it up
my $github = Net::GitHub::Upload->new(
login => $creds{login},
token => $creds{token},
);
# delete leftover stuff
if (my $files = $github->list_files($repo)) {
foreach my $file (@$files) {
my $fbname = basename($filename);
next unless $file->{name} eq $fbname;
$github->delete(repos => $repo, fileinfo => $file) or die;
}
}
# and upload
warn "Uploading \"$filename\" (content type: @{[$mimetype||'unknown']}) to $repo\n";
$github->upload(
repos => $repo,
file => $filename,
content_type => $mimetype,
) or die;