diff options
author | Guillermo Ramos | 2019-06-24 13:10:02 +0200 |
---|---|---|
committer | Guillermo Ramos | 2019-06-24 17:39:53 +0200 |
commit | 57f8e009519b305294d002784967239e7a41ef5e (patch) | |
tree | 1f76ce42d4dc96e3561dbef035631fdef008a704 | |
parent | 47fb4fc02407def9d9b19a2746983b8c65937e3f (diff) | |
download | tgutils-57f8e009519b305294d002784967239e7a41ef5e.tar.gz |
TgLib::fetch_token
-rw-r--r-- | lib/TgLib.pm | 16 | ||||
-rw-r--r-- | lib/TgLib/Env.pm | 3 | ||||
-rwxr-xr-x | tgrecv | 19 | ||||
-rwxr-xr-x | tgsend | 19 |
4 files changed, 25 insertions, 32 deletions
diff --git a/lib/TgLib.pm b/lib/TgLib.pm index 9be77d7..192d28a 100644 --- a/lib/TgLib.pm +++ b/lib/TgLib.pm @@ -1,6 +1,20 @@ package TgLib; +use TgLib::Env qw<$CONFIG_HOME>; + use parent qw<Exporter>; -our @EXPORT = qw<>; +our @EXPORT = qw<fetch_token>; + +sub fetch_token { + my $token = $ENV{'TGUTILS_TOKEN'}; + unless ($token) { + open(my $cfg, "<", "$CONFIG_HOME/tgutils_token") or return; + $token = <$cfg>; + chomp $token; + close $cfg; + } + $token =~ /^[0-9]+:[a-zA-Z0-9]+$/ or die "Invalid bot token ($token)"; + return $token; +} 1; diff --git a/lib/TgLib/Env.pm b/lib/TgLib/Env.pm index b1e94aa..c019a2c 100644 --- a/lib/TgLib/Env.pm +++ b/lib/TgLib/Env.pm @@ -1,10 +1,11 @@ package TgLib::Env; use parent qw<Exporter>; -our @EXPORT = qw<$HOME $CONFIG_HOME $CACHE_HOME>; +our @EXPORT = qw<$HOME $CONFIG_HOME $CACHE_HOME $TOKEN>; our $HOME = $ENV{'HOME'}; our $CONFIG_HOME = $ENV{'XDG_CONFIG_HOME'} || "$HOME/.config"; our $CACHE_HOME = $ENV{'XDG_CACHE_HOME'} || "$HOME/.cache"; +our $TOKEN = $ENV{'TGUTILS_TOKEN'}; 1; @@ -20,6 +20,7 @@ use JSON qw<encode_json decode_json>; # Local modules use FindBin; use lib "$FindBin::Bin/lib"; +use TgLib qw<fetch_token>; use TgLib::Env qw<$HOME $CONFIG_HOME $CACHE_HOME>; use TgLib::Cache; @@ -40,22 +41,10 @@ GetOptions("token=s" => \$TOKEN, "help" => \$HELP); pod2usage(-verbose => $VERBOSE+1) if $HELP; -# If token was not specified in CLI, try to get it from ENV -$TOKEN ||= $ENV{'TGUTILS_TOKEN'}; - -# If still no token, try to get it from ~/.config/tgutils_token -unless ($TOKEN) { - my $CONFIG = "$CONFIG_HOME/tgutils_token"; - open(my $cfg, "<", $CONFIG) or - pod2usage(-message => "ERROR: Unable to get bot token ($CONFIG: $!).\n", +# If token was not specified in CLI, get it from ENV/file +$TOKEN ||= fetch_token() or + pod2usage(-message => "ERROR: Unable to get bot token ($!).\n", -verbose => 99, -sections => "AUTHENTICATION"); - $TOKEN = <$cfg>; - chomp $TOKEN; - close $cfg; -} - -# Sanity check -$TOKEN =~ /^[0-9]+:[a-zA-Z0-9]+$/ or die "Invalid bot token ($TOKEN)"; my $cache = TgLib::Cache->new; print STDERR "Using cache:\n", Dumper($cache), "\n" if $VERBOSE > 1; @@ -20,6 +20,7 @@ use JSON qw<encode_json>; # Local modules use FindBin; use lib "$FindBin::Bin/lib"; +use TgLib qw<fetch_token>; use TgLib::Env qw<$HOME $CONFIG_HOME $CACHE_HOME>; my $TOKEN; @@ -33,22 +34,10 @@ GetOptions("token=s" => \$TOKEN, "help" => \$HELP); pod2usage(-verbose => $VERBOSE+1) if $HELP or ! @ARGV; -# If token was not specified in CLI, try to get it from ENV -$TOKEN ||= $ENV{'TGUTILS_TOKEN'}; - -# If still no token, try to get it from ~/.config/tgutils_token -unless ($TOKEN) { - my $CONFIG = "$CONFIG_HOME/tgutils_token"; - open(my $cfg, "<", $CONFIG) or - pod2usage(-message => "ERROR: Unable to get bot token ($CONFIG: $!).\n", +# If token was not specified in CLI, get it from ENV/file +$TOKEN ||= fetch_token() or + pod2usage(-message => "ERROR: Unable to get bot token ($!).\n", -verbose => 99, -sections => "AUTHENTICATION"); - $TOKEN = <$cfg>; - chomp $TOKEN; - close $cfg; -} - -# Sanity check -$TOKEN =~ /^[0-9]+:[a-zA-Z0-9]+$/ or die "Invalid bot token ($TOKEN)"; # Read text from stdin undef $/; |