Wireguard-WGmeta

 view release on metacpan or  search on metacpan

lib/Wireguard/WGmeta/Cli/Commands/Command.pm  view on Meta::CPAN


An instance of a Command

=cut

sub new($class, @input_arguments) {
    my $self = {
        'input_args' => \@input_arguments
    };
    # check if env var is available
    if (defined($ENV{'WIREGUARD_HOME'})) {
        $self->{wireguard_home} = $ENV{'WIREGUARD_HOME'};
    }
    else {
        $self->{wireguard_home} = WIREGUARD_HOME;
    }
    $self->{wg_meta} = undef;
    bless $self, $class;
    return $self;
}

#@returns Wireguard::WGmeta::Wrapper::Config
sub wg_meta($self) {
    # (sort of) singleton
    unless (defined $self->{wg_meta}){
        $self->{wg_meta} = Wireguard::WGmeta::Wrapper::Config->new($self->{wireguard_home});
    }
    return $self->{wg_meta};
}

=head2 entry_point()

Method called from L<Wireguard::WGmeta::Cli::Router/route_command($ref_list_input_args)>

=cut
sub entry_point($self) {
    die 'Please instantiate the actual implementation';
}

=head2 cmd_help()

Stub for specific cmd help. Is expected to terminate the command by calling C<exit()>
and print the help content directly to I<std_out>.

=cut
sub cmd_help($self) {
    die 'Please instantiate the actual implementation';
}

=head2 check_privileges()

Check if the user has r/w access to C<wireguard_home>.

B<Raises>

Exception if the user has insufficient privileges .

=cut
sub check_privileges($self) {
    if (not -w $self->{wireguard_home}) {
        my $username = getpwuid($<);
        die "Insufficient privileges - `$username` has no rw permissions to `$self->{wireguard_home}`. You probably forgot `sudo`";
    }
}

sub _retrieve_or_die($self, $ref_array, $idx) {
    my @arr = @{$ref_array};
    eval {return $arr[$idx]} or $self->cmd_help();
}

sub _unknown_attr_handler($attribute, $value) {
    my $prefix = substr $attribute, 0, 1;
    die "`$attribute` is unknown, please add `+` as prefix to add it" unless $prefix eq '+';
    return (substr $attribute, 1), $value;
}

1;



( run in 0.957 second using v1.01-cache-2.11-cpan-800906f7e73 )