| 1 | # | ||||||||||
| 2 | # This file is part of App-KeePass2 | ||||||||||
| 3 | # | ||||||||||
| 4 | # This software is copyright (c) 2013 by celogeek <me@celogeek.com>. | ||||||||||
| 5 | # | ||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||
| 8 | # | ||||||||||
| 9 | package App::KeePass2; | ||||||||||
| 10 | |||||||||||
| 11 | # ABSTRACT: KeePass2 commandline tools | ||||||||||
| 12 | |||||||||||
| 13 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 2 | our $VERSION = '0.04'; # VERSION | |||||||||
| 17 | 2 | ▶ | use utf8::all; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 2 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 2 | with 'App::KeePass2::Icons'; | |||||||||
| 20 | 2 | ▶ | use MooX::Options; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 2 | ▶ | use File::KeePass; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 2 | ▶ | use IO::Prompt; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 2 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 2 | ▶ | use Data::Printer; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 2 | ▶ | use feature 'say'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | 2 | option 'file' => ( | |||||||||
| 28 | doc => 'Your keepass2 file', | ||||||||||
| 29 | is => 'ro', | ||||||||||
| 30 | short => 'f', | ||||||||||
| 31 | required => 1, | ||||||||||
| 32 | format => 's', | ||||||||||
| 33 | ); | ||||||||||
| 34 | |||||||||||
| 35 | 2 | option 'create' => ( | |||||||||
| 36 | doc => 'Create a keepass2 file', | ||||||||||
| 37 | is => 'ro', | ||||||||||
| 38 | short => 'c', | ||||||||||
| 39 | ); | ||||||||||
| 40 | |||||||||||
| 41 | 2 | option 'dump_groups' => ( | |||||||||
| 42 | doc => 'Dump the groups', | ||||||||||
| 43 | is => 'ro', | ||||||||||
| 44 | short => 'd', | ||||||||||
| 45 | ); | ||||||||||
| 46 | |||||||||||
| 47 | has _fkp => ( | ||||||||||
| 48 | is => 'ro', | ||||||||||
| 49 | 1 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 50 | 1 | File::KeePass->new; | |||||||||
| 51 | } | ||||||||||
| 52 | 2 | ); | |||||||||
| 53 | |||||||||||
| 54 | 2 | option 'list_groups' => ( | |||||||||
| 55 | doc => 'List the groups', | ||||||||||
| 56 | is => 'ro', | ||||||||||
| 57 | short => 'l', | ||||||||||
| 58 | ); | ||||||||||
| 59 | |||||||||||
| 60 | 0 | ▶ | sub run { | ||||||||
|
Subroutine
run: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 61 | 0 | my ($self) = @_; | |||||||||
| 62 | 0 | ▶ | $self->_create, return if ( $self->create ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 0 | ▶ | $self->_list_groups, return if ( $self->list_groups ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | 0 | ▶ | $self->_dump_groups, return if ( $self->dump_groups ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 0 | return; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 0 | ▶ | sub _get_master_key { | ||||||||
|
Subroutine
_get_master_key: not called CC=1 SCAR=6.9
| |||||||||||
| 69 | 0 | my ($self) = @_; | |||||||||
| 70 | 0 | return "" . prompt( "Master Password : ", -e => "*", -tty ); | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 0 | ▶ | sub _get_confirm_key { | ||||||||
|
Subroutine
_get_confirm_key: not called CC=1 SCAR=6.9
| |||||||||||
| 74 | 0 | my ($self) = @_; | |||||||||
| 75 | 0 | return "" . prompt( "Confirm Password : ", -e => "*", -tty ); | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 0 | ▶ | sub _create { | ||||||||
|
Subroutine
_create: not called CC=4 SCAR=30.0
| |||||||||||
| 79 | 0 | my ($self) = @_; | |||||||||
| 80 | 0 | ▶ | croak "The file already exists !" if -f $self->file; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 0 | $self->_fkp->clear; | |||||||||
| 82 | 0 | my $root = $self->_fkp->add_group( | |||||||||
| 83 | { title => 'My Passwords', | ||||||||||
| 84 | icon => $self->get_icon_id_from_key('key') | ||||||||||
| 85 | } | ||||||||||
| 86 | ); | ||||||||||
| 87 | 0 | my $gid = $root->{'id'}; | |||||||||
| 88 | 0 | $self->_fkp->add_group( | |||||||||
| 89 | { title => 'Internet', | ||||||||||
| 90 | group => $gid, | ||||||||||
| 91 | icon => $self->get_icon_id_from_key('internet') | ||||||||||
| 92 | } | ||||||||||
| 93 | ); | ||||||||||
| 94 | 0 | $self->_fkp->add_group( | |||||||||
| 95 | { title => 'Private', | ||||||||||
| 96 | group => $gid, | ||||||||||
| 97 | icon => $self->get_icon_id_from_key('key5') | ||||||||||
| 98 | } | ||||||||||
| 99 | ); | ||||||||||
| 100 | 0 | $self->_fkp->add_group( | |||||||||
| 101 | { title => 'Bank', | ||||||||||
| 102 | group => $gid, | ||||||||||
| 103 | icon => $self->get_icon_id_from_key('dollar') | ||||||||||
| 104 | } | ||||||||||
| 105 | ); | ||||||||||
| 106 | 0 | ▶ | $self->_fkp->unlock if $self->_fkp->is_locked; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | 0 | my $master = $self->_get_master_key; | |||||||||
| 108 | 0 | my $confirm = $self->_get_confirm_key; | |||||||||
| 109 | 0 | ▶ | croak "Your master password is different from the confirm password !" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | if $master ne $confirm; | ||||||||||
| 111 | 0 | $self->_fkp->save_db( $self->file, $master ); | |||||||||
| 112 | 0 | return; | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | 0 | ▶ | sub _dump_groups { | ||||||||
|
Subroutine
_dump_groups: not called CC=1 SCAR=6.9
| |||||||||||
| 116 | 0 | my ($self) = @_; | |||||||||
| 117 | 0 | $self->_fkp->load_db( $self->file, $self->_get_master_key ); | |||||||||
| 118 | 0 | p( $self->_fkp->groups ); | |||||||||
| 119 | 0 | return; | |||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | 0 | ▶ | sub _list_groups { | ||||||||
|
Subroutine
_list_groups: not called CC=1 SCAR=6.9
| |||||||||||
| 123 | 0 | my ($self) = @_; | |||||||||
| 124 | 0 | $self->_fkp->load_db( $self->file, $self->_get_master_key ); | |||||||||
| 125 | 0 | $self->_display_groups( $self->_fkp->groups, 0 ); | |||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | 0 | ▶ | sub _display_groups { | ||||||||
|
Subroutine
_display_groups: not called CC=2 SCAR=17.9
| |||||||||||
| 129 | 0 | my ( $self, $groups, $level ) = @_; | |||||||||
| 130 | 0 | for my $group (@$groups) { | |||||||||
| 131 | 0 | my $key = $self->get_icon_key_from_id( $group->{icon} ); | |||||||||
| 132 | 0 | my $icon = $self->get_icon_char_from_key($key); | |||||||||
| 133 | 0 | say sprintf( "%s%-3s%s", " " x $level, $icon, $group->{title} ); | |||||||||
| 134 | 0 | $self->_display_groups( $group->{groups}, $level + 1 ); | |||||||||
| 135 | } | ||||||||||
| 136 | } | ||||||||||
| 137 | 2 | 1; | |||||||||
| 138 | |||||||||||
| 139 | __END__ | ||||||||||