File Coverage

lib/Wireguard/WGmeta/Cli/Commands/Set.pm
Criterion Covered Total %
statement 74 82 90.2
branch 9 12 75.0
condition n/a
subroutine 11 12 91.6
pod 2 2 100.0
total 96 108 88.8


line stmt bran cond sub pod time code
1             package Wireguard::WGmeta::Cli::Commands::Set;
2 1     1   6 use strict;
  1         2  
  1         32  
3 1     1   6 use warnings FATAL => 'all';
  1         1  
  1         62  
4 1     1   7 use experimental 'signatures';
  1         2  
  1         15  
5              
6 1     1   92 use parent 'Wireguard::WGmeta::Cli::Commands::Command';
  1         2  
  1         4  
7 1     1   75 use Wireguard::WGmeta::Wrapper::Config;
  1         2  
  1         21  
8 1     1   5 use constant TRUE => 1;
  1         2  
  1         73  
9 1     1   17 use constant FALSE => 0;
  1         2  
  1         715  
10              
11 3     3 1 6 sub entry_point($self) {
  3         6  
  3         4  
12 3 50       14 if ($self->_retrieve_or_die($self->{input_args}, 0) eq 'help') {
13 0         0 $self->cmd_help();
14             return
15 0         0 }
16             else {
17 3         14 $self->check_privileges();
18             # would be very nice if we can set a type hint here...possible?
19 3         17 $self->_run_command();
20             }
21             }
22              
23 3     3   6 sub _run_command($self) {
  3         5  
  3         4  
24 3         9 my $interface = $self->_retrieve_or_die($self->{input_args}, 0);
25 3         5 my $offset = -1;
26 3         6 my $cur_start = 0;
27 3         5 my @input_args = @{$self->{input_args}};
  3         9  
28 3         5 for my $value (@{$self->{input_args}}) {
  3         7  
29 28 100       57 if ($value eq 'peer') {
30              
31             # if there is just one peer we skip here
32 5 100       13 if ($offset != 0) {
33 3         12 $self->_apply_change_set($interface, @input_args[$cur_start .. $offset]);
34 2         6 $cur_start = $offset;
35             }
36             }
37 27         33 $offset++;
38             }
39 2         12 $self->_apply_change_set($interface, @input_args[$cur_start .. $offset]);
40 2 50       10 if (defined $ENV{IS_TESTING}) {
41             # omit header
42 2         6 $self->wg_meta->commit(1, 1);
43             }
44             else {
45 0         0 $self->wg_meta->commit(1, 0);
46             }
47              
48             }
49              
50             # internal method to split commandline args into "change-sets".
51             # This method is fully* compatible with the `wg set`-syntax.
52             # *exception: remove
53 5     5   9 sub _apply_change_set($self, $interface, @change_set) {
  5         8  
  5         6  
  5         20  
  5         8  
54 5         9 my $offset = 1;
55 5         7 my $identifier;
56 5 100       16 if ($self->_retrieve_or_die(\@change_set, 1) eq 'peer') {
57             # this could be either a public key or alias
58 4         14 $identifier = $self->_retrieve_or_die(\@change_set, 2);
59              
60             # try to resolve alias
61 4         11 $identifier = $self->wg_meta->try_translate_alias($interface, $identifier);
62              
63 4         7 $offset += 2;
64             }
65             else {
66 1         3 $identifier = $interface;
67             }
68 5         15 my @value_keys = splice @change_set, $offset;
69              
70 5 50       14 if (@value_keys % 2 != 0) {
71 0         0 die "Odd number of value/key-pairs";
72             }
73             # parse key/value - pairs into a hash
74 5         8 my %args;
75 5         9 my $idx = 0;
76              
77 5         11 while ($idx < @value_keys) {
78 8         23 $args{$value_keys[$idx]} = $value_keys[$idx + 1];
79 8         18 $idx += 2;
80             }
81             # print "Got command set:
82             # interface: $interface
83             # ident: $identifier
84             # attrs: @value_keys
85             # ";
86              
87 5         13 $self->_set_values($interface, $identifier, \%args);
88             }
89 0     0 1 0 sub cmd_help($self) {
  0         0  
  0         0  
90 0         0 print "Usage: wg-meta set [attr1 value1] [attr2 value2] [peer {alias|public-key}] [attr1 value1] [attr2 value2] ...\n\n"
91             . "Notes:\n The interface aims to follow the official `wg set` specification\n"
92             . "To introduce new attributes prefix them with `+`";
93             }
94              
95 5     5   6 sub _set_values($self, $interface, $identifier, $ref_hash_values) {
  5         10  
  5         6  
  5         9  
  5         6  
  5         9  
96 5         6 for my $key (keys %{$ref_hash_values}) {
  5         18  
97 7         21 $self->wg_meta->set($interface, $identifier, $key, $ref_hash_values->{$key}, \&Wireguard::WGmeta::Cli::Commands::Command::_unknown_attr_handler);
98             }
99             }
100              
101             1;