App-OnePif

 view release on metacpan or  search on metacpan

lib/App/OnePif.pm  view on Meta::CPAN

      for my $list (values %$bt) {
         $retval{$_->{_id}} = $_ for @$list;
      }
      return \%retval;
   },
);

has records_bytype => (
   is => 'rw',
   lazy => 1,
   default => \&DEFAULT_records,
);

has JSON_decoder => (
   is      => 'rw',
   lazy    => 1,
   default => sub {
      for my $module (qw< JSON JSON::PP >) {
         (my $filename = "$module.pm") =~ s{::}{/}gmxs;
         my $retval = eval {
            require $filename;
            $module->can('decode_json');
         } or next;
         return $retval;
      } ## end for my $module (qw< JSON JSON::PP >)
      return;
   },
);

has YAML_dumper => (
   is      => 'rw',
   lazy    => 1,
   default => sub {
      for my $module (qw< YAML YAML::Tiny >) {
         (my $filename = "$module.pm") =~ s{::}{/}gmxs;
         my $retval = eval {
            require $filename;
            $module->can('Dump');
         } or next;
         return $retval;
      } ## end for my $module (qw< YAML YAML::Tiny >)
      return;
   },
);

has term => (
   is      => 'rw',
   lazy    => 1,
   default => sub {
      return Term::ReadLine->new('1password');
   },
);

has out => (
   is      => 'rw',
   lazy    => 1,
   default => sub {
      my ($self) = @_;
      my $term = $self->term();
      my $out = eval { $term->out() } || \*STDOUT;
      binmode $out, ':encoding(utf8)';
      return $out;
   },
);

has type => (
   is      => 'rw',
   lazy    => 1,
   default => sub { '*' },
);

has types => (
   is => 'rw',
   lazy => 1,
   default => \&DEFAULT_types,
);

sub run {
   my ($package, @ARGV) = @_;
   $package->new(args => \@ARGV)->run_interactive();
}

sub run_interactive {
   my ($self) = @_;
   my %main_for = (
      '.q' => 'quit',
      e => 'exit',
      f => 'file',
      h => 'help',
      l => 'list',
      p => 'print',
      q => 'quit',
      s => 'search',
      t => 'type',
      ts => 'types',
      u => 'type',
      use => 'type',
   );
   require Term::ReadLine;
   my $term = $self->term();
   my $out  = $self->out();
   $self->records_bytype; # just load...
   while (defined(my $line = $term->readline('1password> '))) {
      my ($command, $rest) = $line =~ m{\A \s* (\S+) \s* (.*?) \s*\z}mxs;
      next unless defined($command) && length($command);
      $command = $main_for{$command} if exists $main_for{$command};
      if (my $cb = $self->can("do_$command")) {
         $self->$cb($rest);
      }
      else {
         print {$out} "ERROR: unknown command [$command]\n",;
      }
   } ## end while (defined(my $line =...
} ## end sub run_interactive

sub attachments_for {
   my ($self, $uuid) = @_;
   my $target = $self->attachments_dir()->child($uuid);
   return unless $target->exists;
   return [ map { $_->stringify } $target->children ];
}



( run in 0.769 second using v1.01-cache-2.11-cpan-39bf76dae61 )