File Coverage

blib/lib/App/Stow/Check.pm
Criterion Covered Total %
statement 18 48 37.5
branch 0 10 0.0
condition 0 9 0.0
subroutine 6 9 66.6
pod 2 2 100.0
total 26 78 33.3


line stmt bran cond sub pod time code
1             package App::Stow::Check;
2              
3 2     2   83076 use strict;
  2         11  
  2         65  
4 2     2   10 use warnings;
  2         4  
  2         67  
5              
6 2     2   10 use Cwd qw(realpath);
  2         5  
  2         127  
7 2     2   1144 use File::Spec::Functions qw(abs2rel splitdir);
  2         2007  
  2         142  
8 2     2   1068 use File::Which qw(which);
  2         2340  
  2         120  
9 2     2   4690 use Getopt::Std;
  2         148  
  2         982  
10              
11             our $VERSION = 0.01;
12              
13             # Constructor.
14             sub new {
15 0     0 1   my ($class, @params) = @_;
16              
17             # Create object.
18 0           my $self = bless {}, $class;
19              
20             # Object.
21 0           return $self;
22             }
23              
24             # Run.
25             sub run {
26 0     0 1   my $self = shift;
27              
28             # Process arguments.
29 0           $self->{'_opts'} = {
30             'h' => 0,
31             };
32 0 0 0       if (! getopts('h', $self->{'_opts'}) || $self->{'_opts'}->{'h'} || @ARGV < 1) {
      0        
33 0           print STDERR "Usage: $0 [-h] [--version] command\n";
34 0           print STDERR "\t-h\t\tHelp.\n";
35 0           print STDERR "\t--version\tPrint version.\n";
36 0           print STDERR "\tcommand\t\tCommand for which is stow dist looking.\n";
37 0           return 1;
38             }
39 0           $self->{'_command'} = $ARGV[0];
40              
41 0           my ($exit_code, $message) = $self->_check;
42 0 0         if ($exit_code == 0) {
43 0           print $message."\n";
44             } else {
45 0           print STDERR $message."\n";
46             }
47              
48 0           return $exit_code;
49             }
50              
51             sub _check {
52 0     0     my $self = shift;
53              
54 0           my $command = $self->{'_command'};
55              
56 0           my $command_path = which($command);
57 0 0         if (! defined $command_path) {
58 0           return (1, "Command '$command' not found.");
59             }
60              
61             # XXX Need to be flexible.
62 0           my $stow_dir = '/usr/local/stow';
63              
64 0           my $rel_path = abs2rel(realpath($command_path), $stow_dir);
65 0           my @rel_path = splitdir($rel_path);
66 0 0         if ($rel_path[0] eq '..') {
67 0           return (1, "Command '$command' doesn't use stow.");
68             }
69 0 0 0       if ($rel_path[1] ne 'bin' && $rel_path[1] ne 'sbin') {
70 0           return (1, "Command '$command' don't use 'bin/sbin' path.");
71             }
72              
73 0           return (0, $rel_path[0]);
74             }
75              
76             1;
77              
78              
79             __END__