File Coverage

lib/Wireguard/WGmeta/Cli/Commands/Command.pm
Criterion Covered Total %
statement 51 62 82.2
branch 7 10 70.0
condition n/a
subroutine 10 12 83.3
pod 4 5 80.0
total 72 89 80.9


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Cli::Command - Base class for commands
4              
5             =head1 DESCRIPTION
6              
7             Base class for the individual CLI commands. A command takes care of (possible) input arguments and performs actions accordingly.
8              
9             =head1 SYNOPSIS
10              
11             A command must implement at least
12              
13             package Wireguard::WGmeta::Cli::Commands::YourCommand;
14              
15             use experimental 'signatures';
16             use parent 'Wireguard::WGmeta::Cli::Commands::Command';
17              
18             # is called from the router
19             sub entry_point($self) {
20             ...
21             }
22              
23             # show cmd specific help
24             sub cmd_help($self) {
25             ...
26             }
27              
28             =head1 METHODS
29              
30             =cut
31              
32              
33             package Wireguard::WGmeta::Cli::Commands::Command;
34 1     1   460 use strict;
  1         3  
  1         32  
35 1     1   5 use warnings FATAL => 'all';
  1         4  
  1         46  
36 1     1   5 use experimental 'signatures';
  1         2  
  1         4  
37              
38 1     1   529 use Wireguard::WGmeta::Utils;
  1         3  
  1         73  
39 1     1   7 use constant WIREGUARD_HOME => '/etc/wireguard/';
  1         2  
  1         489  
40              
41             =head2 new(@input_arguments)
42              
43             Creates a new Command instance. The following data is accessible through C<$self> after calling:
44              
45             my $commandline_args = $self->{input_args}; # reference to array of input arguments
46             my $wireguard_home = $self->{wireguard_home}; # Path to wireguard home dir
47              
48             Please also take note of the effect of environment vars: L
49              
50             B
51              
52             =over 1
53              
54             =item
55              
56             C<@input_arguments> List of input args (except C)
57              
58             =back
59              
60             B
61              
62             An instance of a Command
63              
64             =cut
65              
66 5     5 1 10 sub new($class, @input_arguments) {
  5         8  
  5         15  
  5         7  
67 5         14 my $self = {
68             'input_args' => \@input_arguments
69             };
70             # check if env var is available
71 5 50       49 if (defined($ENV{'WIREGUARD_HOME'})) {
72 5         15 $self->{wireguard_home} = $ENV{'WIREGUARD_HOME'};
73             }
74             else {
75 0         0 $self->{wireguard_home} = WIREGUARD_HOME;
76             }
77 5         11 $self->{wg_meta} = undef;
78 5         11 bless $self, $class;
79 5         31 return $self;
80             }
81              
82             #@returns Wireguard::WGmeta::Wrapper::Config
83 20     20 0 33 sub wg_meta($self) {
  20         28  
  20         27  
84             # (sort of) singleton
85 20 100       48 unless (defined $self->{wg_meta}){
86 5         32 $self->{wg_meta} = Wireguard::WGmeta::Wrapper::Config->new($self->{wireguard_home});
87             }
88 20         81 return $self->{wg_meta};
89             }
90              
91             =head2 entry_point()
92              
93             Method called from L
94              
95             =cut
96 0     0 1 0 sub entry_point($self) {
  0         0  
  0         0  
97 0         0 die 'Please instantiate the actual implementation';
98             }
99              
100             =head2 cmd_help()
101              
102             Stub for specific cmd help. Is expected to terminate the command by calling C
103             and print the help content directly to I.
104              
105             =cut
106 0     0 1 0 sub cmd_help($self) {
  0         0  
  0         0  
107 0         0 die 'Please instantiate the actual implementation';
108             }
109              
110             =head2 check_privileges()
111              
112             Check if the user has r/w access to C.
113              
114             B
115              
116             Exception if the user has insufficient privileges .
117              
118             =cut
119 5     5 1 9 sub check_privileges($self) {
  5         11  
  5         6  
120 5 50       114 if (not -w $self->{wireguard_home}) {
121 0         0 my $username = getpwuid($<);
122 0         0 die "Insufficient privileges - `$username` has no rw permissions to `$self->{wireguard_home}`. You probably forgot `sudo`";
123             }
124             }
125              
126 21     21   34 sub _retrieve_or_die($self, $ref_array, $idx) {
  21         30  
  21         30  
  21         57  
  21         27  
127 21         34 my @arr = @{$ref_array};
  21         57  
128 21 50       34 eval {return $arr[$idx]} or $self->cmd_help();
  21         92  
129             }
130              
131 2     2   5 sub _unknown_attr_handler($attribute, $value) {
  2         3  
  2         4  
  2         3  
132 2         5 my $prefix = substr $attribute, 0, 1;
133 2 100       22 die "`$attribute` is unknown, please add `+` as prefix to add it" unless $prefix eq '+';
134 1         7 return (substr $attribute, 1), $value;
135             }
136              
137             1;