File Coverage

lib/Wireguard/WGmeta/Wrapper/Show.pm
Criterion Covered Total %
statement 23 62 37.1
branch 0 4 0.0
condition n/a
subroutine 8 14 57.1
pod 4 6 66.6
total 35 86 40.7


line stmt bran cond sub pod time code
1             =pod
2              
3             =head1 NAME
4              
5             WGmeta::Wrapper::Show - Class for interfacing `wg show dump` output
6              
7             =head1 SYNOPSIS
8              
9             use Wireguard::WGmeta::Wrapper::Show;
10             my $wg_show = Wireguard::WGmeta::Wrapper::Show->new();
11              
12             =head1 DESCRIPTION
13              
14             This class provides wrapper-functions around the output of L.
15              
16             =head1 EXAMPLES
17              
18             use Wireguard::WGmeta::Wrapper::Show;
19             use Wireguard::WGmeta::Wrapper::Bridge;
20              
21             my ($out, $err) = get_wg_show();
22             my $wg_show = Wireguard::WGmeta::Wrapper::Show->new($out);
23              
24             # get a specific interface section
25             wg_show->get_interface_section('wg0', '');
26              
27             =head1 METHODS
28              
29             =cut
30 1     1   13 use v5.20.0;
  1         4  
31             package Wireguard::WGmeta::Wrapper::Show;
32 1     1   7 use strict;
  1         2  
  1         24  
33 1     1   7 use warnings FATAL => 'all';
  1         2  
  1         33  
34 1     1   6 use experimental 'signatures';
  1         2  
  1         6  
35              
36              
37             our $VERSION = "0.3.3";
38              
39 1     1   118 use constant FALSE => 0;
  1         2  
  1         66  
40 1     1   6 use constant TRUE => 1;
  1         2  
  1         56  
41 1     1   6 use Wireguard::WGmeta::Utils;
  1         2  
  1         64  
42 1     1   456 use Wireguard::WGmeta::Parser::Show;
  1         2  
  1         435  
43              
44             =head3 new($wg_show_dump)
45              
46             Creates a new instance of the show parser
47              
48             B
49              
50             =over 1
51              
52             =item
53              
54             C<$wg_show_dump> Output of the (external) command C.
55              
56             =back
57              
58             B
59              
60             Instance
61              
62             =cut
63 0     0 1   sub new($class, $wg_show_dump) {
  0            
  0            
  0            
64 0           my $self = {
65             'parsed_show' => wg_show_dump_parser($wg_show_dump)
66             };
67              
68 0           bless $self, $class;
69              
70 0           return $self;
71             }
72              
73 0     0 0   sub reload($self, $wg_show_dump){
  0            
  0            
  0            
74 0           $self->{parsed_show} = wg_show_dump_parser($wg_show_dump);
75             }
76              
77             =head3 get_interface_list()
78              
79             Returns a list with all available interface names
80              
81             B
82              
83             B
84              
85             A list with valid interface names.
86              
87             =cut
88 0     0 1   sub get_interface_list($self) {
  0            
  0            
89 0           return sort keys %{$self->{parsed_show}};
  0            
90             }
91              
92             =head3 iface_exists($interface)
93              
94             Simply checks if data is available for a specific interface. Useful to check if an interface is up.
95              
96             B
97              
98             =over 1
99              
100             =item
101              
102             C<$interface> An interface name
103              
104             =back
105              
106             B
107              
108             If yes, returns True else False
109              
110             =cut
111 0     0 1   sub iface_exists($self, $interface) {
  0            
  0            
  0            
112 0           return exists $self->{parsed_show}{$interface};
113             }
114              
115             =head3 get_interface_section($interface, $identifier)
116              
117             Returns a specific section of an interface
118              
119             B
120              
121             =over 1
122              
123             =item
124              
125             C<$interface> A valid interface name, ideally retrieved through L.
126              
127             =item
128              
129             C<$identifier> A valid identifier, if the requested section is a peer this is its public-key, otherwise the interface
130             name again.
131              
132             =back
133              
134             B
135              
136             A hash of the requested section. If non-existent, empty hash.
137              
138             =cut
139 0     0 1   sub get_interface_section($self, $interface, $identifier) {
  0            
  0            
  0            
  0            
140 0 0         if (exists($self->{parsed_show}{$interface}{$identifier})) {
141 0           return %{$self->{parsed_show}{$interface}{$identifier}};
  0            
142             }
143             else {
144 0           return ();
145             }
146             }
147             =head3 get_section_list($interface)
148              
149             Returns a sorted list of all peers belonging to given interface
150              
151             B
152              
153             =over 1
154              
155             =item
156              
157             C<$interface> A valid interface name, ideally retrieved through L.
158              
159             =back
160              
161             B
162              
163             A list of peer public-keys (identifiers), if the interface does not exist -> empty list.
164              
165             =cut
166 0     0 0   sub get_section_list($self, $interface) {
  0            
  0            
  0            
167 0 0         if (exists($self->{parsed_show}{$interface})) {
168 0           return sort keys %{$self->{parsed_show}{$interface}};
  0            
169             }
170             else {
171 0           return {};
172             }
173             }
174              
175             1;