From 65a731a71b96920ca93fab12e8187ce0287104a4 Mon Sep 17 00:00:00 2001 From: mischa Date: Sun, 24 Sep 2023 12:59:55 +0000 Subject: [PATCH] added config file logic --- .gitignore | 1 + tibber.conf-sample | 1 + tibber.pl | 15 ++++++++++++++- 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 tibber.conf-sample diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bdbf511 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +tibber.conf diff --git a/tibber.conf-sample b/tibber.conf-sample new file mode 100644 index 0000000..5a4d97a --- /dev/null +++ b/tibber.conf-sample @@ -0,0 +1 @@ +token = TOKENTOKEN diff --git a/tibber.pl b/tibber.pl index c5a7e89..64d2826 100755 --- a/tibber.pl +++ b/tibber.pl @@ -3,12 +3,25 @@ use 5.024; use strict; use warnings; use autodie; +use Config::Tiny; use Getopt::Long; use HTTP::Tiny; use JSON::PP; use Data::Dumper; -my $TOKEN = ""; +GetOptions( + "config=s" => \(my $CONFIG), +); + +my $USAGE = <<"END_USAGE"; +Usage: $0 [-c config] +Options: +-c | --config text (no default) +END_USAGE + +my @config_files = $CONFIG || grep { -e } ('./_tibber.conf', './.tibber.conf', './tibber.conf', "$ENV{'HOME'}/_tibber.conf", "$ENV{'HOME'}/.tibber.conf", "$ENV{'HOME'}/tibber.conf"); +my $config = Config::Tiny->read($config_files[-1], 'utf8'); +my $TOKEN = $config->{$APPLICATION}->{token} || die("$USAGE\nError: TOKEN not found.\n"); my $http = HTTP::Tiny->new; my %HEADERS = ("Content-Type" => "application/json", "Authorization" => "Bearer $TOKEN");