Added config system

This commit is contained in:
2019-07-04 22:23:44 +02:00
parent f23e2c0336
commit 06b95e3150
2 changed files with 65 additions and 16 deletions

View File

@ -10,11 +10,59 @@ use Archive::Extract;
sub parse_arguments {
my %toret;
GetOptions (
"verbose+" => \$toret{'verbose'},
"verbose+" => \$toret{'verbose'},
"wowpath=s" => \$toret{'wowpath'},
"baseuri=s" => \$toret{'baseuri'},
"config=s" => \$toret{'config'},
);
return %toret;
}
sub load_config {
my $file = shift;
my $toret;
unless(-e $file) {
my @p = split(/\//, $file);
my $file = pop(@p);
my $path = join('/', @p);
unless(-d $path) {
print 'Will create path: '.$path."\n";
system("mkdir","-p","$path");
}
} else {
$toret = pcurse::import_json($file);
}
$toret = pcurse::sane_defaults($toret);
foreach my $k(keys %{$toret}) {
print "k: $k, val: $toret->{$k}\n";
}
return $toret;
}
sub check_config {
my $conf = shift;
unless($conf->{'wowpath'}) {
print 'Where is your addons installed? (complete path, including AddOns on the end): ';
while(my $line = <>) {
chomp($line);
if(-e $line) {
$conf->{'wowpath'} = $line;
last;
}
print 'You sure? Cannot read that path. Try again: ';
}
}
return $conf;
}
sub sane_defaults {
my $in = shift;
$in->{'baseuri'} = 'http://www.curseforge.com' unless(exists($in->{'baseuri'}));
$in->{'config'} = $ENV{'HOME'}.'/.pcurse/config.json' unless(exists($in->{'config'}));
$in->{'addons'} = $ENV{'HOME'}.'/.pcurse/addons.json' unless(exists($in->{'addons'}));
return $in;
}
sub load_addons {
my $addons_file = shift;
unless(-e $addons_file) {
@ -43,15 +91,15 @@ sub load_addons {
return 0;
}
sub save_addons {
sub save_config {
my $json = JSON->new;
my $addons_file = shift;
my $file = shift;
my $json_data = shift;
my $text = $json->pretty->encode($json_data);
open my $fh, ">", $addons_file or return (0,'Could not open '.$addons_file.' for writing: '.$!);
open my $fh, ">", $file or return (0,'Could not open '.$file.' for writing: '.$!);
print $fh $text;
close $fh;
return (1,$addons_file.' saved successfully');
return (1,$file.' saved successfully');
}
sub import_json {