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   475 use strict;
  1         2  
  1         35  
35 1     1   4 use warnings FATAL => 'all';
  1         2  
  1         39  
36 1     1   5 use experimental 'signatures';
  1         2  
  1         5  
37              
38 1     1   518 use Wireguard::WGmeta::Utils;
  1         3  
  1         72  
39 1     1   6 use constant WIREGUARD_HOME => '/etc/wireguard/';
  1         2  
  1         467  
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 11 sub new($class, @input_arguments) {
  5         9  
  5         13  
  5         8  
67 5         15 my $self = {
68             'input_args' => \@input_arguments
69             };
70             # check if env var is available
71 5 50       17 if (defined($ENV{'WIREGUARD_HOME'})) {
72 5         14 $self->{wireguard_home} = $ENV{'WIREGUARD_HOME'};
73             }
74             else {
75 0         0 $self->{wireguard_home} = WIREGUARD_HOME;
76             }
77 5         7 $self->{wg_meta} = undef;
78 5         11 bless $self, $class;
79 5         24 return $self;
80             }
81              
82             #@returns Wireguard::WGmeta::Wrapper::Config
83 19     19 0 25 sub wg_meta($self) {
  19         33  
  19         24  
84             # (sort of) singleton
85 19 100       47 unless (defined $self->{wg_meta}){
86 5         26 $self->{wg_meta} = Wireguard::WGmeta::Wrapper::Config->new($self->{wireguard_home});
87             }
88 19         68 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 8 sub check_privileges($self) {
  5         8  
  5         8  
120 5 50       111 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         32  
  21         29  
  21         31  
  21         29  
127 21         31 my @arr = @{$ref_array};
  21         64  
128 21 50       37 eval {return $arr[$idx]} or $self->cmd_help();
  21         93  
129             }
130              
131 2     2   3 sub _unknown_attr_handler($attribute, $value) {
  2         4  
  2         4  
  2         2  
132 2         5 my $prefix = substr $attribute, 0, 1;
133 2 100       21 die "`$attribute` is unknown, please add `+` as prefix to add it" unless $prefix eq '+';
134 1         5 return (substr $attribute, 1), $value;
135             }
136              
137             1;