#!/usr/bin/env perl
binmode
STDOUT,
":encoding(UTF-8)"
;
binmode
STDERR,
":encoding(UTF-8)"
;
@ARGV
=
map
{ Encode::decode(
"UTF-8"
,
$_
) }
@ARGV
;
my
@api_cmds
=
qw(
get_ping get_version get_model get_connections get_completion
get_config get_config_sync get_system get_errors get_discovery
get_deviceid get_upgrade get_ignores get_need
post_ping post_config post_restart post_reset
post_shutdown post_error post_error_clear post_discovery_hint
post_scan post_upgrade post_ignores post_bump
)
;
my
@cmds
= (
@api_cmds
,
qw(
help auth-token=s url=s
folder=s device=s
)
);
my
%Options
=
map
{
$_
=~ s/=.*//
r
=>
undef
}
@cmds
;
GetOptions(\
%Options
,
@cmds
)
or
die
"Error: invalid command line arguments\n"
;
pod2usage(0)
if
(
$Options
{help});
my
@provided_cmds
=
map
{
$Options
{
$_
} ?
$_
: () }
@api_cmds
;
pod2usage(
'Error: Only provide one and only one command'
)
if
(
scalar
@provided_cmds
!= 1);
my
$cmd
=
$provided_cmds
[0];
my
$auth_token
=
$Options
{
'auth-token'
} ||
$ENV
{SYNCTHING_AUTH_TOKEN};
my
$url
=
$Options
{
'url'
} ||
$ENV
{SYNCTHING_URL};
my
$ST
= WebService::Syncthing->new(
auth_token
=>
$auth_token
,
base_url
=>
$url
,
);
my
$rc
= \&{
$cmd
};
$rc
->();
sub
get_ping {
p
$ST
->get_ping();
}
sub
get_version {
p
$ST
->get_version();
}
sub
get_model {
my
$folder
=
$Options
{folder} //
die
"Error: Require --folder\n"
;
p
$ST
->get_folder(
$folder
);
}
sub
get_connections {
p
$ST
->get_connections();
}
sub
get_completion {
my
$device
=
$Options
{device} //
die
"Error: Require --device\n"
;
my
$folder
=
$Options
{folder} //
die
"Error: Require --folder\n"
;
p
$ST
->get_completion(
$device
,
$folder
);
}
sub
get_config {
p
$ST
->get_config();
}
sub
get_config_sync {
p
$ST
->get_config_sync();
}
sub
get_system {
p
$ST
->get_system();
}
sub
get_errors {
p
$ST
->get_errors();
}
sub
get_discovery {
p
$ST
->get_discovery();
}
sub
get_deviceid {
my
$device
=
$Options
{device} //
die
"Error: Require --device\n"
;
p
$ST
->get_deviceid();
}
sub
get_upgrade {
p
$ST
->get_upgrade();
}
sub
get_ignores {
my
$folder
=
$Options
{folder} //
die
"Error: Require --folder\n"
;
p
$ST
->get_ignores(
$folder
);
}
sub
get_need {
my
$folder
=
$Options
{folder} //
die
"Error: Require --folder\n"
;
p
$ST
->get_need(
$folder
);
}