| 1 | # SPDX-FileCopyrightText: SUSE LLC | ||||||||||||||||
| 2 | # SPDX-License-Identifier: GPL-2.0-or-later | ||||||||||||||||
| 3 | |||||||||||||||||
| 4 | package Cavil::CLI::Config; | ||||||||||||||||
| 5 | 7 | ▶ | use Mojo::Base -base, -signatures; | ||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||
| 6 | |||||||||||||||||
| 7 | 7 | ▶ | use Mojo::File qw(path); | ||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||
| 8 | 7 | ▶ | use Mojo::JSON qw(from_json to_json); | ||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||
| 9 | |||||||||||||||||
| 10 | # Persistent per-user settings (the Cavil URL and API token) so they need not be passed on every run. Under | ||||||||||||||||
| 11 | # XDG_CONFIG_HOME, and written 0600 because it holds a credential. | ||||||||||||||||
| 12 | |||||||||||||||||
| 13 | 7 | ▶ | has dir => sub { path($ENV{XDG_CONFIG_HOME} || (($ENV{HOME} // '.') . '/.config'))->child('cavil-cli') }; | ||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=14.2
Condition25%1/4
$ENV{'XDG_CONFIG_HOME'} || ($ENV{'HOME'} // '.') . '/.config'
$ENV{"HOME"} // "."
MC/DC0%0/1
$ENV{'XDG_CONFIG_HOME'} || ($ENV{'HOME'} // '.') . '/.config'
$ENV{'XDG_CONFIG_HOME'}
Truth table50%1/2
A
A= $ENV{'XDG_CONFIG_HOME'}
| |||||||||||||||||
| 14 | |||||||||||||||||
| 15 | 14 | ▶ | sub file ($self) { return $self->dir->child('config.json') } | ||||||||||||||
|
Subroutine
file: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||
| 16 | |||||||||||||||||
| 17 | 6 | ▶ | sub load ($self) { | ||||||||||||||
|
Subroutine
load: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||
| 18 | 6 | my $file = $self->file; | |||||||||||||||
| 19 | 6 | ▶ | return {} unless -f $file; | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 20 | 4 | my $data = eval { from_json($file->slurp) }; | |||||||||||||||
| 21 | 4 | ▶ | return ref $data eq 'HASH' ? $data : {}; | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 22 | } | ||||||||||||||||
| 23 | |||||||||||||||||
| 24 | 3 | ▶ | sub save ($self, $data) { | ||||||||||||||
|
Subroutine
save: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||
| 25 | 3 | ▶ | $self->dir->make_path unless -d $self->dir; | ||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||
| 26 | 3 | my $file = $self->file; | |||||||||||||||
| 27 | 3 | $file->spew(to_json($data)); | |||||||||||||||
| 28 | 3 | chmod 0600, $file->to_string; | |||||||||||||||
| 29 | 3 | return $self; | |||||||||||||||
| 30 | } | ||||||||||||||||
| 31 | |||||||||||||||||
| 32 | 7 | 1; | |||||||||||||||