| 1 | package Bot::Cobalt::Frontend::RC; | ||
| 2 | $Bot::Cobalt::Frontend::RC::VERSION = '0.021003'; | ||
| 3 | 2 | ▶ | use strictures 2; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 2 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 2 | ▶ | use Cwd (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | ▶ | use File::Spec (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 2 | ▶ | use Try::Tiny; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | 0 | ▶ | use Bot::Cobalt::Serializer; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | |||
| 14 | use parent 'Exporter::Tiny'; | ||
| 15 | |||
| 16 | our @EXPORT_OK = qw/ | ||
| 17 | rc_read | ||
| 18 | rc_write | ||
| 19 | /; | ||
| 20 | |||
| 21 | sub rc_read { | ||
| 22 | my ($rcfile) = @_; | ||
| 23 | croak "rc_read needs a rcfile path" unless $rcfile; | ||
| 24 | |||
| 25 | my $generic_crappy_err = sub { | ||
| 26 | warn | ||
| 27 | "Errors reported during rcfile parse\n", | ||
| 28 | "You may have an old, incompatible, or broken rcfile.\n", | ||
| 29 | "Path: $rcfile\n", | ||
| 30 | "Try running cobalt2-installer\n" ; | ||
| 31 | }; | ||
| 32 | |||
| 33 | my $rc_err; | ||
| 34 | my $rc_h = try { | ||
| 35 | Bot::Cobalt::Serializer->new->readfile($rcfile) | ||
| 36 | } catch { | ||
| 37 | $generic_crappy_err->(); | ||
| 38 | $rc_err = $_; | ||
| 39 | undef | ||
| 40 | } or croak "Could not rc_read(); readfile said $rc_err"; | ||
| 41 | |||
| 42 | unless ($rc_h && ref $rc_h eq 'HASH') { | ||
| 43 | $generic_crappy_err->(); | ||
| 44 | croak "rc_read ($rcfile) expected to receive a hash" | ||
| 45 | } | ||
| 46 | |||
| 47 | my ($BASE, $ETC, $VAR) = @$rc_h{'BASE', 'ETC', 'VAR'}; | ||
| 48 | |||
| 49 | unless ($BASE && $ETC && $VAR) { | ||
| 50 | warn "rc_read; could not find BASE, ETC, VAR\n"; | ||
| 51 | warn "BASE: $BASE\nETC: $ETC\nVAR: $VAR\n"; | ||
| 52 | croak "Cannot continue without a valid rcfile" | ||
| 53 | } | ||
| 54 | |||
| 55 | return ($BASE, $ETC, $VAR) | ||
| 56 | } | ||
| 57 | |||
| 58 | sub rc_write { | ||
| 59 | my ($rcfile, $basepath) = @_; | ||
| 60 | croak "rc_write needs rc file path and base directory path" | ||
| 61 | unless $rcfile and $basepath; | ||
| 62 | |||
| 63 | unless ( File::Spec->file_name_is_absolute($basepath) ) { | ||
| 64 | my $homedir = $ENV{HOME} || Cwd::cwd(); | ||
| 65 | $basepath = File::Spec->catdir( $homedir, $basepath ); | ||
| 66 | } | ||
| 67 | |||
| 68 | my $rc_h = { | ||
| 69 | BASE => $basepath, | ||
| 70 | ETC => File::Spec->catdir( $basepath, 'etc' ), | ||
| 71 | VAR => File::Spec->catdir( $basepath, 'var' ), | ||
| 72 | }; | ||
| 73 | |||
| 74 | Bot::Cobalt::Serializer->new->writefile( | ||
| 75 | $rcfile, $rc_h | ||
| 76 | ); | ||
| 77 | |||
| 78 | $basepath | ||
| 79 | } | ||
| 80 | |||
| 81 | 1; | ||
| 82 | __END__ | ||