Add --setup to create a config file

tags/patrix-0.1.4-1
Daniel Berteaud 7 years ago
parent e74a1a1431
commit c305f14349
  1. 16
      scripts/patrix

@ -37,6 +37,7 @@ GetOptions(
"get-access-token" => \$opt->{'get-access-token'}, "get-access-token" => \$opt->{'get-access-token'},
"get-room-list" => \$opt->{'get-room-list'}, "get-room-list" => \$opt->{'get-room-list'},
"get-room-id" => \$opt->{'get-room-id'}, "get-room-id" => \$opt->{'get-room-id'},
"setup" => \$opt->{setup},
"config=s" => \$opt->{conf}, "config=s" => \$opt->{conf},
"invite=s@" => \$opt->{invite}, "invite=s@" => \$opt->{invite},
"name=s" => \$opt->{name}, "name=s" => \$opt->{name},
@ -59,7 +60,8 @@ if ($opt->{conf} && -e $opt->{conf}){
# alias for --action=foo is --foo # alias for --action=foo is --foo
foreach my $action (qw(send-msg send-notice send-file create-room modify-room foreach my $action (qw(send-msg send-notice send-file create-room modify-room
delete-room-alias get-access-token get-room-list get-room-id)){ delete-room-alias get-access-token get-room-list get-room-id
setup)){
if ($opt->{$action}){ if ($opt->{$action}){
$opt->{action} = $action; $opt->{action} = $action;
last; last;
@ -492,6 +494,15 @@ sub del_room_alias {
die "Error removing the alias\n" unless ($resp->is_success); die "Error removing the alias\n" unless ($resp->is_success);
} }
# Write settings in a config file
sub setup {
my $cfg = Config::Simple->new($opt->{conf});
foreach my $param (qw(access_token server room)){
$cfg->param($param, $opt->{$param}) if ($opt->{$param});
}
$cfg->save;
}
# If we ask for a new access token, then we must login, and ignore any # If we ask for a new access token, then we must login, and ignore any
# access_token from the config file # access_token from the config file
$opt->{access_token} = undef if ($opt->{action} eq 'get-access-token'); $opt->{access_token} = undef if ($opt->{action} eq 'get-access-token');
@ -532,6 +543,9 @@ my $must_logout = ($opt->{access_token} || $opt->{action} eq 'get-access-token')
if (!$opt->{access_token}){ if (!$opt->{access_token}){
login(); login();
} }
if ($opt->{action} eq 'setup'){
setup();
}
if ($opt->{action} eq 'get-access-token'){ if ($opt->{action} eq 'get-access-token'){
print $opt->{access_token} . "\n"; print $opt->{access_token} . "\n";
} }

Loading…
Cancel
Save